php - 根据 WooCommerce 中的产品或类别隐藏特定运输选项

标签 php wordpress woocommerce custom-taxonomy

我的 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 种时,这并不理想。

最佳答案

更新有多种方法可以做到这一点,而不需要任何 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 文件中。经过测试并可以工作。

To make it work for Product Tags:
You will simply replace the taxonomy 'product_cat' by 'product_tag'


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

Filter Shipping method based on shipping class in 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…
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.

关于php - 根据 WooCommerce 中的产品或类别隐藏特定运输选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50184151/

相关文章:

php - 带有下拉类别的搜索栏。搜索结果未从数据库中提取并显示

php - 通过 PHP 以最快的速度将数据从 MSSQL 传输到 MySQL

PHP 依赖注入(inject) - fatal error

php - fatal error register_globals已禁用

php - 菜单栏居中后下拉菜单错位

wordpress - Woocommerce REST API 扩展订单响应

mysql - Wordpress MySQL 结果资源无效

php - Hooks around 添​​加到 Woocommerce 的购物车

php - 设置和检索 WordPress 的 session 值

wordpress - Woocommerce 购物车显示重复的购物车总计