Last updated on October 18, 2023

WooCommerce bulk discount rules

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Set up bulk discount rules effectively in WooCommerce.

The code snippet below demonstrates how to create bulk discount rules for WooCommerce using custom PHP functions. This functionality can be useful for online store owners who want to offer discounts to customers who purchase a certain quantity of products.

/**
 * Apply bulk discount rules based on quantity of products in cart.
 *
 * @param float $discount The discount amount.
 * @param float $cart_total The total amount in the cart.
 * @param int   $cart_item_count The total number of items in the cart.
 * @return float The updated discount amount.
 */
function wpsnippets_apply_bulk_discount( $discount, $cart_total, $cart_item_count ) {
    // Define the discount rules based on quantity thresholds and discount percentages.
    $discount_rules = array(
        array(
            'quantity' => 5,
            'discount' => 10,
        ),
        array(
            'quantity' => 10,
            'discount' => 15,
        ),
    );

    // Loop through the discount rules and apply the appropriate discount.
    foreach ( $discount_rules as $rule ) {
        if ( $cart_item_count >= $rule['quantity'] ) {
            $discount = ( $cart_total * $rule['discount'] ) / 100;
            break; // Exit the loop after applying the first matching discount.
        }
    }

    return $discount;
}
add_filter( 'woocommerce_cart_discount_total', 'wpsnippets_apply_bulk_discount', 10, 3 );

In this code example, we define an array of discount rules that specify the quantity threshold and the corresponding discount percentage. The wpsnippets_apply_bulk_discount function takes three parameters: the initial discount amount, the total amount in the cart, and the total number of items in the cart.

Inside the function, we loop through the discount rules and check if the cart item count meets the quantity threshold. If it does, we calculate the discount amount based on the specified percentage and update the $discount variable. We use the break statement to exit the loop after applying the first matching discount.

Finally, we return the updated discount amount. The add_filter function is used to hook this custom function into the woocommerce_cart_discount_total filter, which allows us to modify the discount applied to the cart total.

Examples

Example 1: Applying a bulk discount to specific product categories

This use case demonstrates how to apply a bulk discount to specific product categories in WooCommerce. The code example below uses the woocommerce_product_get_categories filter to check if a product belongs to a specific category, and if so, applies a discount to the product price.

function wpsnippets_apply_bulk_discount_to_categories( $price, $product ) {
    // Define the product categories to apply the discount to
    $discounted_categories = array( 'category1', 'category2' );

    // Get the product categories
    $categories = wp_get_post_terms( $product->get_id(), 'product_cat', array( 'fields' => 'slugs' ) );

    // Check if the product belongs to any of the discounted categories
    if ( array_intersect( $categories, $discounted_categories ) ) {
        // Apply a 10% discount to the product price
        $price = $price * 0.9;
    }

    return $price;
}
add_filter( 'woocommerce_product_get_price', 'wpsnippets_apply_bulk_discount_to_categories', 10, 2 );

In this code example, we define an array of product categories to apply the discount to. Then, we use the wp_get_post_terms function to retrieve the categories of the current product. If any of the product categories intersect with the discounted categories, we apply a 10% discount to the product price.

Example 2: Applying a bulk discount based on the quantity of items in the cart

This use case demonstrates how to apply a bulk discount based on the quantity of items in the WooCommerce cart. The code example below uses the woocommerce_cart_calculate_fees action to calculate the discount based on the total quantity of items in the cart.

function wpsnippets_apply_bulk_discount_based_on_quantity( $cart ) {
    // Define the discount percentage based on the quantity
    $discount_percentage = 0.05; // 5% discount

    // Get the total quantity of items in the cart
    $total_quantity = WC()->cart->get_cart_contents_count();

    // Calculate the discount amount
    $discount_amount = $cart->subtotal * $discount_percentage * $total_quantity;

    // Apply the discount as a fee
    $cart->add_fee( 'Bulk Discount', -$discount_amount );
}
add_action( 'woocommerce_cart_calculate_fees', 'wpsnippets_apply_bulk_discount_based_on_quantity', 10, 1 );

In this code example, we define a discount percentage based on the quantity of items in the cart. We then calculate the discount amount by multiplying the cart subtotal with the discount percentage and the total quantity of items. Finally, we apply the discount as a fee using the add_fee method.

Example 3: Applying a bulk discount to specific user roles

This use case demonstrates how to apply a bulk discount to specific user roles in WooCommerce. The code example below uses the woocommerce_product_get_price filter to check if the current user has a specific role, and if so, applies a discount to the product price.

function wpsnippets_apply_bulk_discount_to_user_roles( $price, $product ) {
    // Define the user roles to apply the discount to
    $discounted_roles = array( 'customer_role1', 'customer_role2' );

    // Get the current user roles
    $user = wp_get_current_user();
    $user_roles = $user->roles;

    // Check if the user has any of the discounted roles
    if ( array_intersect( $user_roles, $discounted_roles ) ) {
        // Apply a 15% discount to the product price
        $price = $price * 0.85;
    }

    return $price;
}
add_filter( 'woocommerce_product_get_price', 'wpsnippets_apply_bulk_discount_to_user_roles', 10, 2 );

In this code example, we define an array of user roles to apply the discount to. We then use the wp_get_current_user function to retrieve the current user and their roles. If any of the user roles intersect with the discounted roles, we apply a 15% discount to the product price.

Last updated on October 18, 2023. Originally posted on December 29, 2023.

Leave a Reply