php - WooCommerce:从免费送货的阈值计数中排除产品
问题描述
我已经为英国的某人设置了 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' );
}
}
解决方案
我没有设置 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;
}
}
这个答案也应该有所帮助。
但总的来说,这是一种非常脆弱的方法,因为每次提供新礼物时都需要更改硬编码的产品 ID。最好为您的产品添加一个名为“免费礼物”或类似名称的自定义字段,根据需要为作为免费礼物提供的物品设置该值,然后设置代码以从免费送货中排除任何这些物品.
推荐阅读
- vb.net - 将光标/插入符号发送到 RichTextBox 中的下一段
- html - 为什么文本装饰在这种情况下不起作用?
- testing - 有没有办法在 Robot Framework 的 If 条件中设置变量?
- javascript - JavaScript:通过在标准 JS array.sort 的自定义比较函数中暂停来等待来自用户(比较 2 张图片)的 onclick 事件?
- java - 在 liquibasev 中禁用删除表
- c++ - 通过引用传递 std::istream 和 std::ostream 以在函数内使用
- html - 我在反应中的 CSS 类正在影响不同的页面
- javascript - 承诺 {
尽管使用了 async/await,但仍返回 - javascript - 反应数组中的元素。警告:React.createElement:类型无效
- laravel - npm 运行成功但终端进程停止(系统限制)