WC_Product_Grouped_Data_Store_CPT::update_prices_from_children
Loop over child products and update the grouped product prices.
Method of the class: WC_Product_Grouped_Data_Store_CPT{}
Hooks from the method
Returns
null. Nothing (null).
Usage
// protected - for code of main (parent) or child class $result = $this->update_prices_from_children( $product );
- $product(WC_Product) (required) (passed by reference — &)
- Product object.
WC_Product_Grouped_Data_Store_CPT::update_prices_from_children() WC Product Grouped Data Store CPT::update prices from children code WC 11.2.2
protected function update_prices_from_children( &$product ) {
$product_id = $product->get_id();
$child_prices = array();
$child_ids = $product->get_children( 'edit' );
if ( ! empty( $child_ids ) ) {
// Prime caches to reduce future queries.
_prime_post_caches( $child_ids );
foreach ( $child_ids as $child_id ) {
$child = wc_get_product( $child_id );
if ( $child ) {
$child_prices[] = $child->get_price( 'edit' );
}
}
$child_prices = array_filter( $child_prices );
}
// Performance note: prefilter with metadata_exists, to avoid unnecessary meta cache invalidations and SQLs.
$price_metas = array_filter(
array( '_price', '_sale_price', '_regular_price' ),
static fn( $meta_key ) => metadata_exists( 'post', $product_id, $meta_key )
);
array_walk( $price_metas, static fn( $meta_key ) => delete_post_meta( $product_id, $meta_key ) );
if ( ! empty( $child_prices ) ) {
add_post_meta( $product_id, '_price', min( $child_prices ) );
add_post_meta( $product_id, '_price', max( $child_prices ) );
}
$this->update_lookup_table( $product_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_id );
}