【问题标题】:Woocommerce Progressive extra cost based on total number of items in the cartWoocommerce 基于购物车中商品总数的渐进式额外费用
【发布时间】:2018-04-10 08:56:37
【问题描述】:

我正在寻找一些可以根据购物车中的商品总数收取额外费用的代码,例如:

  • 如果购物车中的商品数量 > 6 ===> 额外费用 = 5
  • 如果购物车中的商品数量 > 12 ==> 额外费用 = 10

我已经尝试过this code,但我无法让它适用于我的情况。

如何根据购物车中的商品总数计算额外费用?

【问题讨论】:

  • 请阅读How to Ask,向我们展示您迄今为止尝试/研究的内容。
  • 对不起,兄弟,我是堆栈溢出的新手,这就是为什么。无论如何,我已经收到了正确的答案。

标签: php wordpress woocommerce cart fee


【解决方案1】:

基于(this answer) WooCommerce Cart Quantity Base Discount,您可以根据项目总数添加累进费用:

add_action( 'woocommerce_cart_calculate_fees','woocommerce_cart_extra_cost', 10, 1 );
function woocommerce_cart_extra_cost( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )  return;

    $cart_item_count = $cart->get_cart_contents_count();

    // CONDITIONAL ITEMS QUANTITY FEE AMOUNT
    if( $cart_item_count < 6 )
        $fee = 0;
    elseif( $cart_item_count >= 6 && $cart_item_count < 12 )
        $fee = 5;
    elseif( $cart_item_count >= 12 )
        $fee = 10;

    if( $fee > 0 )
        $cart->add_fee( __( "Extra Cost", "woocommerce" ), $fee, true);
}

代码进入您的活动子主题(或主题)的functions.php 文件或任何插件文件中。

经过测试并且有效。

【讨论】:

  • 非常感谢兄弟,它成功了。在您的帮助下,我现在将它作为插件制作了。
猜你喜欢
  • 2018-01-30
  • 1970-01-01
  • 1970-01-01
  • 2019-01-31
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 2018-02-16
  • 1970-01-01
相关资源
最近更新 更多