如何在购物车总额中添加折扣? [英] How to add discount to cart total?

查看:24
本文介绍了如何在购物车总额中添加折扣?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据购物车中的产品数量添加折扣,此折扣将适用于购物车总数.不使用优惠券还有其他选择吗?

I need to add discount according to number of product in cart and this discount will apply on total of cart. Is there any other option without use of coupons?

推荐答案

这段代码应该可以工作:

This code should work:

add_action('woocommerce_before_cart_table', 'discount_when_produts_in_cart');
function discount_when_produts_in_cart( ) {
    global $woocommerce;
    if( $woocommerce->cart->cart_contents_count > 3 ) {
        $coupon_code = 'maryscode';
        if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
            $woocommerce->show_messages();
        }
        echo '<div class="woocommerce_message"><strong>You have more than 3 items in your cart, a 10% discount has been added.</strong></div>';
    }
}

以上将应用优惠券maryscode";如果客户购物车中有 4 件或更多产品,则添加到购物车.

The above will apply the coupon "maryscode" to the cart if there are 4 or more products in the customers cart.

将以下内容添加到您的 css

Add the following to your css

.coupon {
    display: none !important;
}

这篇关于如何在购物车总额中添加折扣?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆