Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * START patch for minimum price meta on bundle products
- */
- // We will create a new meta or update the existing one each time we update our products
- add_action('save_post_product', 'update_bundle_product_min_price', 10, 1);
- function update_bundle_product_min_price( $post_id) {
- $product = wc_get_product( $post_id );
- if($product->get_type() == 'grouped') {
- $bundled_items = $product->get_children();
- // Verify that the product bundle has bundle items
- if(count($bundled_items) > 0) {
- $lower_price = min_price_in_bundle($bundled_items);
- update_post_meta($post_id,'_min_group_product_price',$lower_price);
- }
- } else if ($product->get_type() == 'composite') {
- update_post_meta($post_id,'_min_group_product_price',get_post_meta($post_id,'_price',true));
- } else if($product->get_type() == 'variable'){
- update_post_meta($post_id,'_min_group_product_price',get_post_meta($post_id,'_min_variation_price',true));
- } else {
- update_post_meta($post_id,'_min_group_product_price',get_post_meta($post_id,'_regular_price',true));
- }
- }
- // Small helper function to find the cheapest product
- function min_price_in_bundle($bundle) {
- return min(array_map(function($b){
- return wc_get_product($b)->get_price();
- },$bundle));
- }
- /*
- * END patch for minimum price meta on bundle products
- */
Advertisement
Add Comment
Please, Sign In to add comment