2017-02-22 87 views
1

在客户端WooCommerce网站,免费送货方式启用订单金额高达250.我使用代码(从this answer,以隐藏其他运费时,订单金额超过250,除非购物车内有重物。运费计算的项目重量和购物车的金额

add_filter('woocommerce_package_rates', 'conditionally_hide_other_shipping_based_on_items_weight', 100, 1); 
function conditionally_hide_other_shipping_based_on_items_weight($rates) { 
    // targeted weight 
    $target_product_weight = 12; 
    $target_cart_amount = 250; 

    WC()->cart->subtotal_ex_tax >= $target_cart_amount ? $passed = true : $passed = false ; 

    // Iterating trough cart items to get the weight for each item 
    foreach(WC()->cart->get_cart() as $cart_item){ 
     if($cart_item['variation_id'] > 0) 
      $item_id = $cart_item['variation_id']; 
     else 
      $item_id = $cart_item['product_id']; 

     // Getting the product weight 
     $product_weight = get_post_meta($item_id , '_weight', true); 

     if(!empty($product_weight) && $product_weight >= $target_cart_amount){ 
      $light_products_only = false; 
      break; 
     } 
     else $light_products_only = true; 
    } 

    // If 'free_shipping' method is available and if products are not heavy 
    // and cart amout up to the target limit, we hide other methods 
    $free = array(); 
    foreach ($rates as $rate_id => $rate) { 
     if ('free_shipping' === $rate->method_id && $passed && $light_products_only) { 
      $free[ $rate_id ] = $rate; 
      break; 
     } 
    } 

    return ! empty($free) ? $free : $rates; 
} 

但是现在,我想设置将在2种方式计算的变量出厂量:

  • 当顺序量低于250时,计算将1欧元由千对总订单项重量。
  • 当订单金额达到250时,只会计算重物重量(每公斤1欧元)。如果没有重物品,可以免费送货。

我该如何做到这一点,因为它有点复杂?
任何跟踪?

我已经尝试了一些现有的相关插件,但它们对于这种情况并不方便。

谢谢。

回答

3

是的,这是可能的,而不使用自定义运送费用,上车的物品的重量和车量计算插件...但是你需要有一个'Flat rate'运输方式一套以最小量。这通常应该与您使用的代码一起工作。

这里是代码(评论)

//Adding a custom Shipping Fee to cart based conditionaly on weight and cart amount 
add_action('woocommerce_cart_calculate_fees', 'custom_conditional_shipping_fee', 10, 1); 
function custom_conditional_shipping_fee($cart_object){ 

    #### YOUR SETTINGS #### 

    // Your targeted "heavy" product weight 
    $target_weight = 20; 

    // Your targeted cart amount 
    $target_cart_amount = 250; 

    // Price by Kg; 
    $price_kg = 1; 

    // Amount set in 'flat rate' shipping method; 
    $flat_rate_price; 


    // Initializing variables 
    $fee = 0; 
    $calculated_weight = 0; 

    // For cart SUBTOTAL amount EXCLUDING TAXES 
    WC()->cart->subtotal_ex_tax >= $target_cart_amount ? $passed = true : $passed = false ; 

    // For cart SUBTOTAL amount INCLUDING TAXES (replace by this): 
    // WC()->cart->subtotal >= $target_cart_amount ? $passed = true : $passed = false ; 

    // Iterating through each cart items 
    foreach($cart_object->get_cart() as $cart_item){ 

     // Item id ($product ID or variation ID) 
     if($cart_item['variation_id'] > 0) 
      $item_id = $cart_item['variation_id']; 
     else 
      $item_id = $cart_item['product_id']; 

     // Getting the product weight 
     $product_weight = get_post_meta($item_id , '_weight', true); 

     // Line item weight 
     $line_item_weight = $cart_item['quantity'] * $product_weight; 

     // When cart amount is up to 250, Adding weight of heavy items 
     if($passed && $product_weight > $target_weight) 
      $calculated_weight += $line_item_weight; 
    } 

    #### Making the fee calculation #### 

    // Cart is up to 250 with heavy items 
    if ($passed && $calculated_weight != 0) { 
     // Fee is based on cumlated weight of heavy items 
     $fee = ($calculated_weight * $price_kg) - $flat_rate_price; 
    } 
    // Cart is below 250 
    elseif (!$passed) { 
     // Fee is based on cart total weight 
     $fee = ($cart_object->get_cart_contents_weight() * $price_kg) - $flat_rate_price; 
    } 

    #### APPLYING THE CALCULATED FEE #### 

    // When cart is below 250 or when there is heavy items 
    if ($fee > 0){ 
     // Rounding the fee 
     $fee = round($fee); 
     // This shipping fee is taxable (You can have it not taxable changing last argument to false) 
     $cart_object->add_fee(__('Shipping weight fee', 'woocommerce'), $fee, true); 
    } 
} 

代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。

此代码测试,它的工作原理

最小“扁平率”量从费用计算扣除,这样一来客户支付对价

+0

非常感谢! – spy

相关问题