WC_Product_Data_Store_CPT::handle_updated_props()
Handle updated meta props after updating meta data.
Method of the class: WC_Product_Data_Store_CPT{}
Hooks from the method
Return
null
. Nothing (null).
Usage
// protected - for code of main (parent) or child class $result = $this->handle_updated_props( $product );
- $product(WC_Product) (required) (passed by reference — &)
- Product Object.
Changelog
Since 3.0.0 | Introduced. |
WC_Product_Data_Store_CPT::handle_updated_props() WC Product Data Store CPT::handle updated props code WC 9.4.2
protected function handle_updated_props( &$product ) { $price_is_synced = $product->is_type( array( 'variable', 'grouped' ) ); if ( ! $price_is_synced ) { if ( in_array( 'regular_price', $this->updated_props, true ) || in_array( 'sale_price', $this->updated_props, true ) ) { if ( $product->get_sale_price( 'edit' ) >= $product->get_regular_price( 'edit' ) ) { update_post_meta( $product->get_id(), '_sale_price', '' ); $product->set_sale_price( '' ); } } /** * It's tempting to add a check for `_price` in the updated props, so that when `wc_scheduled_sales` is called, we don't have to rely on `date_on_sale_from` being present in the list of updated props. * * However, it has a side effect of overriding the `_price` meta value with the `_sale_price` meta value when product is in sale, or with `_regular_price` meta value when product is not in sale. This is not desirable, because `_price` can also be set as a temporary active price for a product, and we don't want to override it. * * If we want to preserve previous sales schedules, a better way would be to store them in dedicated meta keys as logs. */ $product_price_props = array( 'date_on_sale_from', 'date_on_sale_to', 'regular_price', 'sale_price', 'product_type' ); if ( count( array_intersect( $product_price_props, $this->updated_props ) ) > 0 ) { if ( $product->is_on_sale( 'edit' ) ) { update_post_meta( $product->get_id(), '_price', $product->get_sale_price( 'edit' ) ); $product->set_price( $product->get_sale_price( 'edit' ) ); } else { update_post_meta( $product->get_id(), '_price', $product->get_regular_price( 'edit' ) ); $product->set_price( $product->get_regular_price( 'edit' ) ); } } } if ( in_array( 'stock_quantity', $this->updated_props, true ) ) { if ( $product->is_type( 'variation' ) ) { /** * Action to signal that the value of 'stock_quantity' for a variation has changed. * * @since 3.0 * * @param WC_Product $product The variation whose stock has changed. */ do_action( 'woocommerce_variation_set_stock', $product ); } else { /** * Action to signal that the value of 'stock_quantity' for a product has changed. * * @since 3.0 * * @param WC_Product $product The variation whose stock has changed. */ do_action( 'woocommerce_product_set_stock', $product ); } } if ( in_array( 'stock_status', $this->updated_props, true ) ) { if ( $product->is_type( 'variation' ) ) { /** * Action to signal that the `stock_status` for a variation has changed. * * @since 3.0 * * @param int $product_id The ID of the variation. * @param string $stock_status The new stock status of the variation. * @param WC_Product $product The product object. */ do_action( 'woocommerce_variation_set_stock_status', $product->get_id(), $product->get_stock_status(), $product ); } else { /** * Action to signal that the `stock_status` for a product has changed. * * @since 3.0 * * @param int $product_id The ID of the product. * @param string $stock_status The new stock status of the product. * @param WC_Product $product The product object. */ do_action( 'woocommerce_product_set_stock_status', $product->get_id(), $product->get_stock_status(), $product ); } } if ( array_intersect( $this->updated_props, array( 'sku', 'global_unique_id', 'regular_price', 'sale_price', 'date_on_sale_from', 'date_on_sale_to', 'total_sales', 'average_rating', 'stock_quantity', 'stock_status', 'manage_stock', 'downloadable', 'virtual', 'tax_status', 'tax_class' ) ) ) { $this->update_lookup_table( $product->get_id(), 'wc_product_meta_lookup' ); } // Trigger action so 3rd parties can deal with updated props. do_action( 'woocommerce_product_object_updated_props', $product, $this->updated_props ); // After handling, we can reset the props array. $this->updated_props = array(); }