Search code examples
phpwordpresswoocommerceproductcart

Cart bulk quantity discount for specific product tag IDs in WooCommerce


I want to create a function to create conditional bulk dynamic pricing for WooCommerce, different unit prices based on the quantity added to cart.

I have two product tags ids (326, 327) and there is a bunch of product in each tag. I want to give a different discount for both tags and for the same quantity.

  • Tag ID 326
  • 15% discount above 10 units
  • 20% discount above 25 units

  • Tag ID 327
  • 20% discount above 10 units
  • 35% discount above 25 units.

This is the code I am working with:

add_action( 'woocommerce_before_calculate_totals', 'woocommerce_quantity_based_pricing', 9999 );
function woocommerce_quantity_based_pricing( $cart ) {
 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
 
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
     
    
    if( has_term( 326, 'product_tag' ) ) {
        $threshold1 = 10; 
        $discount1 = 0.15;
        $threshold2 = 25; 
        $discount2 = 0.3;
} elseif( has_term( 327, 'product_tag' ) ) {
        $threshold1 = 10; 
        $discount1 = 0.2; 
        $threshold2 = 25; 
        $discount2 = 0.35; 
}
    
    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() * ( 1 - $discount1 ), 2 );
         $cart_item['data']->set_price( $price );
      } elseif ( $cart_item['quantity'] >= $threshold2 ) {
         $price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );
         $cart_item['data']->set_price( $price );
      }    
    }
    
 }

Unfortunately without the desired result. Any advice?


Solution

  • To apply a discount based on product tag IDs, you can use WC_Product::get_tag_ids(), and then compare it.

    I have provided an extra piece of code via the woocommerce_cart_item_price hook, which will allow you to strikethrough the original price and show the new price next to it

    So you get:

    // Used to calculate totals
    function action_woocommerce_before_calculate_totals( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
        
        // Set Tag IDs
        $tag_id_1 = 326;
        $tag_id_2 = 327;
        
        // Tag ID 1
        // 15% discount above 10 units 
        $tag_id_1_threshold_1 = 10;
        $tag_id_1_discount_1  = 0.15;
        // 20% discount above 25 units
        $tag_id_1_threshold_2 = 25;
        $tag_id_1_discount_2 = 0.20;
        
        // Tag ID 2
        // 20% discount above 10 units 
        $tag_id_2_threshold_1 = 10;
        $tag_id_2_discount_1  = 0.20;
        // 35% discount above 25 units
        $tag_id_2_threshold_2 = 25;
        $tag_id_2_discount_2 = 0.35;
    
        // Loop through cart items
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
            // Get price
            $price = $cart_item['data']->get_price();
            
            // Get quantity
            $quantity = $cart_item['quantity'];
            
            // Get product tag ID's
            $tag_ids = $cart_item['data']->get_tag_ids();
            
            // Check for tag ID
            if ( in_array ( $tag_id_1, $tag_ids ) ) {
                // Compare
                if ( $quantity >= $tag_id_1_threshold_1 && $quantity < $tag_id_1_threshold_2 ) {
                    $discount = $tag_id_1_discount_1;
                } elseif ( $quantity >= $tag_id_1_threshold_2 ) {
                    $discount = $tag_id_1_discount_2;
                }
            } elseif ( in_array ( $tag_id_2, $tag_ids ) ) {
                // Compare
                if ( $quantity >= $tag_id_2_threshold_1 && $quantity < $tag_id_2_threshold_2 ) {
                    $discount = $tag_id_2_discount_1;
                } elseif ( $quantity >= $tag_id_2_threshold_2 ) {
                    $discount = $tag_id_2_discount_2;
                }               
            }
            
            // Isset & NOT empty
            if ( isset( $discount ) && ! empty( $discount ) ) {         
                // Set new price
                $cart_item['data']->set_price( $price * ( 1 - $discount ) );
                
                // Reset
                $discount = '';
            }
        }
    }
    add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );  
    
    // Strikethrough the original price
    function filter_woocommerce_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
        // Get the product object
        $product = $cart_item['data'];
        
        // Is a WC product
        if ( is_a( $product, 'WC_Product' ) ) {
            // Get reqular price
            $regular_price = $product->get_regular_price();
            
            // New price
            $new_price = $cart_item['data']->get_price();
            
            // NOT empty and NOT equal
            if ( ! empty ( $regular_price ) && $regular_price != $new_price ) {
                // Output
                $price_html = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $new_price ) . '</ins>';        
            }
        }
    
        return $price_html;
    }
    add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 );
    

    enter image description here