plugin development - UWooCommerce - add cart discount programmatically?

admin2025-06-03  2

I need to develop a plugin that applies a discount to the cart if x number of products have been purchased. Could someone point me in the direction of the code/API I need to use.

Can find nothing from / and do not fancy paying $100+ for the official bulk discount plugin just to get some simple functionality like this.

I have found that WooCommerce's get_total_discount() function applies the woocommerce_cart_total_discount filter, but I don't seem to be able to successfully tap into that filter...

function mwe_calculate_discount( $total_discount, $cart ) {

    //global $woocommerce;
    $num_products_required_for_discount = 4;

    echo "<!-- Discount? -->";

    $number_products = $cart->cart_contents_count;
    if ($number_products >= $num_products_required_for_discount) {

        echo "<!-- QUALIFIES FOR DISCOUNT! -->";

        for ($i = 0; $i < $number_products; $i++) {

            // Get the reduced price of the product - TODO

            // Calculate $product_discount
            $product_discount = 11.11; // TODO

            // Add $product_discount to the $total_discount
            $total_discount += $product_discount;
        }

        echo "<!-- Total discount: $total_discount -->";

    }
    else {

        echo "<!-- NO DISCOUNT -->";

    }

    return $total_discount;

}
add_filter('woocommerce_cart_total_discount', 'mwe_calculate_discount', 10, 2);

...any idea what is wrong with my code?

I need to develop a plugin that applies a discount to the cart if x number of products have been purchased. Could someone point me in the direction of the code/API I need to use.

Can find nothing from https://docs.woothemes/documentation/plugins/woocommerce/woocommerce-codex/ and do not fancy paying $100+ for the official bulk discount plugin just to get some simple functionality like this.

I have found that WooCommerce's get_total_discount() function applies the woocommerce_cart_total_discount filter, but I don't seem to be able to successfully tap into that filter...

function mwe_calculate_discount( $total_discount, $cart ) {

    //global $woocommerce;
    $num_products_required_for_discount = 4;

    echo "<!-- Discount? -->";

    $number_products = $cart->cart_contents_count;
    if ($number_products >= $num_products_required_for_discount) {

        echo "<!-- QUALIFIES FOR DISCOUNT! -->";

        for ($i = 0; $i < $number_products; $i++) {

            // Get the reduced price of the product - TODO

            // Calculate $product_discount
            $product_discount = 11.11; // TODO

            // Add $product_discount to the $total_discount
            $total_discount += $product_discount;
        }

        echo "<!-- Total discount: $total_discount -->";

    }
    else {

        echo "<!-- NO DISCOUNT -->";

    }

    return $total_discount;

}
add_filter('woocommerce_cart_total_discount', 'mwe_calculate_discount', 10, 2);

...any idea what is wrong with my code?

Share Improve this question edited Oct 6, 2015 at 18:33 Boycott A.I. asked Oct 6, 2015 at 17:35 Boycott A.I.Boycott A.I. 16811 silver badges24 bronze badges 1
  • I have found the article about WooCommerce discount cart which describe how to get discount on total sales, orders, shipping cost and sales per item cloudways/blog/woocommerce-discount-cart – Owais Alam Commented Dec 14, 2017 at 8:47
Add a comment  | 

1 Answer 1

Reset to default 3

I haven't applied any discounts like that before, but have done it with 'fees' a lot.

Adding a fee is quite easy:

function custom_wc_add_fee() {
    WC()->cart->add_fee( 'Fee', -10 );
}
add_action( 'woocommerce_cart_calculate_fees','custom_wc_add_fee' );

(or if you want a plugin solution, I created this plugin: https://aceplugins/plugin/woocommerce-advanced-fees/)

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748961311a315183.html

最新回复(0)