微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Woocommerce 中以编程方式自定义运费

如何解决在 Woocommerce 中以编程方式自定义运费

我想根据 woocommerce 的购物车数量计算自定义运费,我的要求如下,

购物车数量

  1. 0-10 => 4.99 英镑(shipping_rate_id = flat_rate:12),
  2. 10-20 => 3.99 英镑(shipping_rate_id = flat_rate:13),
  3. 20-30 => 2.99 英镑(shipping_rate_id = flat_rate:14),
  4. 30-40 => 1.99 英镑(shipping_rate_id = flat_rate:15),
  5. 超过 40 => 免费送货(shipping_rate_id = flat_rate:17)

注意:我们不计算烟草类别的运费。如果有人购买烟草物品,烟草 商品价格不用于运费计算。为了实现这一点,所有的烟草项目 添加到免费送货类别(id = 150)。

设置自定义查询来完成上述要求,但它没有按我预期的那样工作

add_filter('woocommerce_package_rates','custom_shipping_rates_based_on_shipping_class',11,2);

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

  // HERE define your shipping class to find
  $class = array(150);

  // HERE define the shipping method to change rates for
  $shipping_rate_ids = array('flat_rate:12','flat_rate:13','flat_rate:14','flat_rate:15');

  // Initialising
  $item_price = $item_qty = $item_total = 0;

  // Loop through cart items
  foreach($package['contents'] as $cart_item_key => $cart_item) {
    $item_shipping_class_id = $cart_item['data'] - > get_shipping_class_id();

    if (!in_array($item_shipping_class_id,$class)) {
      $item_price += $cart_item['data'] - > get_price(); // Sum line item prices that have target shipping class
      $item_qty += $cart_item['quantity']; // Sum line item prices that have target shipping class
      $item_total = $item_price * $item_qty;
    }
  }

  // Loop through shipping rates
  foreach($rates as $rate_key => $rate) {
    if (in_array($rate_key,$shipping_rate_ids)) {
      if ($item_total > 0 && $item_total < 10) {
        $rates[$rate_key] - > cost = 4.99;
        unset($rates['flat_rate:13']);
        unset($rates['flat_rate:14']);
        unset($rates['flat_rate:15']);
        unset($rates['flat_rate:17']);
      }
      elseif($item_total > 10.01 && $item_total < 20) {
        $rates[$rate_key] - > cost = 3.99;
        unset($rates['flat_rate:12']);
        unset($rates['flat_rate:14']);
        unset($rates['flat_rate:15']);
        unset($rates['flat_rate:17']);
      }
      elseif($item_total > 20.01 && $item_total < 30) {
        $rates[$rate_key] - > cost = 2.99;
        unset($rates['flat_rate:12']);
        unset($rates['flat_rate:13']);
        unset($rates['flat_rate:15']);
        unset($rates['flat_rate:17']);
      }
      elseif($item_total > 30.01 && $item_total < 40) {
        $rates[$rate_key] - > cost = 1.99;
        unset($rates['flat_rate:12']);
        unset($rates['flat_rate:13']);
        unset($rates['flat_rate:14']);
        unset($rates['flat_rate:17']);
      } else {
        $rates[$rate_key] - > cost = 0;
        unset($rates['flat_rate:12']);
        unset($rates['flat_rate:13']);
        unset($rates['flat_rate:14']);
        unset($rates['flat_rate:15']);
      }
    }
  }
  return $rates;
}

以上代码并不适合所有场景,请帮助我。

解决方法

如果您知道每种运输方式的 shipping_rate_id 值,则意味着在 WooCommerce 插件中(WooCommerce > 设置 > 运输 > 运输区域 > 编辑 > 运输方式)您已经创建并分配了运输成本(或免费)适用于每种运输方式

因此您无需再次更改每一项的成本。

另外,对于免费送货,运费 ID 将为 free_shipping:17 而不是 flat_rate:17
除非您创建了零成本送货方式(统一费率而不是免费送货).

工作代码将是:

add_filter('woocommerce_package_rates','change_shipping_method_based_on_cart_total',11,2);
function change_shipping_method_based_on_cart_total( $rates,$package ) {

    // set the shipping class id to exclude
    $shipping_class_id = 150;

    // initializes the total cart of products
    $total_cart = 0;

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        // if the product has the shipping class id equal to "$shipping_class_id" it is excluded from the count
        if ( $product->get_shipping_class_id() == $shipping_class_id ) {
            continue;
        }
        $qty = $cart_item['quantity'];
        $price = $cart_item['data']->get_price();
        // add the product line total to the total
        $total_cart += $price * $qty;

    }

    switch ( true ) {
        case $total_cart < 10: // £4.99
            // only the "flat_rate:12" shipping method will be enabled
            unset($rates['flat_rate:13']);
            unset($rates['flat_rate:14']);
            unset($rates['flat_rate:15']);
            unset($rates['flat_rate:17']); // or "unset( $rates['free_shipping:17'] );"
            break;
        case $total_cart >= 10 && $total_cart < 20: // £3.99
            // only the "flat_rate:13" shipping method will be enabled
            unset($rates['flat_rate:12']);
            unset($rates['flat_rate:14']);
            unset($rates['flat_rate:15']);
            unset($rates['flat_rate:17']); // or "unset( $rates['free_shipping:17'] );"
            break;
        case $total_cart >= 20 && $total_cart < 30: // £2.99
            // only the "flat_rate:14" shipping method will be enabled
            unset($rates['flat_rate:12']);
            unset($rates['flat_rate:13']);
            unset($rates['flat_rate:15']);
            unset($rates['flat_rate:17']); // or "unset( $rates['free_shipping:17'] );"
            break;
        case $total_cart >= 30 && $total_cart < 40: // £1.99
            // only the "flat_rate:15" shipping method will be enabled
            unset($rates['flat_rate:12']);
            unset($rates['flat_rate:13']);
            unset($rates['flat_rate:14']);
            unset($rates['flat_rate:17']);
            break;
        case $total_cart >= 40: // free
            // only the "flat_rate:17" or "free_shipping:17" shipping method will be enabled
            unset($rates['flat_rate:12']);
            unset($rates['flat_rate:13']);
            unset($rates['flat_rate:14']);
            unset($rates['flat_rate:15']);
            break;
    }
    
    return $rates;
}

代码已经过测试并且可以工作。代码进入您主题的 functions.php 文件。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。