- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用普通的 woocommerce 优惠券方法创建了一张“BOGOF”(买一送一)优惠券。
优惠券为用户提供了购物车中其他 1 件商品的 100% 百分比折扣。
Coupon settings
General:
Discount type: Percentage discount Coupon
amount: 100
Usage limits:
- Limit usage to X items: 1
使用时:
期望:
我尝试用下面的代码实现我的目标,不幸的是没有得到想要的结果
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
变量 Hook 参数是当前购物车商品,而不是购物车商品数组...
以下将对最便宜的购物车商品(注释代码)应用 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 文件。经过测试并有效。
关于php - 对 WooCommerce 中最便宜的购物车商品应用 100% 的优惠券折扣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61480164/
我试图了解如何返回多个值。现在,我的代码设置为为任何具有一个兑换代码的代码返回单个对象(redeemDisc)。我的问题是,如果一个代码有多个兑换代码,它只会返回“多个折扣”。我试图了解如何将“red
我有一个函数discount,它接受一个“rate”参数( double )并将我的类中的私有(private)变量netPrice更新为减价。计算看似可行,但有一个问题;它会产生不准确的结果。 pu
我正在尝试获得折扣以应用于我编写的购物车。我正在使用 Adaptive API 系统,但我似乎无法正确使用它。我在 SetPaymentOptions 调用中尝试过: 'receiverO
我在尝试为我的程序创建 switch 语句时陷入困境。为了充分披露,这是明天到期的家庭作业。我应该使用 switch 语句来为所售 CD 的总费用指定折扣率。黄金级折扣 15%,银级折扣 10%,铜级
我的动画遵循这个计时函数:cubic-bezier(0.25, 0.1, 0.25, 1.0) 我想修改这个函数,所以我只得到它的结尾 40%。为了让事情变得简单,我们可以说我想要函数的最后 50%。
Paypal 的新手。在协会网站上使用“立即购买”按钮,适用于单价商品。 但是,现在客户想要设置折扣功能。我认为逻辑很简单,但就我的生活而言,我不了解文档或如何让它正常工作。 这是设置。高尔夫郊游,有
Array ( [return] => http://www.example.com/ [cancel] => http://www.example.com/ [currenc
Paypal 列出了折扣的方法,例如: discount_amount 可选与项目关联的折扣金额。它必须低于商品的售价。如果您指定 discount_amount 且未定义 discount_amou
我在 woocommerce 中有很多产品(将近 30,000 个)。常规价格已设置正确,现在我想将销售价格设置为具有一定折扣百分比的常规价格。我尝试使用一些插件,但由于产品数量,我总是遇到 PHP
如何应用税收、小计折扣以及计算总计? 我的这些段落具有以下 ID 15000 10 // In percentage 1000 // Grandtotal will be calculated an
有人知道与Stripe一次性交易是否可以接受优惠券吗? 我知道您可以添加带有定期订阅的优惠券,但我只希望能够提供一次性付款折扣。 最佳答案 我认为这里的想法是,您可以完全控制单个费用(或一次性付款,无
当产品价格为零或 100% 折扣时,我需要一个解决方案来隐藏产品页面上的“添加到购物车”按钮 我认为以下代码可能用于添加到购物车部分此代码位于以下路径中mytheme/woocommerce/单一产品
我希望为特定的可变产品设置特定的折扣,如果客户购买一种产品,他们会以 50% 的折扣获得另一种(相同的)(买一个 50% 的折扣)。我试过很多折扣插件,我发现最接近的是: WooCommerce 的定
在我上一个问题之后 WooCommerce discount: buy one get one 50% off 每当特定产品(不是所有产品)添加到购物车时,我想向购物车添加自定义通知。 我想先检查数量
我是一名优秀的程序员,十分优秀!