【问题标题】:Set shipping method per cart item in WooCommerce在 WooCommerce 中为每个购物车项目设置运输方式
【发布时间】:2021-02-17 13:15:21
【问题描述】:

我工作的一家在线商店使用 2 种运输方式:基于尺寸的费率和本地取货。

基于尺寸的费率是默认的送货方式,仅当客户点击点击并取货而不是添加到购物车时才使用本地取货(在内部,产品是在这两种情况下都添加到购物车,但在使用点击取货时,会添加额外的购物车商品数据以表明这一点)。

使用 Click and Pickup 预订所有购物车商品时计算总运费没有问题,因为您可以在运行时使用过滤器 woocommerce_package_rates 过滤运费并更改运输方式,以便它将影响所有购物车项目。

但是,如果购物车包含通过 Click and Pickupadd to cart 添加的商品,则计算总运费会变得更加困难,因为运费必须根据购物车项目更改方法,并且不会影响所有购物车项目。

所需流量:

购物车物品: 3
预订 x 2 通过点击和取货预订(成本:0,本地取货运输方式)
纸张 x 1以正常方式添加到购物车,(成本:默认尺寸费率,默认运输方式)
总运费:本地取货费用 + 默认运费

当前代码:

function cust_shipping_rates( $rates, $package ) {
   if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

   $product_ids_with_local_pickup = array(); // 'click and pickup'
   
   // Loop through line items
   foreach( $package['contents'] as $line_item ) {
       // Get product id
       $product_id = $line_item['product_id'];
   
       if ( $line_item['sofs_cap'] ) { // indication it's reserved with 'Click and pick'
         array_push($product_ids_with_local_pickup, $product_id);
       }
   }
   
   if ( count($product_ids_with_local_pickup) > 0 ) {
       foreach ( $rates as $rate_key => $rate ) {
           if ( in_array( $rate->method_id, array( 'bring_fraktguiden:5800' ) ) ) { // default shipping method
               // how can i exclude the reserved products from being affected by this shipping method?
               $rates[$rate_key]->set_cost(/* cost */); // changing this affects ALL items, not just the specific ones
           }
       }
   }
   
   return $rates;
}

add_filter('woocommmerce_package_rates', 'cust_shipping_rates', 999, 2);

【问题讨论】:

  • 您需要使用 woocommerce_cart_shipping_packages 挂钩将购物车物品拆分为运输包裹,请参阅 this related threads 并尝试为自己做点什么。
  • 非常感谢!这就是我一直在寻找的过滤器类型,它会被运费迭代。
  • 欢迎您!这并不容易,但它是正确的方法......如果您的代码有任何问题,请在新问题中提供它,添加一些解释和细节......
  • 过滤器完成了它的工作。我只需要一种方法来排除一些购物车商品,使其不计入运费。:)
  • 您应该使用适合您的代码回答您自己的问题(如果需要,可以进行一些解释),因为它可以帮助其他读者。

标签: wordpress woocommerce hook-woocommerce


【解决方案1】:

此答案并未解释如何为每种产品添加不同的运输方式,this plugin 可以提供帮助 - 但是,如果您需要将某些购物车商品排除在运费中,此答案可能会对您有所帮助。

过滤器woocommerce_cart_shipping_packages 返回一个包裹,其中包含过滤器woocommerce_package_rates 中的运输方式迭代以计算运输成本的购物车物品。

通过过滤返回的包裹中的购物车,只需删除您不希望运送的购物车物品,如下所示:

// define the woocommerce_cart_shipping_packages callback 
function filter_woocommerce_cart_shipping_packages( $package ) { 
    $new_cart = $package[0]['contents'];

    foreach($cart as $cart_item) {
       // check for desired shipping method
       // cart items not checking for this property, will not be accounted for shipping costs
       if($cart_item['custom_extra_cart_item_data']) {
          array_push($new_cart, $cart_item); 
       } 
    }

    if(!empty($new_cart)) $package[0]['contents'] = $new_cart;

    return $package; 
}; 
         
// add the filter 
add_filter( 'woocommerce_cart_shipping_packages', 'filter_woocommerce_cart_shipping_packages', 10, 1 ); 

这是我的案例的代码(键名sofs_cap 是属于唯一购物车项目的额外购物车项目数据,表示它是用click and pick 添加的,你的键名可以是任何你喜欢的)。

注意:就我而言,我需要至少一种运输方式才能 查看。因此,当购物车仅包含不符合条件的物品时 运输 - 我只是将运输方式设置为“本地取货”(其中 也很合适)。我还排除了这种运输方式 购物车有两种方式添加的物品。

function custom_shipping_rates($rates, $pkg) {
   if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

   $has_cap = false; // added with click and pikcup
   $has_def = false; // added default way
   
   // Loop through line items
   foreach( $pkg['contents'] as $line_item ) {
      if($line_item['sofs_cap']) $has_cap = true; else $has_def = true;
   }

   $new_rates = array();

   if($has_cap && $has_def || $has_def) { // cart with items added both ways returns all shipping methods except local pickup
      foreach ($rates as $rate_key => $rate ) {
         if($rate_key !== 'local_pickup:8') { // id can be different for your shipping method
            $new_rates[$rate_key] = $rate;
         }
     }

     return $new_rates;
   } else if ($has_cap) {  // cart with only items added with Click and pickup returns only shipping method local pickup
      $new_rates['local_pickup:8'] = $rates['local_pickup:8'];
      return $new_rates;
   } 

   return $rates;
}
add_filter('woocommerce_package_rates', 'custom_shipping_rates', 999, 2);

function custom_filtered_cart_shipping_packages( $shipping_package ) {
    $new_cart = array(); // only contains cart items that are not added with 'Click and pickup'
    $has_cap_added_items = false;
    
    foreach($shipping_package[0]['contents'] as $cart_item) {
        if(!$cart_item['sofs_cap']) {
            array_push($new_cart, $cart_item); // add only default added products to the package
        } else {
            $has_cap_added_items = true;
        }
    }

    $shipping_package[0]['contents'] = $new_cart;
      
    return $shipping_package; 
}
      
add_filter( 'woocommerce_cart_shipping_packages', 'custom_filtered_cart_shipping_packages'); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多