WC_Product_Variable_Data_Store_CPT::sync_price( WC_Product $product )
Sync variable product prices with children.
Description Description
Parameters Parameters
- $product
-
(Required) Product object.
Source Source
File: includes/data-stores/class-wc-product-variable-data-store-cpt.php
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() );
}
Changelog Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |