【问题标题】:Hide shipping methods for some shipping classes in WooCommerce在 WooCommerce 中隐藏某些运输类别的运输方式
【发布时间】:2021-02-04 05:39:36
【问题描述】:

基本上,当购物车中只有特定的运输类别(包括 ID 40 到 46)时,我试图隐藏统一费率运输方式“flat_rate:8”。但是,当购物车中有任何运输类别为 47、48、49 的产品时,我想隐藏除此“flat_rate:8”之外的所有其他运输方式。 以下所有详细信息。

我有 9 种运输类别(从 ID 40 到 46):XXS、XS、S、... L、XL、XXL、XXXL 和 5 种运输方式(flate_rate3、local_pickup、flat_rate6、flat_rate7、flat_rate8)。

当我在购物车中时:

  • 混合产品,其中至少有一个是 XL、XXL 或 XXXL
  • 仅限 XL、XXL 或 XXXL 产品(一件或多件)

我只希望提供 2 种运输方式(local_pickup 和 flate_rate8)。基本上,如果购物车中有任何 >= XL,我只希望这两种方法出现。 我能够使用这段代码来完成这 3 段代码:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

// HERE define your shipping class to find. 47 = shipping class XL
$class = 47;

// HERE define the shipping methods you want to hide
$method_key_ids = array('flat_rate:3', 'flat_rate:6', 'flat_rate:7');

// Checking in cart items
foreach( $package['contents'] as $item ) {
    // If we find the shipping class
    if( $item['data']->get_shipping_class_id() == $class ){
        foreach( $method_key_ids as $method_key_id ){
            unset($rates[$method_key_id]); // Remove the targeted methods
        }
        break; // Stop the loop
    }
}
return $rates;
}


add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

// HERE define your shipping class to find. 48 = shipping class XXL
$class = 48;

// HERE define the shipping methods you want to hide
$method_key_ids = array('flat_rate:3', 'flat_rate:6', 'flat_rate:7');

// Checking in cart items
foreach( $package['contents'] as $item ) {
    // If we find the shipping class
    if( $item['data']->get_shipping_class_id() == $class ){
        foreach( $method_key_ids as $method_key_id ){
            unset($rates[$method_key_id]); // Remove the targeted methods
        }
        break; // Stop the loop
    }
}
return $rates;
}

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

// HERE define your shipping class to find. 49 = shipping class XXXL
$class = 49;

// HERE define the shipping methods you want to hide
$method_key_ids = array('flat_rate:3', 'flat_rate:6', 'flat_rate:7');

// Checking in cart items
foreach( $package['contents'] as $item ) {
    // If we find the shipping class
    if( $item['data']->get_shipping_class_id() == $class ){
        foreach( $method_key_ids as $method_key_id ){
            unset($rates[$method_key_id]); // Remove the targeted methods
        }
        break; // Stop the loop
    }
}
return $rates;
}

这很好,现在只显示 2 种运输方式(local_pickup 和 flat_rate8)。

那么,我想做的是:

当我在购物车中时:

  • 仅限运输等级为 XXXS (id=40)、XXS (41)、XS (43)、S (44)、M (45)、L (46) 的商品

我想删除 flat_rate8 并保留所有其他运输方式。 使用当前代码和设置,现在,当我的购物车中有 =

我一直在尝试复制上面显示的代码以仅在购物车中有“小”产品时隐藏此 flat_rate8,但显然它不起作用,因为当我有一个混合购物车(例如 XXXL 和 S)时将从选项中删除 flat_rate8。

我试过了,除了上面显示的那段代码添加这个:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_classd', 10, 2 );
function hide_shipping_method_based_on_shipping_classd( $rates, $package )
{
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    foreach( $package['contents'] as $package_item ){ // Look at the shipping class of each item in package

        $product_id = $package_item['product_id']; // Grab product_id
        $_product   = wc_get_product( $product_id ); // Get product info using that id

        if( $_product->get_shipping_class_id() != 47 ){ // If we DON'T find this shipping class ID (XL)
            unset($rates['flat_rate:8']); // Then remove this shipping method
            break; // Stop the loop, since we've already removed the shipping method from this package
        }
    }
    return $rates;
}

但它不起作用:

  • 当我在购物车中有 XL 产品 (id=47) 和 S 产品时,flat_rate8 不可见(应该是,它是大小产品的混合)
  • 当我只有 XL 产品时 flat_rate8 在这里(很好)
  • 当我有小型产品(XS、S、M 等)时,flat_rate8 不在这里(很好)

我一直在寻找许多主题,其中包括:

但我无法找到解决问题的方法。

【问题讨论】:

    标签: php wordpress woocommerce cart shipping-method


    【解决方案1】:

    试试下面的代码,所有的东西都合并到一个独特的挂钩函数中:

    add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
    function hide_shipping_method_based_on_shipping_class( $rates, $package )
    {   
        // HERE define your shipping class to find
        $classes_group1 = array(47, 48, 49);
        $classes_group2 = array(40, 41, 43, 44, 45, 46);
        
        // HERE define the shipping methods you want to hide
        $method_key_ids1 = array('flat_rate:3', 'flat_rate:6', 'flat_rate:7');
        $method_key_ids2 = array('flat_rate:8');
        
        $found_group1 = $found_group2 = false; // Initializing
        
        // Checking in cart items
        foreach( $package['contents'] as $item ) {
            $shipping_class = $item['data']->get_shipping_class_id();
            
            // Shipping Classes group 1
            if( in_array( $shipping_class, $classes_group1 ) ){
                foreach( $method_key_ids1 as $method_key_id ){
                    // Remove the targeted methods 1
                    unset($rates[$method_key_id]); 
                    $found_group1 = true; // Flag
                }
            }
            // Shipping Classes group 2
            if( in_array( $shipping_class, $classes_group2 ) ){
                foreach( $method_key_ids2 as $method_key_id ){
                    $found_group2 = true; // Flag
                }
            }
            
            // If Shipping Classes group 2 alone in cart
            if( ! $found_group1 && $found_group2 ){
                foreach( $method_key_ids2 as $method_key_id ){
                    // Remove the targeted methods 2
                    unset($rates[$method_key_id]);
                }
            }
        }
        return $rates;
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

    刷新送货方式

    • 清空购物车。
    • 如果需要,请转到配送区域设置,然后禁用/保存并重新启用/保存来自配送区域的任何配送方式。

    【讨论】:

    • 太好了,非常感谢!我对其进行了测试,它运行良好,这段代码对我来说非常有意义,干杯。但是,我注意到一个小故障,当您先添加一个小产品 ($classes_group2),然后添加一个大产品 ($classes_group1) 时,购物车中不再有送货选项。但是,如果您先将大的添加到购物车中,然后再添加小的,则它可以正常工作。我猜它正在经历循环,取消设置大产品的交付方式,然后忘记再次激活它?谢谢...
    • 可以设置送货方式吗?喜欢 unset 的反义词吗?
    猜你喜欢
    • 2014-07-05
    • 2019-10-17
    • 2018-07-10
    • 2019-09-04
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多