对 WooCommerce 中最便宜的购物车商品应用 100% 优惠券折扣 [英] Apply a 100% coupon discount on the cheapest cart item in WooCommerce

查看:40
本文介绍了对 WooCommerce 中最便宜的购物车商品应用 100% 优惠券折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用普通的 woocommerce 优惠券方法创建了一个BOGOF"(买一送一)优惠券.

I have created a 'BOGOF' (buy one get one free) coupon, using the normal woocommerce coupon method.

优惠券为用户提供购物车中另外 1 件商品的 100% 折扣.

The coupon gives the user 100% percentage discount on 1 other item in the cart.

优惠券设置

一般:

  • 折扣类型:百分比折扣券

金额:100

使用限制:

  • 限制使用 X 项:1

<小时>

使用时:

  • 优惠券 100% 适用于购物车中的随机商品(我猜是默认行为)
  • Coupon applies 100% to a random item in the cart (default behavior, I guess)

期望:

  • 购物车中最便宜的商品需要 100% 的折扣.
  • It needs to take 100% off the cheapest item in the cart.

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

With the following code I try to achieve my goal, unfortunately without the desired result

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

推荐答案

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

First there is a big mistake in your code as $cart_item variable hook argument is the current cart item but not the cart items array...

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

The following will apply a coupon discount of 100% on the cheapest cart item (commented code):

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 文件中.经测试有效.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

这篇关于对 WooCommerce 中最便宜的购物车商品应用 100% 优惠券折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆