WC_Product_Variable_Data_Store_CPT::sync_price()
Sync variable product prices with children.
Method of the class: WC_Product_Variable_Data_Store_CPT{}
Hooks from the method
Return
null
. Nothing (null).
Usage
$WC_Product_Variable_Data_Store_CPT = new WC_Product_Variable_Data_Store_CPT(); $WC_Product_Variable_Data_Store_CPT->sync_price( $product );
- $product(WC_Product) (required) (passed by reference — &)
- Product object.
Changelog
Since 3.0.0 | Introduced. |
WC_Product_Variable_Data_Store_CPT::sync_price() WC Product Variable Data Store CPT::sync price code WC 9.4.2
public function sync_price( &$product ) { global $wpdb; $children = $product->get_visible_children(); if ( $children ) { $format = array_fill( 0, count( $children ), '%d' ); $query_in = '(' . implode( ',', $format ) . ')'; $prices = array_unique( $wpdb->get_col( $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_price' AND post_id IN {$query_in}", $children ) ) ); // @codingStandardsIgnoreLine. } else { $prices = array(); } delete_post_meta( $product->get_id(), '_price' ); delete_post_meta( $product->get_id(), '_sale_price' ); delete_post_meta( $product->get_id(), '_regular_price' ); if ( $prices ) { sort( $prices, SORT_NUMERIC ); // To allow sorting and filtering by multiple values, we have no choice but to store child prices in this manner. foreach ( $prices as $price ) { if ( is_null( $price ) || '' === $price ) { continue; } add_post_meta( $product->get_id(), '_price', $price, false ); } } $this->update_lookup_table( $product->get_id(), 'wc_product_meta_lookup' ); /** * Fire an action for this direct update so it can be detected by other code. * * @since 3.6 * @param int $product_id Product ID that was updated directly. */ do_action( 'woocommerce_updated_product_price', $product->get_id() ); }