运输选项取决于购物车重量和运输 class

Shipping options depending on cart weight and shipping class

我正在开发一个使用 WooCommerce 的 WordPress 网站。我发现这段代码允许我根据重量设置货运选项。如果在产品上选择了货运 class,客户也想要相同的货运选项,那么它的效果很好。

add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 10, 2 );

function bbloomer_woocommerce_tiered_shipping( $rates, $package ) {

  if ( WC()->cart->cart_contents_weight < 300 ) {

    if ( isset( $rates['fedex:PRIORITY_OVERNIGHT'], $rates['fedex:FEDEX_2_DAY'], $rates['fedex:FEDEX_GROUND'] ) );

  } else {

    if ( isset( $rates['flat_rate:10'] ) ) unset( $rates['fedex:PRIORITY_OVERNIGHT'], $rates['fedex:FEDEX_2_DAY'],$rates['fedex:FEDEX_GROUND'] );
  }

return $rates;
}

然后我找到了另一段代码,可以将运输 class 设置为运输选项。我发现的代码意味着 unset 可用的运输选项,但我想 isset 货运,因为我们 unset 通过购物车重量。但是,我 运行 遇到了一个问题,即运费 class 不能再设置为 isset 而不会导致重大问题。以下是我尝试为货运做的。

add_filter( 'woocommerce_package_rates', 'freight_shipping_class_only', 10, 2 );

function freight_shipping_class_only( $rates, $package ) {

  $shipping_class_target = 193;
  $in_cart = false;
  foreach( WC()->cart->cart_contents as $key => $values ) {
    if( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
      $in_cart = true;
      break;
    } 
  }
  if( $in_cart ) {
    unset( $rates['fedex:PRIORITY_OVERNIGHT'], $rates['fedex:FEDEX_2_DAY'],$rates['fedex:FEDEX_GROUND']  ); // shipping method with ID (to find it, see screenshot below)
    isset( $rates['flat_rate:10'] );
  }
  return $rates;
} 

有没有办法在一个函数中同时设置购物车重量和运费 class?这将是理想的,因为我希望他们都通过 unset 所有 FedEx 运输选项和 isset 货运选项来做完全相同的事情。

如果您需要我说得更清楚或者您有任何提示,请告诉我。

谢谢!

我找到了一个解决方案,它不是我想要的,但它有效。我所做的是将购物车重量更改为超过 300 磅 unset FedEx 运输选项。然后 unset FedEx 运输选项为货运运输 class。这仍然使非货运物品上的货运选项可见,我使用 CSS 将其隐藏。

我仍然愿意寻找更好的方法来做到这一点。因此,如果您有任何建议,请按我的方式发送。

下面是我最后做的。

add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 10, 2 );

function bbloomer_woocommerce_tiered_shipping( $rates, $package ) {

if ( WC()->cart->cart_contents_weight < 300 ) {

    if ( isset( $rates['fedex:PRIORITY_OVERNIGHT'], $rates['fedex:FEDEX_2_DAY'], $rates['fedex:FEDEX_GROUND'] ) );

} else {

    if ( isset( $rates['flat_rate:10'] ) ) unset( $rates['fedex:PRIORITY_OVERNIGHT'], $rates['fedex:FEDEX_2_DAY'],$rates['fedex:FEDEX_GROUND'] );
}

 return $rates;
}

/** Freight Shipping Class Only **/
add_filter( 'woocommerce_package_rates', 'freight_shipping_class_only', 10, 2 );

function freight_shipping_class_only( $rates, $package ) {
    $shipping_class_target = 193; 
    $in_cart = false;
    foreach( WC()->cart->cart_contents as $key => $values ) {
        if( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
           $in_cart = true;
           break;
        } 
     }
     if( $in_cart ) {
        unset( $rates['fedex:PRIORITY_OVERNIGHT'], $rates['fedex:FEDEX_2_DAY'],$rates['fedex:FEDEX_GROUND']  );
     }
     return $rates;
}

由于您使用了 2 次相同的钩子,因此您可以简单地将函数合并为一个。

Now with php [isset()][1] you need to use it in as a condition in an IF statement to check if a variable is set (exist), but it has no effect alone outside your IF statement in your function.

So in your first function the following doesn't has any effect:

if ( isset( $rates['fedex:PRIORITY_OVERNIGHT'], $rates['fedex:FEDEX_2_DAY'], $rates['fedex:FEDEX_GROUND'] ) );

因此您可以试试这个简洁高效的代码:

add_filter( 'woocommerce_package_rates', 'freight_shipping_class_only', 20, 2 );
function freight_shipping_class_only( $rates, $package ) {
    // HERE the targeted shipping class ID
    $targeted_shipping_class = 193;

    // Loop through cart items and checking
    $found = false;
    foreach( $package['contents'] as $item ) {
        if( $item['data']->get_shipping_class_id() == $targeted_shipping_class ){
            $found = true;
            break;
        }
    } 
    
    // The condition
    if ( isset( $rates['flat_rate:10'] ) && ( WC()->cart->get_cart_contents_weight() >= 300 || $found ) ){
        unset( $rates['fedex:PRIORITY_OVERNIGHT'], $rates['fedex:FEDEX_2_DAY'], $rates['fedex:FEDEX_GROUND'] );
    }

    return $rates;
}

代码进入活动子主题(或活动主题)的 function.php 文件。它应该有效。