简体   繁体   English

隐藏基于WooCommerce中产品或类别的特定送货选项

[英]Hide specific shipping options based on products or categories in WooCommerce

My WooCommerce website uses 3 different shipping types. 我的WooCommerce网站使用3种不同的运送类型。

  1. Royal mail signed for (7 days) 签署的皇家邮件(7天)
  2. Next day guaranteed 保证第二天
  3. Recorded delivery 记录交货

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). 某些产品只能使用选项1进行运输。将这种产品添加到购物车后,我创建的运输类别有助于在购物车中显示选项1,但其他两个选项仍然可见(不允许客户选择以下选项)这个产品)。 By using jQuery i was able to hide the other two options because option 1 was the default selection. 通过使用jQuery,我能够隐藏其他两个选项,因为选项1是默认选择。 (So it was easy to hide the other two based on this). (因此,很容易基于此隐藏其他两个)。

The problem i am having is that the 2 hidden options still appear on the checkout page and I'm not sure why. 我遇到的问题是2个隐藏选项仍显示在结帐页面上,我不确定为什么。

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? 有人可以提供一些帮助或替代方法吗?

I've had a look on here and this is the closest I've got to a solution without using jQuery. 我在这里已经看过了, 是我不使用jQuery就最接近解决方案的地方。 The problem with this solution is that I need to manually enter the product id's into the functions.php file. 该解决方案的问题在于,我需要手动将产品ID输入到functions.php文件中。 Which is not ideal when there's over 100 products. 当产品超过100种时,这是不理想的。

Updated There is multiple ways to do it without any need of jQuery: 更新了有多种方法,无需使用jQuery:

1) If you have few products you can use the following code, where you will define: 1)如果产品很少 ,则可以使用以下代码进行定义:

  • An array of product IDs 一组产品ID
  • Your shipping methods instance IDs to be removed 您的运送方式实例ID将被删除

The code: 编码:

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;
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 Tested and work. 经过测试和工作。


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. 2)如果您有一堆产品 ,最好通过产品类别(或产品标签)来定位这些产品……您可以批量分配它。

In the code, you will define: 在代码中,您将定义:

  • A product category (or a product tag which taxonomy is product_tag ) 产品类别(或分类为product_tag的产品标签)
  • Your shipping methods instance IDs to be removed 您的运送方式实例ID将被删除

The code: 编码:

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;
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 Tested and work. 经过测试和工作。

To make it work for Product Tags : 为了使其适用于产品标签
You will simply replace the taxonomy 'product_cat' by 'product_tag' 您只需将分类法'product_cat'替换为'product_tag'


3) If you have a bunch of products , you can also create a shipping class and bulk assign it to your products. 3)如果您有一堆产品 ,还可以创建一个运输类别并将其批量分配给您的产品。 You will need to set the price for it in your related shipping methods… 您需要在相关的送货方式中为其设置价格...

Filter Shipping method based on shipping class in Woocommerce 3 在Woocommerce 3中基于运输类别的筛选运输方法

You should need to refresh the shipping caches: 您应该需要刷新运输缓存:
1) First this code is already saved on your function.php file and empty your cart… 1)首先,此代码已保存在您的function.php文件中,并清空购物车…
2) In Shipping settings, enter in a Shipping Zone and disable a Shipping Method and "save". 2)在运送设置中,输入运送区域并禁用运送方法和“保存”。 Then re-enable that Shipping Method and "save". 然后重新启用该运输方法并“保存”。 You are done. 大功告成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM