运输选项取决于购物车的重量和运输类别 [英] Shipping options depending on cart weight and shipping class

查看:38
本文介绍了运输选项取决于购物车的重量和运输类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WooCommerce的WordPress网站上工作.我找到了这段代码,可以根据重量设置货运选项.如果在产品上选择了货运类别,则效果很好,直到客户也希望使用相同的货运选项.

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

然后,我发现了另一段代码,可以将运输类别设置为运输选项.我发现的代码是指unset可用的运输选项的意思,但是我想isset货运,因为我们通过购物车重量unset进行运输.但是,我遇到了一个问题,即在不再引起重大问题的情况下,不能再将货运类别设置为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;
} 

是否可以在一个功能中同时设置购物车重量和货运等级?这将是理想的,因为我希望他们都通过unset所有FedEx运送选项和isset货运选项来做完全相同的事情.

让我知道您是否需要我更清楚或有任何提示.

谢谢!

解决方案

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

现在使用php [isset()][1],您需要在IF语句中使用它作为条件来检查变量是否已设置(存在),但仅在函数中的IF语句之外无效. /p>

因此,在您的第一个函数中,以下内容没有任何作用:

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文件中.应该可以.

I am working on a WordPress site that uses WooCommerce. I found this piece of code that allows me to set a freight shipping option based on weight. It worked great until the client also wanted the same freight shipping option if a freight shipping class is selected on a product.

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

Then I found another piece of code that could set a shipping class to a shipping option. The code I found was mean to unset shipping options that are available but I want to isset the freight shipping since we unset it via the cart weight. However, I am running into an issue where the freight shipping class can no longer be set to isset without causing major issues. Below is what I tried to do for the freight shipping.

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

Is there a way to set both the cart weight and the freight shipping class in one function? It would be ideal since I want them both to do the exact same thing by unset all FedEx shipping option and isset the freight shipping option.

Let me know if you need me to be clearer or if you have any tips.

Thanks!

解决方案

As you are using 2 times the same hook, you can simply merge your functions in one.

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'] ) );

So you can try this compact and efficient code instead:

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

Code goes in function.php file of the active child theme (or active theme). It should work.

这篇关于运输选项取决于购物车的重量和运输类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆