php - 修复 WooCommerce 中购物车百分比的最大优惠券折扣

标签 php woocommerce cart coupon discount

我在 woocommerce 中有优惠券代码 (XYZ25),其中包含 25% 的折扣,最高折扣为 Rs.250。

如果用户使用优惠券代码 XYZ25 获得 25% 的折扣,我如何限制用户获得不超过 250 卢比的折扣。

最佳答案

Since Woocommerce 3.2 or 3.3, this code doesn't work anymore

  1. 您可以根据 **RS.250固定购物车折扣设置额外的优惠券 FIX250 代码 (不含税)并且最低消费 (4 x 250) = RS.1000

  2. 然后在下面脚本的帮助下,如果客户应用您的 XYZ25 优惠券代码并且如果购物车总计达到 Rs.1000,它将用 FIX250 替换 XYZ25 优惠券,同时显示一个解释性通知......

这是代码:

add_action( 'woocommerce_calculate_totals', 'coupon_discount_max_switch', 10, 1);
function coupon_discount_max_switch( $cart_obj ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Set HERE your 2 coupons slugs  <===  <===  <===  <===  <===  <===  <===  <===  <===
    $coupon_25_percent = 'xyz25';
    $coupon_25_fixed = 'fix250';

    // Set HERE the limit amount  <===  <===  <===  <===  <===  <===  <===  <===  <===  <===
    $limit = 250; // Without VAT

    $total_discount = $cart_obj->get_cart_discount_total(); // Total cart discount

    // When 'xyz25' is set and the total discount is reached
    if( $cart_obj->has_discount( $coupon_25_percent ) && $limit_icl_vat <= $total_discount ){
        // Remove the 'xyz25' coupon
        $cart_obj->remove_coupon( $coupon_25_percent );
        // Checking that the fixed dicount is not already set.
        if( ! $cart_obj->has_discount( $coupon_25_fixed ) ){
            // Add the 'fix250' coupon
            $cart_obj->add_discount( $coupon_25_fixed );

            // Displaying a custom message
            $message = __( "The cart discount limit of Rs.$limit is reached", "woocommerce" );
            wc_add_notice( $message, 'notice' );
        }
    } 
}

代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

此工作代码已在 WooCommerce 版本 2.6.x 和 3.0+ 上进行测试。

关于php - 修复 WooCommerce 中购物车百分比的最大优惠券折扣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43887663/

相关文章:

wordpress - 如何从 WooCommerce 购物车中删除 Shipping?

php - php 7 中的同步块(synchronized block)

javascript - 如何在单个产品页面 woocommerce 上显示高级自定义字段 Google map

php - 检查订单是否包含 Woocommerce 3 中的任何退款

php - woocommerce 构建类别树

php - WooCommerce:根据单个商品数量添加折扣

php - WooCommerce 再次订购并自定义商品数据以计算价格

php - 静态对象中的魔术方法

php - 使用构造函数的好处?

php - 如何记住 for 循环中表达式的顺序?