隐藏基于WooCommerce中产品或类别的特定送货选项
我的WooCommerce网站使用3种不同的送货方式。
My WooCommerce website uses 3 different shipping types.
- 签署了7天的皇家邮件
- 保证第二天
- 已记录交货
某些产品只能使用选件进行运输1.将本产品添加到购物车后,我创建的运输类别有助于在购物车中显示选项1,但其他两个选项仍然可见(不允许客户为此产品选择这些选项)。通过使用jQuery,我可以隐藏其他两个选项,因为选项1是默认选择。 (因此,很容易基于此隐藏其他两个)。
Some products can only be shipped using option 1. When this product is added to the cart, a shipping class i created helps to show option 1 in the cart but the other two options are still visible (customers are not allowed to choose these options for this product). By using jQuery i was able to hide the other two options because option 1 was the default selection. (So it was easy to hide the other two based on this).
我遇到的问题是2个隐藏选项仍显示在结帐页面上,我我不确定为什么。
The problem i am having is that the 2 hidden options still appear on the checkout page and I'm not sure why.
这是在购物车中选择第一个选项时用来隐藏其他两个选项的代码:
This is the code I am using to hide the other two options when the first option is selected in the cart:
jQuery(document.body).ready(function() {
if(jQuery('#shipping_method_0_ced_pps').val() == 'ced_pps')
jQuery('ul#shipping_method li:nth-child(2)').hide();
jQuery('ul#shipping_method li:nth-child(3)').hide();
});
奇怪的是,如果我测试看看结帐页面上是否存在值,我可以触发警报,但是当我使用上面的代码时,它根本无法工作。
The strange thing is if I test to see if a value exists on the checkout page I can fire an alert but when I use the above code it does not work at all.
有人可以提供帮助或替代方法吗?
Can someone provide some help or an alternative method?
我在这里和这是我在不使用jQuery的情况下最接近解决方案的地方。该解决方案的问题在于,我需要手动将产品ID输入到functions.php文件中。当产品超过100种时,这是不理想的。
I've had a look on here and this is the closest I've got to a solution without using jQuery. The problem with this solution is that I need to manually enter the product id's into the functions.php file. Which is not ideal when there's over 100 products.
已更新不需要jQuery:
Updated There is multiple ways to do it without any need of jQuery:
1)如果产品很少,则可以使用以下代码进行定义:
1) If you have few products you can use the following code, where you will define:
- 一系列产品ID
- 要删除的运输方式实例ID
代码:
add_filter( 'woocommerce_package_rates', 'specific_products_shipping_methods', 10, 2 );
function specific_products_shipping_methods( $rates, $package ) {
// HERE set the product IDs in the array
$product_ids = array( 113, 115, 126 );
// HERE set the shipping methods to be removed (like "fat_rate:5")
$method_instances_ids = array('2846', '2851');
$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 ) return $rates; // If not found we exit
// Loop through your active shipping methods
foreach( $rates as $rate_id => $rate ) {
// Remove all other shipping methods other than your defined shipping method
if ( in_array( $rate_id, $method_instances_ids ) ){
unset( $rates[$rate_id] );
}
}
return $rates;
}
代码进入活动子主题(或活动主题)的function.php文件)。经过测试和工作。
Code goes in function.php file of your active child theme (or active theme). Tested and work.
2)如果您有一堆产品,最好通过产品类别(或产品标签)定位这些产品。
2) If you have a bunch of products, is better to target those products through a product category (or a product Tag)… You can bulk assign it.
在代码中,您将定义:
In the code, you will define:
- 产品类别(或分类为
product_tag
的产品标签) - 要删除的运输方式实例ID
- A product category (or a product tag which taxonomy is
product_tag
) - Your shipping methods instance IDs to be removed
代码:
add_filter( 'woocommerce_package_rates', 'specific_products_shipping_methods', 10, 2 );
function specific_products_shipping_methods( $rates, $package ) {
// HERE set the product category in the array (ID, slug or name)
$terms = array( 'my-category' );
$taxonomy = 'product_cat';
// HERE set the shipping methods to be removed (like "fat_rate:5")
$method_instances_ids = array('2846', '2851');
$found = false;
// Loop through cart items checking for defined product IDs
foreach( $package['contents'] as $cart_item ) {
if ( has_term( $terms, $taxonomy, $cart_item['product_id'] ) ){
$found = true;
break;
}
}
if ( ! $found ) return $rates; // If not found we exit
// Loop through your active shipping methods
foreach( $rates as $rate_id => $rate ) {
// Remove all other shipping methods other than your defined shipping method
if ( in_array( $rate_id, $method_instances_ids ) ){
unset( $rates[$rate_id] );
}
}
return $rates;
}
代码进入活动子主题(或活动主题)的function.php文件)。经过测试并可以正常工作。
Code goes in function.php file of your active child theme (or active theme). Tested and work.
要使其用于产品标签:
只需将分类法'product_cat'
替换为'product_tag'
3)如果您有一堆产品,则还可以创建货运类别并将其批量分配给您的产品。您将需要在相关的运输方式中为其设置价格...
3) If you have a bunch of products, you can also create a shipping class and bulk assign it to your products. You will need to set the price for it in your related shipping methods…
您需要刷新运输缓存:
1)首先,此代码已保存在function.php文件中,并清空购物车…
2)在运输设置中,输入运输区域并禁用运输方式和保存。然后重新启用该发货方式并保存。 您已完成。
You should need to refresh the shipping caches:
1) First this code is already saved on your function.php file and empty your cart…
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.