首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对WooCommerce中最便宜的购物车项目实行100%的优惠券折扣

对WooCommerce中最便宜的购物车项目实行100%的优惠券折扣
EN

Stack Overflow用户
提问于 2020-04-28 12:27:03
回答 3查看 1.6K关注 0票数 3

我已经创建了一个'BOGOF‘(购买一个免费)优惠券,使用正常的woocommerce优惠券方法。

优惠券给用户100%的折扣,在其他一个项目在购物车。

优惠券设置

General:

Coupon

  • amount: 100
  • 折扣类型:百分比折扣

使用限制:

  • 限制使用X项: 1

使用时:

  • 优惠券100%应用于购物车中的随机项(我猜,默认行为)

所需

  • 它需要100%从购物车中最便宜的商品中取下来。

使用下面的代码,我试图实现我的目标,不幸的是,没有达到预期的结果。

代码语言:javascript
复制
function filter_woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $instance ) { 
    $price_array = array();

    foreach( $cart_item as $item ) {
        echo $item->price;
        if($item->price > 0){
            array_push($price_array, $item->price);
        }
    }

    $lowestPrice = min($price_array);

    if( $lowestPrice < $discount ){
        $discount = $lowestPrice; 
    }

    return $discount; 
}    
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-04-28 16:18:12

首先,代码中有一个很大的错误,因为$cart_item变量钩子参数是当前的购物车项,而不是购物车项数组。

以下优惠将适用于最便宜的购物车商品的优惠券折扣(注释代码):

代码语言:javascript
复制
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_wc_coupon_get_discount_amount', 10, 5 );
function filter_wc_coupon_get_discount_amount( $discount_amount, $discounting_amount, $cart_item, $single, $coupon ) { 
    // Define below your existing coupon code
    $coupon_code = 'BOGOF';

    // Only for a defined coupon code
    if( strtolower( $coupon_code ) !== $coupon->get_code() ) 
        return $discount_amount;

    $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); // return the lowest item price as a discount
        }
    } else {
        return 0;
    }
}

代码在您的活动子主题(或活动主题)的functions.php文件中。测试和工作。

票数 5
EN

Stack Overflow用户

发布于 2021-07-30 09:23:27

如果您想使用自己的折扣,而不仅仅是百分之百地将return reset($items_prices);替换为return $discount_amount;

票数 0
EN

Stack Overflow用户

发布于 2021-10-27 22:06:09

我对此做了一个很小的调整--我注意到如果我的手推车里有很多东西,它就会比最便宜的产品便宜100%。因此,如果一个用户有10件t恤衫,每件10 it,那么它就给了100 it的折扣。我在下面稍微修改了一下,这可能会对将来的人有所帮助:

代码语言: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;
          }

    }

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

https://stackoverflow.com/questions/61480164

复制
相关文章

相似问题

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