Hide specific shipping method for specific products in Woocommerce(在 Woocommerce 中隐藏特定产品的特定运输方式)
问题描述
我的 WooCommerce 商店中有一些产品很重(超过 20 公斤),需要通过某种运输方式运输.我想为所有包含重量超过 20 公斤的产品的购物车项目隐藏 'shipping_method_0_flat_rate2'
.
I have a few products in my WooCommerce store that are to heavy (more than 20kg) to be shipped by a certain shipping method. I would like to hide 'shipping_method_0_flat_rate2'
for all cart items which contain a product that is heavier than 20kg.
我尝试调整下面的代码片段,但它并不完整且有效:
I tried to adjust the snippet below, but it is not complete and working:
add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_tag' , 10, 1 );
function check_cart_for_share() {
// specify the product id's you want to hide
$product_ID = array(
'113', // Product name
);
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;
$found = false;
// loop through the array looking for the products. Switch to true if the product is found.
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ($terms as $term) {
if( in_array( $term->term_id, $product_ID ) ) {
$found = true;
break;
}
}
}
return $found;
}
function hide_shipping_based_on_tag( $available_methods ) {
// use the function above to check the cart for the products.
if ( check_cart_for_share() ) {
// remove the method you want
unset( $available_methods['shipping_method_0_flat_rate2'] ); // Replace with the shipping option that you want to remove.
}
// return the available methods without the one you unset.
return $available_methods;
}
感谢任何帮助.
推荐答案
您的代码使事情变得更加复杂,并且您的运输方式 ID 不是很好的方式……试试这个:
You are making things more complicated in your code and your shipping method ID is not the good one… Try this instead:
add_filter( 'woocommerce_package_rates', 'specific_products_shipping_methods', 10, 2 );
function specific_products_shipping_methods( $rates, $package ) {
$product_ids = array( 113 ); // HERE set the product IDs in the array
$method_id = 'flat_rate:2'; // HERE set the shipping method ID
$found = false;
// Loop through cart items Checking for defined product IDs
foreach( $package['contents'] as $cart_item ) {
if ( in_array( $cart_item['product_id'], $product_ids ) ){
$found = true;
break;
}
}
if ( $found )
unset( $rates[$method_id] );
return $rates;
}
代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
您应该需要刷新运输缓存:
1) 首先,此代码已保存在您的 function.php 文件中.
2) 在配送设置中,输入配送区域并禁用配送方式并保存".然后重新启用该送货方式并保存".大功告成.
You should need to refresh the shipping caches:
1) First this code is already saved on your function.php file.
2) In Shipping settings, enter in a Shipping Zone and disable a Shipping Method and "save". Then re-enable that Shipping Method and "save". You are done.
这篇关于在 Woocommerce 中隐藏特定产品的特定运输方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Woocommerce 中隐藏特定产品的特定运输方式
基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01