WC_Product_Data_Store_CPT::update_post_meta( WC_Product $product, bool $force = false )
Helper method that updates all the post meta for a product based on it’s settings in the WC_Product class.
Description Description
Parameters Parameters
- $product
-
(Required) Product object.
- $force
-
(Optional) Force update. Used during create.
Default value: false
Source Source
File: includes/data-stores/class-wc-product-data-store-cpt.php
protected function update_post_meta( &$product, $force = false ) { $meta_key_to_props = array( '_sku' => 'sku', '_regular_price' => 'regular_price', '_sale_price' => 'sale_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', '_product_image_gallery' => 'gallery_image_ids', '_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', ); // Make sure to take extra data (like product url or text for external products) into account. $extra_data_keys = $product->get_extra_data_keys(); foreach ( $extra_data_keys as $key ) { $meta_key_to_props[ '_' . $key ] = $key; } $props_to_update = $force ? $meta_key_to_props : $this->get_props_to_update( $product, $meta_key_to_props ); foreach ( $props_to_update as $meta_key => $prop ) { $value = $product->{"get_$prop"}( 'edit' ); $value = is_string( $value ) ? wp_slash( $value ) : $value; switch ( $prop ) { case 'virtual': case 'downloadable': case 'manage_stock': case 'sold_individually': $value = wc_bool_to_string( $value ); break; case 'gallery_image_ids': $value = implode( ',', $value ); break; case 'date_on_sale_from': case 'date_on_sale_to': $value = $value ? $value->getTimestamp() : ''; break; } $updated = $this->update_or_delete_post_meta( $product, $meta_key, $value ); if ( $updated ) { $this->updated_props[] = $prop; } } // Update extra data associated with the product like button text or product URL for external products. if ( ! $this->extra_data_saved ) { foreach ( $extra_data_keys as $key ) { $meta_key = '_' . $key; $function = 'get_' . $key; if ( ! array_key_exists( $meta_key, $props_to_update ) ) { continue; } if ( is_callable( array( $product, $function ) ) ) { $value = $product->{$function}( 'edit' ); $value = is_string( $value ) ? wp_slash( $value ) : $value; $updated = $this->update_or_delete_post_meta( $product, $meta_key, $value ); if ( $updated ) { $this->updated_props[] = $key; } } } } if ( $this->update_downloads( $product, $force ) ) { $this->updated_props[] = 'downloads'; } }
Changelog Changelog
Version | Description |
---|---|
3.0.0 | Introduced. |