milardovich

Facebook Product Feed dynamic attribute for WooCommerce

Mar 17th, 2020
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.31 KB | None | 0 0
  1. /*
  2.  * START patch for minimum price meta on bundle products
  3.  */
  4. // We will create a new meta or update the existing one each time we update our products
  5. add_action('save_post_product', 'update_bundle_product_min_price', 10, 1);
  6. function update_bundle_product_min_price( $post_id) {
  7.     $product = wc_get_product( $post_id );
  8.     if($product->get_type() == 'grouped') {
  9.         $bundled_items = $product->get_children();
  10.         // Verify that the product bundle has bundle items
  11.         if(count($bundled_items) > 0) {
  12.             $lower_price = min_price_in_bundle($bundled_items);
  13.             update_post_meta($post_id,'_min_group_product_price',$lower_price);
  14.         }
  15.     } else if ($product->get_type() == 'composite') {
  16.         update_post_meta($post_id,'_min_group_product_price',get_post_meta($post_id,'_price',true));
  17.     } else if($product->get_type() == 'variable'){
  18.        update_post_meta($post_id,'_min_group_product_price',get_post_meta($post_id,'_min_variation_price',true));
  19.     } else {
  20.        update_post_meta($post_id,'_min_group_product_price',get_post_meta($post_id,'_regular_price',true));
  21.     }
  22. }
  23. // Small helper function to find the cheapest product
  24. function min_price_in_bundle($bundle) {
  25.     return min(array_map(function($b){
  26.         return wc_get_product($b)->get_price();
  27.     },$bundle));
  28. }
  29. /*
  30.  * END patch for minimum price meta on bundle products
  31.  */
Advertisement
Add Comment
Please, Sign In to add comment