【问题标题】:Adding shipping costs based on quantity of products in Woocommerce cart that exceed 40EUR. The additional cost differs based on shipping country根据 Woocommerce 购物车中超过 40 欧元的产品数量添加运费。额外费用因运送国家而异
【发布时间】:2020-05-09 11:48:09
【问题描述】:

这是我正在编写的一个小函数,用于根据购物车中价格超过 40 欧元的产品数量添加额外的运费。

screenshot of my code

<br>
<div>

<?php  

  $additionalShippingCosts = 0;

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
     $product = $cart_item['data'];
     $product_id = $cart_item['product_id'];
     $quantity = $cart_item['quantity'];
     $price = WC()->cart->get_product_price( $product );
     $pricenum = (float) $product->get_price();

     if($pricenum < 40) {
      $price2 = 0; 
     } else {
      $price2 = 5;
       $additionalShippingCosts += $price2*$quantity;
     }
?>

  <div>product price: <?php echo $pricenum; ?></div>
  <div>extra cost: <?php echo $price2; ?> * <?php echo $quantity; ?></div>

  <br>
<?php
}


?>

  <div>Additional shipping cost: <?php echo $additionalShippingCosts; ?></div>



</div>

我已经到了函数计算应该添加多少运费的地步,现在我需要 1) 实际将其添加到运费中 2) 每次更新购物车时运行它 3) 为运输国家/地区引入条件

条件句会很简单,我将对它们进行硬编码。基本上“如果运送到德国,每件超过 40 欧元的额外运费为 10 欧元”和“如果运送到爱尔兰,每件超过 40 欧元的额外运费为 15 欧元”

How the cart page looks with notes on how it should look

【问题讨论】:

    标签: php wordpress woocommerce shipping


    【解决方案1】:

    [已解决] 我犯了一个错误,没有正确研究运输类可以做什么。为此无需自定义代码。只需为位置设置统一费率 -> 为超过 XX 欧元的产品添加运费等级 -> 为等级和无等级设置定价,即 10.00*[qty] 和 20.00*[qty]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多