WC_Product_Variation_Data_Store_CPT::read_product_data │ protected │ WC 3.0.0
Read post data.
Метод класса: WC_Product_Variation_Data_Store_CPT{}
Хуков нет.
Возвращает
null. Ничего (null).
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->read_product_data( $product );
- $product(WC_Product_Variation) (обязательный) (передается по ссылке — &)
- Product object.
Список изменений
| С версии 3.0.0 | Введена. |
Код WC_Product_Variation_Data_Store_CPT::read_product_data() WC Product Variation Data Store CPT::read product data WC 10.4.3
protected function read_product_data( &$product ) {
$id = $product->get_id();
$post_meta_values = get_post_meta( $id );
$meta_key_to_props = array(
'_variation_description' => 'description',
'_regular_price' => 'regular_price',
'_sale_price' => 'sale_price',
'_sale_price_dates_from' => 'date_on_sale_from',
'_sale_price_dates_to' => 'date_on_sale_to',
'_manage_stock' => 'manage_stock',
'_stock_status' => 'stock_status',
'_virtual' => 'virtual',
'_product_image_gallery' => 'gallery_image_ids',
'_download_limit' => 'download_limit',
'_download_expiry' => 'download_expiry',
'_downloadable' => 'downloadable',
'_sku' => 'sku',
'_global_unique_id' => 'global_unique_id',
'_stock' => 'stock_quantity',
'_weight' => 'weight',
'_length' => 'length',
'_width' => 'width',
'_height' => 'height',
'_low_stock_amount' => 'low_stock_amount',
'_backorders' => 'backorders',
'_cogs_total_value' => 'cogs_total_value',
'_cogs_value_is_additive' => 'cogs_value_is_additive',
'_tax_class' => 'tax_class',
);
$variation_data = array();
foreach ( $meta_key_to_props as $meta_key => $prop ) {
$meta_value = $post_meta_values[ $meta_key ][0] ?? '';
$variation_data[ $prop ] = maybe_unserialize( $meta_value ); // get_post_meta only unserializes single values.
}
$variation_data['gallery_image_ids'] = array_filter( explode( ',', $variation_data['gallery_image_ids'] ?? '' ) );
$variation_data['shipping_class_id'] = current( $this->get_term_ids( $id, 'product_shipping_class' ) );
$variation_data['image_id'] = get_post_thumbnail_id( $id );
$variation_data['tax_class'] = ! metadata_exists( 'post', $id, '_tax_class' ) ? 'parent' : $variation_data['tax_class'];
$product->set_props( $variation_data );
if ( $this->cogs_feature_is_enabled() ) {
$cogs_value = $variation_data['cogs_total_value'] ?? '';
$cogs_value = '' === $cogs_value ? null : (float) $cogs_value;
$cogs_value_is_additive = 'yes' === ( $variation_data['cogs_value_is_additive'] ?? '' );
$product->set_props(
array(
'cogs_value' => $cogs_value,
'cogs_value_is_additive' => $cogs_value_is_additive,
)
);
}
if ( $product->is_on_sale( 'edit' ) ) {
$product->set_price( $product->get_sale_price( 'edit' ) );
} else {
$product->set_price( $product->get_regular_price( 'edit' ) );
}
$parent_id = $product->get_parent_id();
$parent_object = get_post( $parent_id );
$terms = get_the_terms( $parent_id, 'product_visibility' );
$term_names = is_array( $terms ) ? wp_list_pluck( $terms, 'name' ) : array();
$exclude_search = in_array( 'exclude-from-search', $term_names, true );
$exclude_catalog = in_array( 'exclude-from-catalog', $term_names, true );
if ( $exclude_search && $exclude_catalog ) {
$catalog_visibility = CatalogVisibility::HIDDEN;
} elseif ( $exclude_search ) {
$catalog_visibility = CatalogVisibility::CATALOG;
} elseif ( $exclude_catalog ) {
$catalog_visibility = CatalogVisibility::SEARCH;
} else {
$catalog_visibility = CatalogVisibility::VISIBLE;
}
$parent_post_meta_values = get_post_meta( $parent_id );
$parent_meta_key_to_props = array(
'_sku' => 'sku',
'_global_unique_id' => 'global_unique_id',
'_manage_stock' => 'manage_stock',
'_backorders' => 'backorders',
'_stock' => 'stock_quantity',
'_weight' => 'weight',
'_length' => 'length',
'_width' => 'width',
'_height' => 'height',
'_tax_class' => 'tax_class',
'_purchase_note' => 'purchase_note',
'_sold_individually' => 'sold_individually',
'_tax_status' => 'tax_status',
'_crosssell_ids' => '_crosssell_ids',
);
$parent_data = array();
foreach ( $parent_meta_key_to_props as $meta_key => $prop ) {
$meta_value = $parent_post_meta_values[ $meta_key ][0] ?? '';
$parent_data[ $prop ] = maybe_unserialize( $meta_value ); // get_post_meta only unserializes single values.
}
$parent_data['title'] = $parent_object ? $parent_object->post_title : '';
$parent_data['status'] = $parent_object ? $parent_object->post_status : '';
$parent_data['shipping_class_id'] = absint( current( $this->get_term_ids( $parent_id, 'product_shipping_class' ) ) );
$parent_data['catalog_visibility'] = $catalog_visibility;
$parent_data['stock_quantity'] = wc_stock_amount( $parent_data['stock_quantity'] );
$parent_data['image_id'] = get_post_thumbnail_id( $parent_id );
$product->set_parent_data( $parent_data );
// Pull data from the parent when there is no user-facing way to set props.
$product->set_sold_individually( $parent_data['sold_individually'] );
$product->set_tax_status( $parent_data['tax_status'] );
$product->set_cross_sell_ids( $parent_data['_crosssell_ids'] );
if ( $this->cogs_feature_is_enabled() ) {
$this->load_cogs_data( $product );
}
}