【问题标题】:Discount amount not correct for woocommerce_coupon_get_discount_amountwoocommerce_coupon_get_discount_amount 的折扣金额不正确
【发布时间】:2021-12-13 03:21:12
【问题描述】:

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

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;
}

折扣金额很奇怪——无论我尝试什么,都达不到我期望的价值。起初我以为这是一个百分比折扣,但我希望这是一个固定金额。我尝试在网站的其他地方运行我的最低价格功能,当最低价值项目为 11.95 时,它返回 1.195 - 所以我知道这部分有效。但是一篮子 265.60 的折扣是 23.90 - 我就是不明白!

我只想购买购物车中价格最低的商品,然后打折。

【问题讨论】:

  • 您可以将$lowestprice 作为折扣返回吗?无论如何,这将是一个浮动,所以$lowestprice = min($product_price)/10;
  • 所以我认为问题可能更多在于试图对每个订单项进行折扣。如果我在我的购物篮中只说 10 件一件产品,它就可以完美运行 - 但如果我有一件商品中的 4 件,另一件商品中的 5 件,另一件商品中的 4 件,它会尝试打折所有商品。我想也许我使用的钩子是错误的,我需要对整个购物车应用一次性固定价格折扣。这似乎是我认为的每个订单项。
  • 这可能是您正在寻找的。它对最便宜的购物车商品提供 100% 的折扣...stackoverflow.com/questions/61480164/…
  • 是的!太好了,谢谢老板——我确实做了一个调整,我会在这个问题上补充一下,但这确实很好用。谢谢!

标签: wordpress woocommerce


【解决方案1】:

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

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;
          }

    }

}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 2021-07-23
    相关资源
    最近更新 更多