简体   繁体   中英

How can I add a discount to the cart total in woocommerce?

I am trying to add some additional discounts to the cart total and I tried this code but it's not quite working for me.

function mysite_box_discount( ) {

global $woocommerce;

$total_disc = 10;

  // Alter the cart discount total
  $woocommerce->cart->discount_total = $total_disc;

}
add_action('woocommerce_calculate_totals', 'mysite_box_discount');

I also tried adding $cart as an argument to the function, but it didn't work. I also tried $cart->discount_total but it is not working for me either.

Try this code

function custom_wc_add_discount() {
    $total_disc = 10;
    WC()->cart->add_fee( 'Discount note', -$total_disc );
}
add_action( 'woocommerce_cart_calculate_fees','custom_wc_add_discount' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM