WC_Product_Variation_Data_Store_CPT::read()publicWC 3.0.0

Reads a product from the database and sets its data to the class.

Метод класса: WC_Product_Variation_Data_Store_CPT{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$WC_Product_Variation_Data_Store_CPT = new WC_Product_Variation_Data_Store_CPT();
$WC_Product_Variation_Data_Store_CPT->read( $product );
$product(WC_Product_Variation) (обязательный) (передается по ссылке — &)
Product object.

Список изменений

С версии 3.0.0 Введена.

Код WC_Product_Variation_Data_Store_CPT::read() WC 9.4.2

public function read( &$product ) {
	$product->set_defaults();

	if ( ! $product->get_id() ) {
		return;
	}

	$post_object = get_post( $product->get_id() );

	if ( ! $post_object ) {
		return;
	}

	if ( 'product_variation' !== $post_object->post_type ) {
		throw new WC_Data_Exception( 'variation_invalid_id', __( 'Invalid product type: passed ID does not correspond to a product variation.', 'woocommerce' ) );
	}

	$product->set_props(
		array(
			'name'              => $post_object->post_title,
			'slug'              => $post_object->post_name,
			'date_created'      => $this->string_to_timestamp( $post_object->post_date_gmt ),
			'date_modified'     => $this->string_to_timestamp( $post_object->post_modified_gmt ),
			'status'            => $post_object->post_status,
			'menu_order'        => $post_object->menu_order,
			'reviews_allowed'   => 'open' === $post_object->comment_status,
			'parent_id'         => $post_object->post_parent,
			'attribute_summary' => $post_object->post_excerpt,
		)
	);

	// The post parent is not a valid variable product so we should prevent this.
	if ( $product->get_parent_id( 'edit' ) && 'product' !== get_post_type( $product->get_parent_id( 'edit' ) ) ) {
		$product->set_parent_id( 0 );
	}

	$this->read_downloads( $product );
	$this->read_product_data( $product );
	$this->read_extra_data( $product );
	$product->set_attributes( wc_get_product_variation_attributes( $product->get_id() ) );

	$updates = array();
	/**
	 * If a variation title is not in sync with the parent e.g. saved prior to 3.0, or if the parent title has changed, detect here and update.
	 */
	$new_title = $this->generate_product_title( $product );

	if ( $post_object->post_title !== $new_title ) {
		$product->set_name( $new_title );
		$updates = array_merge( $updates, array( 'post_title' => $new_title ) );
	}

	/**
	 * If the attribute summary is not in sync, update here. Used when searching for variations by attribute values.
	 * This is meant to also cover the case when global attribute name or value is updated, then the attribute summary is updated
	 * for respective products when they're read.
	 */
	$new_attribute_summary = $this->generate_attribute_summary( $product );

	if ( $new_attribute_summary !== $post_object->post_excerpt ) {
		$product->set_attribute_summary( $new_attribute_summary );
		$updates = array_merge( $updates, array( 'post_excerpt' => $new_attribute_summary ) );
	}

	if ( ! empty( $updates ) ) {
		$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $updates, array( 'ID' => $product->get_id() ) );
		clean_post_cache( $product->get_id() );
	}

	// Set object_read true once all data is read.
	$product->set_object_read( true );
}