根据WooCommerce中的总额自动应用百分比或固定购物车折扣

时间:2018-01-15 14:40:29

标签: php wordpress woocommerce cart coupon

我正在尝试在我的客户的WooCommerce网站上设置优惠券,以便在购物车总金额低于上限金额或固定金额等于或大于上限金额时适用百分比折扣。

假设购物车总额的上限为200.如果购物车总数低于此上限,则应用10%的折扣。但如果购物车总数为200或更高,那么固定金额20将作为折扣。

例如:

  • 我的购物车总数是190.由于这小于200的上限,折扣金额计算为10%,即应用19
  • 我的购物车总数是210.因为这大于200的上限,所以固定金额为20。

如何根据总数设置我的WooCommerce应用百分比折扣或固定购物车?

2 个答案:

答案 0 :(得分:2)

您可以使用隐藏在 woocommerce_before_calculate_totals 操作挂钩中的自定义功能,您可以在其中定义2个优惠券代码:

  • 百分比折扣优惠券代码(10%)
  • 固定金额优惠券代码($ 20)

代码:

add_action( 'woocommerce_before_calculate_totals', 'auto_add_coupons_total_based', 10, 1 );
function auto_add_coupons_total_based( $cart ) {

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

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // HERE define your coupon code
    $coupon_percent = 'uget10percent'; # <===  <===  <===  <===  <===  <===
    $coupon_fixed = 'uget20off'; # <===  <===  <===  <===  <===  <===  <===

    // Get cart subtotal
    $subtotal = 0;
    foreach($cart->get_cart() as $cart_item ){
        $subtotal += $cart_item['line_subtotal'];
        $subtotal += $cart_item['line_subtotal_tax']; // with taxes
    }

    // Coupon type "percent" (less than 200)
    if( $subtotal < 200 && ! $cart->has_discount( $coupon_percent ) ){
        // If coupon "fixed amount" type is in cart we remove it
        if( $cart->has_discount( $coupon_fixed ) )
            $cart->remove_coupon( $coupon_fixed );

        // Apply the "percent" type coupon code
        $cart->add_discount( $coupon_percent );
    }
    // Coupon type "fixed amount" (Up to 200)
    elseif( $subtotal >= 200 && ! $cart->has_discount( $coupon_fixed ) ) {
        // If coupon "percent" type is in cart we remove it
        if( $cart->has_discount( $coupon_percent ) )
            $cart->remove_coupon( $coupon_percent );

        // Apply the "fixed amount" type coupon code
        $cart->add_discount( $coupon_fixed );
    }
}

代码放在活动子主题(或活动主题)的function.php文件中。

经过测试和工作。

  

如果您想将其应用于小计不含税,则必须对此行进行评论:

$subtotal += $cart_item['line_subtotal_tax']; // with taxes

或者你也可以用这种方式使用负费用(折扣)代替优惠券:

add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_total', 25, 1 );
function discount_based_on_total( $cart ) {

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

    $total = $cart->cart_contents_total;

    // Percentage discount (10%)
    if( $total < 200 )
        $discount = $total * 0.1;
    // Fixed amount discount ($20)
    else
        $discount = 20;

    // Add the discount
    $cart->add_fee( __('discount', 'woocommerce'), -$discount );
}

代码放在活动子主题(或活动主题)的function.php文件中。

经过测试和工作。

答案 1 :(得分:0)

好,所以我终于弄清楚了如何使用此代码,以便仅在添加优惠券时触发。我还添加了一个脚本来通知买方,优惠券折扣额已达到

add_action( 'woocommerce_before_calculate_totals', 'auto_add_coupons_total_based', 10, 1 ); function auto_add_coupons_total_based( $cart ) {

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

// HERE define your coupon code
$coupon_percent = 'xyz20'; # <===  <===  <===  <===  <===  <===
$coupon_fixed = 'fixedamount'; # <===  <===  <===  <===  <===  <===  <===

// Get cart subtotal
$subtotal = 0;
foreach($cart->get_cart() as $cart_item ){
    $subtotal += $cart_item['line_subtotal'];
    $subtotal += $cart_item['line_subtotal_tax']; // with taxes
}

//Set HERE the limit amount
$limit = 40; //without Tax


// Coupon type "percent" (less than 200)
if( $subtotal < 200 && ! $cart->has_discount( $coupon_percent ) ){
    // If coupon "fixed amount" type is in cart we remove it
    if( $cart->has_discount( $coupon_fixed ) )
        $cart->remove_coupon( $coupon_fixed );


}
// Coupon type "fixed amount" (Up to 200)
if( $subtotal >= 200 && $cart->has_discount( $coupon_percent ) ) {
    // If coupon "percent" type is in cart we remove it
    if( $cart->has_discount( $coupon_percent ) )
        $cart->remove_coupon( $coupon_percent );

    // Apply the "fixed amount" type coupon code
    $cart->add_discount( $coupon_fixed );


    // Displaying a custom message
        $message = __( "The total discount limit of $$limit has been reached", "woocommerce" );
        wc_add_notice( $message, 'notice' );



}

}