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

问题描述 投票:4回答:2

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

如果他们使用优惠码XYZ 25获得25%的折扣,我如何限制用户获得超过Rs.250折扣。

php woocommerce cart coupon discount
2个回答
3
投票

自Woocommerce 3.2或3.3以来,此代码不再起作用

  1. 您可以根据** FIX250(不含税)的固定购物车折扣和RS.250的Minimun支出设置额外的优惠券(4 x 250) = RS.1000代码。
  2. 然后在下面的脚本的帮助下,如果客户申请你的XYZ25优惠券代码,如果购物车总额高达1000卢比,它将取代XYZ25优惠券FIX250同时显示一个明确的通知...

这是代码:

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+上进行了测试。


2
投票

正如@LoicTheAztec指出的那样。这是固定折扣或百分比折扣。

这是代码:

add_filter( 'woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 20, 5 );
function woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $coupon ) {

    $max_discount = 250; // coupon limit
    $coupon_code = 'XYZ25'; // coupon to check.

    if ( ( $coupon->get_code() == $coupon_code ) && ! is_null( $cart_item ) && WC()->cart->subtotal_ex_tax ) {

        $cart_item_qty = is_null( $cart_item ) ? 1 : $cart_item['quantity'];
        if ( wc_prices_include_tax() ) {
            $discount_percent = ( wc_get_price_including_tax( $cart_item['data'] ) * $cart_item_qty ) / WC()->cart->subtotal;
        } else {
            $discount_percent = ( wc_get_price_excluding_tax( $cart_item['data'] ) * $cart_item_qty ) / WC()->cart->subtotal_ex_tax;
        }
        $_discount = ( $max_discount * $discount_percent ) / $cart_item_qty;

        $discount = min( $_discount, $discount );
    }

    return $discount;
}

这样做也是使用“固定购物车折扣”逻辑计算折扣;并使用$max_discount作为计算的优惠券金额。然后两者中较小的一个用完了。

简单地说,让我们以min( A, B )为例。 A是最大折扣,而B是百分比折扣计算的结果。

min(250,100)= 100 min(250,150)= 150 min(250,250)= 250 min(250,300)= 250 min(250,600)= 250

因此总是得到所需的最大折扣。 I have written more code relating to this here.

更新另一种方法,但同样的结果。

add_filter( 'woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 20, 5 );
function woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $coupon ) {

    $fake_code = 'fake_code_abcdefghigklmnopqrstuvwxyz'; // used to ignore this filter
    if ( $coupon->get_code() == $fake_code ) return $discount;

    $max_discount = 250; // coupon limit
    $coupon_code = 'XYZ25'; // coupon to check.

    if ( $coupon->get_code() == $coupon_code )  {

        $_coupon = new WC_Coupon( ); // lets create a fake coupon to test our $max_discount.
        $_coupon->set_props( array(
            'discount_type' => 'fix_cart',
            'amount'        => $max_discount,
            'code'          => $fake_code
        ) );

        $_discount = $_coupon->get_discount_amount( $discounting_amount, $cart_item, $single );

        $discount = min( $_discount, $discount );

    }

    return $discount;
}
© www.soinside.com 2019 - 2024. All rights reserved.