我需要手动设置折扣而不使用优惠券。
我已经检查了 Github 中的 Woocommerce 源,我看到它使用了“set_discount_total”函数,但它不起作用。
我试过 WC()->cart->set_discount_total(15)
和 $order->set_discount_total(15);
但没什么。
这是我的代码:
<?php
function letsgo_order_total($order_id, $posted_data, $order) {
$order = wc_get_order( $order_id );
$order->set_discount_total(50);
}
add_action('woocommerce_checkout_order_processed','letsgo_order_total',10,3);
?>
实际上我发现了一种方法,它可以工作,但我不喜欢代码:
<?php
$cart_discount = 50;
$discount = (double)get_post_meta($order_id,'_cart_discount',true);
update_post_meta($order_id,'_cart_discount',$cart_discount + $discount);
?>
最佳答案
这是这个答案的代码片段,我使用过并且运行良好。 How to add discount to cart total?
// Hook before calculate fees
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
/**
* Add custom fee if more than three article
* @param WC_Cart $cart
*/
function add_custom_fees( WC_Cart $cart ){
if( $cart->cart_contents_count < 3 ){
return;
}
// Calculate the amount to reduce
$discount = $cart->subtotal * 0.1;
$cart->add_fee( 'You have more than 3 items in your cart, a 10% discount has been added.', -$discount);
}
关于wordpress - 在没有优惠券的情况下在 WooCommerce 中设置折扣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48777044/