对 WooCommerce 中最便宜的购物车商品应用 100% 的优惠券折扣
Apply a 100% coupon discount on the cheapest cart item in WooCommerce
我使用普通的 woocommerce 优惠券方法创建了一张“BOGOF”(买一送一)优惠券。
优惠券为用户购买购物车中的其他 1 件商品提供 100% 的百分比折扣。
Coupon settings
General:
Discount type: Percentage discount Coupon
amount: 100
Usage limits:
- Limit usage to X items: 1
使用时:
- 优惠券 100% 应用于购物车中的 随机 商品(我猜是默认行为)
期望:
- 它需要对购物车中最便宜的 商品进行 100% 的折扣。
使用下面的代码我尝试实现我的目标,不幸的是没有得到想要的结果
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
变量挂钩参数是当前购物车项目而不是购物车项目数组...
以下将对最便宜的购物车商品(注释代码)应用 100% 的优惠券折扣:
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 文件。已测试并有效。
如果您想使用自己的折扣不仅 100% 将 return reset($items_prices);
替换为 return $discount_amount;
我对此做了一个相当小的调整 - 我注意到如果我的购物车中有很多商品,它会给最便宜的多种商品 100% 的折扣。因此,如果用户有 10 件 T 恤,每件 10 英镑,则可以享受 100 英镑的折扣。我在下面稍微修改了一下,这可能对以后的人有帮助:
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;
}
}
}
我使用普通的 woocommerce 优惠券方法创建了一张“BOGOF”(买一送一)优惠券。
优惠券为用户购买购物车中的其他 1 件商品提供 100% 的百分比折扣。
Coupon settings
General:
Discount type: Percentage discount Coupon
amount: 100
Usage limits:
- Limit usage to X items: 1
使用时:
- 优惠券 100% 应用于购物车中的 随机 商品(我猜是默认行为)
期望:
- 它需要对购物车中最便宜的 商品进行 100% 的折扣。
使用下面的代码我尝试实现我的目标,不幸的是没有得到想要的结果
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
变量挂钩参数是当前购物车项目而不是购物车项目数组...
以下将对最便宜的购物车商品(注释代码)应用 100% 的优惠券折扣:
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 文件。已测试并有效。
如果您想使用自己的折扣不仅 100% 将 return reset($items_prices);
替换为 return $discount_amount;
我对此做了一个相当小的调整 - 我注意到如果我的购物车中有很多商品,它会给最便宜的多种商品 100% 的折扣。因此,如果用户有 10 件 T 恤,每件 10 英镑,则可以享受 100 英镑的折扣。我在下面稍微修改了一下,这可能对以后的人有帮助:
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;
}
}
}