我已经创建了一个'BOGOF‘(购买一个免费)优惠券,使用正常的woocommerce优惠券方法。
优惠券给用户100%的折扣,在其他一个项目在购物车。
优惠券设置
General:
Coupon
使用限制:
使用时:
所需:
使用下面的代码,我试图实现我的目标,不幸的是,没有达到预期的结果。
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 );发布于 2020-04-28 16:18:12
首先,代码中有一个很大的错误,因为$cart_item变量钩子参数是当前的购物车项,而不是购物车项数组。
以下优惠将适用于最便宜的购物车商品的优惠券折扣(注释代码):
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文件中。测试和工作。
发布于 2021-07-30 09:23:27
如果您想使用自己的折扣,而不仅仅是百分之百地将return reset($items_prices);替换为return $discount_amount;
发布于 2021-10-27 22:06:09
我对此做了一个很小的调整--我注意到如果我的手推车里有很多东西,它就会比最便宜的产品便宜100%。因此,如果一个用户有10件t恤衫,每件10 it,那么它就给了100 it的折扣。我在下面稍微修改了一下,这可能会对将来的人有所帮助:
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;
}
}
}https://stackoverflow.com/questions/61480164
复制相似问题