Bulk (Dynamic) Pricing - WooCommerce

Viewed 93

The plan is to apply bulk discount pricing per product using Advanced Custom Fields ie. each product may have its own bulk pricing requirement/s which would be entered via the relevant fields eg. Threshold 1, discount 1 etc.

We have found that the 'Bulk (Dynamic) Pricing - WooCommerce' snippet by Rodolfo Melogli is suitable in regard to a fixed Qty/Price via hard coding however we can't seem to get it to function when allocating the ACF to the relevant variables... what are we missing?

  /**
 * @snippet       Bulk (Dynamic) Pricing - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.8
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 * modified version with fixed price discount 
 */
 
add_action( 'woocommerce_before_calculate_totals', 'bbloomer_quantity_based_pricing', 9999 );
 
function bbloomer_quantity_based_pricing( $cart ) {
 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; 
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
 
    // Define discount rules and thresholds
    $threshold1 = 100; // Change price if items > 99

//Define variable value via ACF eg. $threshold1 = get_field('discount_threshold_1');

    $discount1 = .10; // Reduce unit price by 20c
    $threshold2 = 200; // Change price if items > 199
    $discount2 = .20; // Reduce unit price by 40c
 
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
      if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
         $price = round( $cart_item['data']->get_price() - $discount1, 2 );
         $cart_item['data']->set_price( $price );
      } elseif ( $cart_item['quantity'] >= $threshold2 ) {
         $price = round( $cart_item['data']->get_price() - $discount2, 2 );
         $cart_item['data']->set_price( $price );
      }    
    }
    
 }

Also, as we are considering up to 5 qty/discount options how would we adjust the code... would we be better off utilising a PHP switch?

Pretty new to this stuff, any help would be greatly appreciated :) A

0 Answers
Related