【问题标题】:Hide specific shipping options based on products or categories in WooCommerce根据 WooCommerce 中的产品或类别隐藏特定的运输选项
【发布时间】:2018-05-04 23:08:59
【问题描述】:

我的 WooCommerce 网站使用 3 种不同的运输类型。

  1. 皇家邮政签收(7 天)
  2. 保证第二天
  3. 发送记录

某些产品只能使用选项 1 发货。当此产品添加到购物车时,我创建的运输类别有助于在购物车中显示选项 1,但其他两个选项仍然可见(不允许客户选择本产品的这些选项)。通过使用 jQuery,我能够隐藏其他两个选项,因为选项 1 是默认选择。 (所以很容易基于此隐藏其他两个)。

我遇到的问题是 2 个隐藏选项仍然出现在结帐页面上,我不知道为什么。

这是我在购物车中选择第一个选项时隐藏其他两个选项的代码:

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

奇怪的是,如果我测试结帐页面上是否存在某个值,我可以触发警报,但是当我使用上面的代码时,它根本不起作用。

有人可以提供一些帮助或替代方法吗?

我看过这里,this 是我在不使用 jQuery 的情况下最接近的解决方案。这个解决方案的问题是我需要手动将产品 ID 输入到 functions.php 文件中。当产品超过 100 种时,这并不理想。

【问题讨论】:

  • fullhousecustoms.com 我使用了你的 php 解决方案,它有效。但是我有太多产品 ID 无法添加到该代码中。我觉得jquery方法实现起来可能会更快。
  • 您必须选择所有选项才能添加到购物车
  • 我刚刚清除了缓存并注意到了。让我回过头来……
  • 我知道。我不确定是什么原因造成的。你能帮忙吗?
  • 嗨,这是一个导致问题的插件。我正在尝试缩小范围。

标签: php wordpress woocommerce shipping custom-taxonomy


【解决方案1】:

更新有多种方法可以做到这一点,而不需要任何 jQuery:

1) 如果您的产品很少,您可以使用以下代码,您将在其中定义:

  • 产品 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 文件中。经过测试和工作。


2)如果您有一堆产品,最好通过产品类别(或产品标签)来定位这些产品......您可以批量分配它。

在代码中,您将定义:

  • 产品类别(或分类为product_tag的产品标签)
  • 要删除的运输方式实例 ID

代码:

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 文件中。经过测试和工作。

为了使其适用于产品标签
您只需将分类法 'product_cat' 替换为 'product_tag'


3) 如果您有一堆产品,您还可以创建运输类别并将其批量分配给您的产品。您需要在相关的运输方式中为其设置价格……

Filter Shipping method based on shipping class in Woocommerce 3

您应该需要刷新运输缓存:
1) 首先,此代码已保存在您的 function.php 文件中并清空您的购物车...
2) 在配送设置中,输入配送区域并禁用配送方式并“保存”。然后重新启用运送方式并“保存”。 你已经完成了。

【讨论】:

  • 谢谢,我设法解决了这个问题(500 错误)。我会检查你的代码,明天再测试。
  • @user2770714 我已经更新并测试了我的代码:它可以工作(即使是免费送货方式)...我已经添加了您正确的送货方式实例 IDS...现在您可以尝试一下。
  • 这很好用。谢谢。有没有一种简单的方法来定位产品变体?
  • @user2770714 如果此答案正在回答您的问题,please don't forget to accept the answer。谢谢……我再次更新了我的答案……我的代码还将通过产品类别(分配给父变量产品)来定位产品变体……对您来说,简单的方法是将产品类别批量分配给您的产品简单或变量(或产品标签)。或者也有运输类(但不太容易)......
猜你喜欢
  • 2020-08-25
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 2018-07-10
  • 2018-02-26
  • 2022-08-24
  • 2018-09-08
相关资源
最近更新 更多