首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >woocommerce_coupon_get_discount_amount的折扣金额不正确

woocommerce_coupon_get_discount_amount的折扣金额不正确
EN

Stack Overflow用户
提问于 2021-10-27 20:51:23
回答 1查看 40关注 0票数 3

如果使用我的优惠券类型,我会尝试打折购物车中最便宜的商品:

代码语言:javascript
复制
add_filter('woocommerce_coupon_get_discount_amount', 'wc_cpn_disc', 10, 5);
function wc_cpn_disc($discount, $discounting_amount, $cart_item, $single, $coupon) {
    // IF TYPE MATCHES PERFORM CUSTOM CALCULATION
    if ($coupon->type == 'cheapest_free'){
      global $woocommerce;
      foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
          $_product = $values['data'];
          $product_price[] = get_option('woocommerce_tax_display_cart') == 'excl' ? $_product->get_price_excluding_tax() : $_product->get_price_including_tax(); /*Store all product price from cart items in Array */
      }
      $lowestprice = min($product_price);
      $discount = number_format((float)$lowestprice/10,2,'.','');
    }

    return $discount;
}

折扣金额非常奇怪--无论我怎么尝试,它都不会达到我期望的值。起初我认为这是一个百分比折扣,但我希望这是一个固定的数额。我试着在网站的其他地方运行我的get know函数,当最低值项是11.95时,它返回1.195 -所以我知道这部分是有效的。但是总共265.60个篮子的折扣是23.90%-我就是不明白!

我只想买购物车里最便宜的东西,然后打折。

EN

回答 1

Stack Overflow用户

发布于 2021-10-27 21:59:36

在Bossman和其他几个线程的帮助下,我设法解决了这个问题。事实证明,我需要更改我的方法-下面是完整的代码。

代码语言:javascript
复制
add_filter('woocommerce_coupon_get_discount_amount', 'tfcc_cheapest_free', 10, 5);
function tfcc_cheapest_free($discount, $discounting_amount, $cart_item, $single, $coupon) {
    // IF TYPE MATCHES PERFORM CUSTOM CALCULATION
    if ($coupon->type == 'cheapest_free'){

      $items_prices = [];
          $items_count  = 0;

          // Loop through cart items
          foreach( WC()->cart->get_cart() as $key => $item ){
              // Get the cart item price (the product price)
              if ( wc_prices_include_tax() ) {
                  $price = wc_get_price_including_tax( $item['data'] );
              } else {
                  $price = wc_get_price_excluding_tax( $item['data'] );
              }

              if ( $price > 0 ){
                  $items_prices[$key] = $price;
                  $items_count       += $item['quantity'];
              }
          }

          // Only when there is more than one item in cart
          if ( $items_count > 1 ) {
              asort($items_prices);  // Sorting prices from lowest to highest

              $item_keys = array_keys($items_prices);
              $item_key  = reset($item_keys); // Get current cart item key

              // Targeting only the current cart item that has the lowest price
              if ( $cart_item['key'] == $item_key ) {
                  return reset($items_prices)/$cart_item['quantity']; // return the lowest item price as a discount
              }
          } else {
              return 0;
          }

    }

}

我希望这能帮助将来需要类似功能的其他人。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69745346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档