wordpress - 在没有优惠券的情况下在 WooCommerce 中设置折扣

标签 wordpress woocommerce discount

我需要手动设置折扣而不使用优惠券。

我已经检查了 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/

相关文章:

php - 在 WooCommerce 结账时禁用 AJAX

java - 特定银行信用卡的 Stripe 付款折扣

html - 此页面上的第二个滚动条 - 不需要

php - 使用 $.ajax 向 PHP 传递数据会使 $_POST 为空

php - 在 WooCommerce 格式的产品尺寸输出中包含尺寸字母 L/W/H

wordpress - Woocommerce REST API 404 错误

php - WordPress - 生成帖子的 PDF

wordpress - Wordpress cronjob 取消已删除产品的 WC 订单?

php - IF/ELSEIF 比较数据库中的变量

Paypal Express Checkout 提供全商品折扣