根据特定运输类别的购物车商品数量显示或隐藏运输方式

仅当购物车有 4 个或更少的特定运输类别的产品时,我才尝试取消设置两种运输方式。


运送方式: flat_rate:20 和 flat_rate:21


船级:182


这就是我所拥有的:


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;


    // Shipping Class To Find

    $class = 182;


    // Number Of Shipping Class Items In Cart

    $amount = 4;


    // Shipping Methods To Hide

    $method_key_ids = array('flat_rate:20', 'flat_rate:21');


    // Checking In Cart Items

    foreach( $package['contents'] as $item ) {

        // If We Find The Shipping Class and Number of Items

        if( $item['data']->get_shipping_class_id() == $class && count($package['contents']) <= $amount ){

            foreach( $method_key_ids as $method_key_id ){

                unset($rates[$method_key_id]); // Remove Targeted Methods

            }

            break; // Stop The Loop

        }

    }

    return $rates;

}

创建以下逻辑:

1. 如果购物车有 4 件或更少的运输类别 181 的产品,请取消设置以下运输方式:

  • '统一费率:20'

  • '统一费率:21'

2. 如果购物车有 5 个或更多运输类别 181 的产品,请取消设置以下运输方式:

  • 'wf_shipping_ups:07'

  • 'wf_shipping_ups:08'

  • 'wf_shipping_ups:11'

  • 'wf_shipping_ups:54'

  • 'wf_shipping_ups:65'

  • 'wf_shipping_ups:70'

  • 'wf_shipping_ups:74'

  • '免费送货:2'

  • '请求运输报价'

如果我单独使用它们,这两个代码都可以工作。但是当我尝试同时使用两者时出现错误。

我收到以下错误: 无法重新声明 hide_shipping_method_based_on_shipping_class() (之前在 /functions.php:272 中声明)


慕的地8271018
浏览 96回答 2
2回答

梦里花落0921

您应该为每个代码片段使用不同的函数名称,但最好的方法是将所有内容合并到一个唯一的函数中。以下是使其在独特功能中工作的方法(对于来自特定运输方式的物品):当购物车中有 4 件或更少的商品时隐藏一些运输方式当购物车中有 4 件或更少的商品时隐藏一些其他运输方式代码:add_filter( 'woocommerce_package_rates', 'show_hide_shipping_methods_based_on_shipping_class', 10, 2 );function show_hide_shipping_methods_based_on_shipping_class( $rates, $package ) {&nbsp; &nbsp; $targeted_class_ids&nbsp; = array(182); // Shipping Class To Find&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $allowed_max_qty&nbsp; &nbsp; &nbsp;= 4; // Max allowed quantity for the shipping class&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $shipping_rates_ids1 = array( // Shipping Method rates Ids To Hide if more than 4 items are in cart&nbsp; &nbsp; &nbsp; &nbsp; 'wf_shipping_ups:07',&nbsp; &nbsp; &nbsp; &nbsp; 'wf_shipping_ups:08',&nbsp; &nbsp; &nbsp; &nbsp; 'wf_shipping_ups:11',&nbsp; &nbsp; &nbsp; &nbsp; 'wf_shipping_ups:54',&nbsp; &nbsp; &nbsp; &nbsp; 'wf_shipping_ups:65',&nbsp; &nbsp; &nbsp; &nbsp; 'wf_shipping_ups:70',&nbsp; &nbsp; &nbsp; &nbsp; 'wf_shipping_ups:74',&nbsp; &nbsp; &nbsp; &nbsp; 'free_shipping:2',&nbsp; &nbsp; &nbsp; &nbsp; 'request_shipping_quote',&nbsp; &nbsp; );&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $shipping_rates_ids2 = array( // Shipping Method rates Ids to Hide if 4 or less items are in cart&nbsp; &nbsp; &nbsp; &nbsp; 'flat_rate:20',&nbsp; &nbsp; &nbsp; &nbsp; 'flat_rate:20',&nbsp; &nbsp; );&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $related_total_qty&nbsp; &nbsp;= 0; // Initializing&nbsp; &nbsp; // Checking cart items for current package&nbsp; &nbsp; foreach( $package['contents'] as $key => $cart_item ) {&nbsp; &nbsp; &nbsp; &nbsp; if( in_array( $cart_item['data']->get_shipping_class_id(), $targeted_class_ids ) ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $related_total_qty += $cart_item['quantity'];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // When total allowed quantity is more than allowed (for items from defined shipping classes)&nbsp; &nbsp; if ( $related_total_qty > $allowed_max_qty ) {&nbsp; &nbsp; &nbsp; &nbsp; // Hide related defined shipping methods (more than 4 items)&nbsp; &nbsp; &nbsp; &nbsp; foreach( $shipping_rates_ids1 as $shipping_rate_id ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( isset($rates[$shipping_rate_id]) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset($rates[$shipping_rate_id]); // Remove Targeted Methods&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; // Hide related defined shipping methods (4 or less items)&nbsp; &nbsp; &nbsp; &nbsp; foreach( $shipping_rates_ids2 as $shipping_rate_id ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( isset($rates[$shipping_rate_id]) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset($rates[$shipping_rate_id]); // Remove Targeted Methods&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $rates;}代码位于活动子主题(或活动主题)的functions.php 文件中。未经测试它应该有效。刷新运输缓存:此代码已保存在您的functions.php 文件中。在运输区域设置中,禁用/保存任何运输方式,然后启用返回/保存。你已经完成了,你可以测试它。处理物品数量而不是物品累计数量:代替:$related_total_qty&nbsp;+=&nbsp;$cart_item['quantity'];经过$related_total_qty++;

慕容708150

我假设您已经通过依次编写这两个代码片段来组合它们。由于您两次使用相同的函数名称,因此出现错误:无法重新声明......因此,您可以尝试通过重命名第二个片段的函数名称来修复它,如下所示 -add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class_logic_2', 10, 2 );function hide_shipping_method_based_on_shipping_class_logic_2( $rates, $package ) {&nbsp; &nbsp; // other stuffs}
打开App,查看更多内容
随时随地看视频慕课网APP