$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - UWooCommerce - add cart discount programmatically?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - UWooCommerce - add cart discount programmatically?

matteradmin9PV0评论

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/)

Post a comment

comment list (0)

  1. No comments so far