Php 隐藏商业中特定产品的特定装运方式
我在我的WooCommerce商店里有一些产品很重(超过20公斤),需要通过某种运输方式运输。我想隐藏Php 隐藏商业中特定产品的特定装运方式,php,wordpress,woocommerce,cart,shipping,Php,Wordpress,Woocommerce,Cart,Shipping,我在我的WooCommerce商店里有一些产品很重(超过20公斤),需要通过某种运输方式运输。我想隐藏“装运方法\u 0 \u统一费率2”,用于所有包含超过20公斤产品的购物车项目 我试图调整下面的代码段,但它不完整且无法正常工作: add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_tag' , 10, 1 ); function check_cart_for_share() { // specify
“装运方法\u 0 \u统一费率2”
,用于所有包含超过20公斤产品的购物车项目
我试图调整下面的代码段,但它不完整且无法正常工作:
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不是一个好的ID…请尝试以下方法:
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文件。测试和工作
您应该需要刷新发货缓存:1) 首先,此代码已保存在function.php文件中
2) 在配送设置中,输入配送区域并禁用配送方法和“保存”。然后重新启用该装运方法和“保存”你完成了。
您好,这很好,但是我需要向代码中添加什么来隐藏2个装运方法而不是1个?我有两个装运方法id,当特定产品id在阵列中时需要隐藏。我已经做了这些更改,它工作得非常好。您将如何修改代码以适应100种产品?请看一下我不久前发布的这个问题:()方法ID中的“2”是什么<代码>$method_id='flat_rate:2'@Mohandes方法ID中的这个“2”是实例ID…每个方法ID由一个方法类型slug+“:”+实例ID组成(当您在装运区注册新装运方法时生成…