WooCommerce购物车数量基础折扣

时间:2017-07-01 02:35:22

标签: php wordpress woocommerce cart discount

在WooCommerce中,如何根据购物车中的商品总数设置购物车折扣?

例如:

  • 1至4项 - 无折扣
  • 5到10项 - 5%
  • 11至15项 - 10%
  • 16至20项 - 15%
  • 21至25项 - 20%
  • 26至30项 - 25%

我搜索互联网但未找到任何可用的解决方案或插件。

感谢。

1 个答案:

答案 0 :(得分:5)

您可以使用负车费来获得折扣。然后你会添加你的条件和&通过这种方式计算加入 woocommerce_cart_calculate_fees 动作挂钩的acustom函数:

## Tested and works on WooCommerce 2.6.x and 3.0+
add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );
function wc_cart_quantity_discount( $cart_object ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    ## -------------- DEFINIG VARIABLES ------------- ##
    $discount = 0;
    $cart_item_count = $cart_object->get_cart_contents_count();
    $cart_total_excl_tax = $cart_object->subtotal_ex_tax;

    ## ----------- CONDITIONAL PERCENTAGE ----------- ##
    if( $cart_item_count <= 4 )
        $percent = 0;
    elseif( $cart_item_count >= 5 && $cart_item_count <= 10 )
        $percent = 5;
    elseif( $cart_item_count > 10 && $cart_item_count <= 15 )
        $percent = 10;
    elseif( $cart_item_count > 15 && $cart_item_count <= 20 )
        $percent = 15;
    elseif( $cart_item_count > 20 && $cart_item_count <= 25 )
        $percent = 20;
    elseif( $cart_item_count > 25 )
        $percent = 25;


    ## ------------------ CALCULATION ---------------- ##
    $discount -= ($cart_total_excl_tax / 100) * $percent;

    ## ----  APPLYING CALCULATED DISCOUNT TAXABLE ---- ##
    if( $percent > 0 )
        $cart_object->add_fee( __( "Quantity discount $percent%", "woocommerce" ), $discount, true);
}

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

测试并使用WooCommerce 2.6.x和3.0 +