php – 在WooCommerce中修复最高优惠券折扣购物百分比

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

如果他们以25%的折扣优惠券代码XYZ25,我如何限制用户获得超过Rs.250折扣.

解决方法:

>您可以根据** RS.250(不含税)的固定购物车折扣和最小花费(4 x 250)= RS.1000设置额外的优惠券FIX250代码.
>然后在下面的脚本的帮助下,如果客户申请您的XYZ25优惠券代码,如果购物车总额达到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上进行了测试.

上一篇:CM: word template web service schema number的限制


下一篇:NEO4J亿级数据全文索引构建优化