【问题标题】:WooCommerce: Exclude product from threshold count for free shippingWooCommerce:从免费送货的阈值计数中排除产品
【发布时间】:2020-07-11 16:25:51
【问题描述】:

我已为英国的某人设置了 3 件商品的免费送货门槛。每次有人添加特定类别的产品时,我都会自动将产品添加到他们的购物车中,这是免费礼物。

我的问题是,我试图从阈值计数中排除此免费礼物,因为目前正在计算此免费礼物,并且人们在购物车中没有 3 件实际收费物品的情况下获得免费送货。

我不确定如何从计数中排除产品 id 29,因此我们将不胜感激。

add_action( 'woocommerce_before_cart', 'ds_free_shipping_cart_notice' );
  
function ds_free_shipping_cart_notice() {
    $threshold = 3;
    $current = WC()->cart->get_cart_contents_count();
    $billing_country = WC()->customer->get_billing_country();
    if ( $current < $threshold && WC()->customer->get_billing_country() == 'GB' ) {
        wc_print_notice( 'Nearly there! Order ' . ( $threshold - $current ) . ' more and shipping is on us', 'notice' );
    }
    
}

【问题讨论】:

    标签: php woocommerce


    【解决方案1】:

    我没有设置 woocommerce 来进行测试,但这是您需要的逻辑类型:

    $cart_items = WC()->cart->get_cart();
    $current = WC()->cart->get_cart_contents_count();
    foreach($cart_items as $item => $values) { 
      if ($values['product_id'] == 29) {
       --$current;
      }
    } 
    

    This answer 也应该有帮助。

    不过,一般来说,这是一种非常脆弱的方法,因为您每次提供新礼物时都需要更改硬编码的产品 ID。最好为您的产品添加一个名为“免费礼物”或类似名称的自定义字段,根据需要为作为免费礼物提供的物品设置该值,然后设置代码以从免费送货中排除任何这些物品.

    【讨论】:

    • 谢谢,我添加了这个,虽然产品 id 29 没有被计算在内,但是当我更改购物车中已有商品的数量时,如果没有你的位,它不会被计算在内。任何想法?至于免费产品,只会有这个,因为它是针对特定类别的,所以没关系。
    • 好的,如果发生这种情况,最好颠倒逻辑。请看看我编辑的答案。
    • 你是绝对的明星!!太感谢了。它按预期工作,我删除了您答案中的 $current ,因为它已经是原始代码的一部分。再次感谢您。
    • 如果您不介意,请点击旁边的大勾号将我的回答标记为已接受
    • 没问题,乐于助人!
    猜你喜欢
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多