适用于购物车20%的折扣限制为最高金额

时间:2021-07-21 22:59:55

I Would like in woocommerce cart, that customers will get a 20% discount, but I want to limit the discounted amount to $500 for example.

我想在woocommerce购物车中,客户将获得20%的折扣,但我想将折扣金额限制在500美元。

Is that possible in WooCommerce?

这可能在WooCommerce?

Thanks.

谢谢。

1 个解决方案

#1


4  

This can be done with ease using woocommerce_cart_calculate_fees hook and the WC_cart method add_fee(). Then if you use a negative fee, it becomes then a DISCOUNT.

这可以通过使用woocommerce_cart_calculate_fees钩子和WC_cart方法add_fee()轻松完成。然后,如果你使用负面费用,它就变成了折扣。

In this function the discount is calculate from the cart subtotal excluding taxes (and you can easily change it to total including taxes).

在此功能中,折扣是根据不包含税的购物车小计计算的(您可以轻松将其更改为包含税的总额)。

Here is the code:

这是代码:

add_action( 'woocommerce_cart_calculate_fees', 'custom_limited_discount', 10, 1 );
function custom_limited_discount($cart_object) {

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

    // Here 20 % of discount
    $discount_percent = 0.2;

    // Here the max discounted amount
    $max_discount = 500;

    // Here are some different cart totals
    $cart_subtotal_excl_tax = WC()->cart->subtotal_ex_tax;
    $cart_subtotal = WC()->cart->subtotal;
    $cart_total = WC()->cart->total;

    $discount = 0;

    // CALCULATION with subtotal excluding taxes
    $calculation = $cart_subtotal_excl_tax * $discount_percent;

    // Limiting the discount to $max_discount
    if ( $calculation > $max_discount ) {
        $discount -= $max_discount;
    } else {
        $discount -= $calculation;
    }

    $discount_text_output = __( 'Discount (20 %)', 'woocommerce' );

    // Adding the discount
    $cart_object->add_fee( $discount_text_output, $discount, false );
    // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
}

This code is tested and is fully functional.

此代码已经过测试并且功能齐全。

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.

代码位于活动子主题(或主题)的function.php文件中。或者也可以在任何插件的php文件中。

Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false).

注意:add_fee()方法中的最后一个参数与将税或不应用于折扣(true或false)有关。

#1


4  

This can be done with ease using woocommerce_cart_calculate_fees hook and the WC_cart method add_fee(). Then if you use a negative fee, it becomes then a DISCOUNT.

这可以通过使用woocommerce_cart_calculate_fees钩子和WC_cart方法add_fee()轻松完成。然后,如果你使用负面费用,它就变成了折扣。

In this function the discount is calculate from the cart subtotal excluding taxes (and you can easily change it to total including taxes).

在此功能中,折扣是根据不包含税的购物车小计计算的(您可以轻松将其更改为包含税的总额)。

Here is the code:

这是代码:

add_action( 'woocommerce_cart_calculate_fees', 'custom_limited_discount', 10, 1 );
function custom_limited_discount($cart_object) {

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

    // Here 20 % of discount
    $discount_percent = 0.2;

    // Here the max discounted amount
    $max_discount = 500;

    // Here are some different cart totals
    $cart_subtotal_excl_tax = WC()->cart->subtotal_ex_tax;
    $cart_subtotal = WC()->cart->subtotal;
    $cart_total = WC()->cart->total;

    $discount = 0;

    // CALCULATION with subtotal excluding taxes
    $calculation = $cart_subtotal_excl_tax * $discount_percent;

    // Limiting the discount to $max_discount
    if ( $calculation > $max_discount ) {
        $discount -= $max_discount;
    } else {
        $discount -= $calculation;
    }

    $discount_text_output = __( 'Discount (20 %)', 'woocommerce' );

    // Adding the discount
    $cart_object->add_fee( $discount_text_output, $discount, false );
    // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
}

This code is tested and is fully functional.

此代码已经过测试并且功能齐全。

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.

代码位于活动子主题(或主题)的function.php文件中。或者也可以在任何插件的php文件中。

Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false).

注意:add_fee()方法中的最后一个参数与将税或不应用于折扣(true或false)有关。