WC_Product_Data_Store_CPT::read_product_data()protectedWC 3.0.0

Read product data. Can be overridden by child classes to load other props.

Method of the class: WC_Product_Data_Store_CPT{}

No Hooks.

Return

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->read_product_data( $product );
$product(WC_Product) (required) (passed by reference — &)
Product object.

Changelog

Since 3.0.0 Introduced.

WC_Product_Data_Store_CPT::read_product_data() code WC 9.4.2

protected function read_product_data( &$product ) {
	$id                = $product->get_id();
	$post_meta_values  = get_post_meta( $id );
	$meta_key_to_props = array(
		'_sku'                   => 'sku',
		'_global_unique_id'      => 'global_unique_id',
		'_regular_price'         => 'regular_price',
		'_sale_price'            => 'sale_price',
		'_price'                 => 'price',
		'_sale_price_dates_from' => 'date_on_sale_from',
		'_sale_price_dates_to'   => 'date_on_sale_to',
		'total_sales'            => 'total_sales',
		'_tax_status'            => 'tax_status',
		'_tax_class'             => 'tax_class',
		'_manage_stock'          => 'manage_stock',
		'_backorders'            => 'backorders',
		'_low_stock_amount'      => 'low_stock_amount',
		'_sold_individually'     => 'sold_individually',
		'_weight'                => 'weight',
		'_length'                => 'length',
		'_width'                 => 'width',
		'_height'                => 'height',
		'_upsell_ids'            => 'upsell_ids',
		'_crosssell_ids'         => 'cross_sell_ids',
		'_purchase_note'         => 'purchase_note',
		'_default_attributes'    => 'default_attributes',
		'_virtual'               => 'virtual',
		'_downloadable'          => 'downloadable',
		'_download_limit'        => 'download_limit',
		'_download_expiry'       => 'download_expiry',
		'_thumbnail_id'          => 'image_id',
		'_stock'                 => 'stock_quantity',
		'_stock_status'          => 'stock_status',
		'_wc_average_rating'     => 'average_rating',
		'_wc_rating_count'       => 'rating_counts',
		'_wc_review_count'       => 'review_count',
		'_product_image_gallery' => 'gallery_image_ids',
	);

	$set_props = array();

	foreach ( $meta_key_to_props as $meta_key => $prop ) {
		$meta_value         = isset( $post_meta_values[ $meta_key ][0] ) ? $post_meta_values[ $meta_key ][0] : null;
		$set_props[ $prop ] = maybe_unserialize( $meta_value ); // get_post_meta only unserializes single values.
	}

	$set_props['category_ids']      = $this->get_term_ids( $product, 'product_cat' );
	$set_props['tag_ids']           = $this->get_term_ids( $product, 'product_tag' );
	$set_props['shipping_class_id'] = current( $this->get_term_ids( $product, 'product_shipping_class' ) );
	$set_props['gallery_image_ids'] = array_filter( explode( ',', $set_props['gallery_image_ids'] ?? '' ) );

	$product->set_props( $set_props );
}