Skip to content
Advertisement

Bulk dynamic pricing for WooCommerce products with specific product-tag

I’m trying to add dynamic discount to all products who have the tag: “bulk-discount” I want the discount to happen if a customer buys eg. 5 similar or different prducts with the tag.

I’m working with this code. And this answer. This is what i have:

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;
 
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {

    // Get an instance of the WC_Product object
    $product = $cart_item['data'];

    // Get product id
    $product_id = $cart_item['product_id'];

    if( method_exists( $product, 'set_name' ) && has_term( 'bulk-discount', 'product_tag', $product_id ) ) {
        
    // Define discount rules and thresholds
    $threshold1 = 5; // Change price if items > 4
    $discount1 = 0.05; // Reduce unit price by 5%
    $threshold2 = 10; // Change price if items > 9
    $discount2 = 0.1; // Reduce unit price by 10%
 
    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 );
      }    
    }
    
 }

Advertisement

Answer

The first loop counts how many times the tag appears on a single product or multiple items from the same product

The 2nd loop applies the discount if the condition is met

function bbloomer_quantity_based_pricing( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
    
    // count tag found
    $tag_found = 0;

    // Loop through cart items, count how many times tag occurs
    foreach ( $cart->get_cart() as $cart_item ) {
        // Get an instance of the WC_Product object
        $product = $cart_item['data'];

        // Get product id
        $product_id = $cart_item['product_id'];

        // if product has tag
        if( has_term( 'bulk-discount', 'product_tag', $product_id ) ) {
            
            // Get quantity from product in cart
            $quantity = $cart_item['quantity'];
            
            // if product quantity > 1
            if ( $quantity > 1) {
                $tag_found = $tag_found + $quantity;
            } else {
                $tag_found += 1;                
            }
        }
    }
    
    // Define discount rules and thresholds
    $threshold = 5; // Change price if items > 4
    $discount = 0.05; // Reduce unit price by 5%
    
    // if tag found >= $threshold
    if ( $tag_found >= $threshold ) {
        // Loop through cart items, add discount
        foreach ( $cart->get_cart() as $cart_item ) {
            // Get an instance of the WC_Product object
            $product = $cart_item['data'];

            // Get product id
            $product_id = $cart_item['product_id'];

            // if product has tag
            if( has_term( 'bulk-discount', 'product_tag', $product_id ) ) {
                // calculate new price
                $price = round( $cart_item['data']->get_price() * ( 1 - $discount ), 2 );
        
                // set new price
                $cart_item['data']->set_price( $price );
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'bbloomer_quantity_based_pricing', 10, 1 );

Related: Multiple bulk dynamic pricing for WooCommerce products with specific product-tag

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement