- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用优惠券代码对促销产品进行双倍折扣。
例如:产品以 10% 的折扣促销。如果我添加优惠券代码 doublediscount
我想将该折扣加倍至 20%。
优惠券折扣应有 15% 的限制。因此,如果产品以 30% 的折扣打折,则使用优惠券代码的最大附加折扣应为 15%。导致正常价格的 45% 折扣(促销 + 额外折扣)。
到目前为止我的代码是这样的:
add_action( 'woocommerce_before_calculate_totals', 'double_saleprice_coupon' );
function double_saleprice_coupon( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
global $woocommerce;
$coupon_id = 'doublediscount';
// Loop through cart items (first loop)
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
// Check if product in cart is on sale
$product = $cart_item['data'];
$cart_item_regular_price = $cart_item['data']->get_regular_price();
$cart_item_sale_price = $cart_item['data']->get_sale_price();
$cart_item_diff = $cart_item_regular_price - $cart_item_sale_price;
$cart_item_per_cent = round( $cart_item_diff / $cart_item_regular_price * 100, 0 );
if ( $product->is_on_sale() && wc_pb_is_bundled_cart_item($cart_item) === false && $cart_item_per_cent < 15 ) {
echo 'on sale';
echo $cart_item_per_cent;
}
}
}
我遍历所有购物车商品并检查它们是否在打折以及折扣是否低于 15%。如果是这样,我想更改这些购物车商品的折扣。
如果购物车商品的折扣超过 15%,我什么都不想做。因此,优惠券代码 doublediscount
将对他们应用 15%。
我只是不知道如何添加/更改购物车商品的折扣。
最佳答案
您可以结合以下优惠券设置使用 woocommerce_coupon_get_discount_amount
Hook :
doublediscount
百分比
15
此答案中应用的步骤:
15
),然后折扣翻倍15
)所以你得到:
function filter_woocommerce_coupon_get_discount_amount( $discount, $price_to_discount , $cart_item, $single, $coupon ) {
// Returns true when viewing the cart page & only apply for this coupon
if ( is_cart() || is_checkout() && $coupon->get_code() == 'doublediscount' ) {
// Get an instance of the WC_Product object
$product = $cart_item['data'];
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// On sale
if ( $product->is_on_sale() ) {
// Regular price
$cart_item_regular_price = $product->get_regular_price();
// Sale price
$cart_item_sale_price = $product->get_sale_price();
// Calculate the percentage difference
$cart_item_diff = $cart_item_regular_price - $cart_item_sale_price;
$cart_item_percentage = round( $cart_item_diff / $cart_item_regular_price * 100, 0 );
// Get maximum added discount
$max_added_discount = $coupon->get_amount();
// Less than maximum added discount
if ( $cart_item_percentage < $max_added_discount ) {
$discount = round( ( $price_to_discount * $cart_item_percentage ) / 100, 0 );
}
} else {
$discount = 0;
}
}
}
return $discount;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
关于php - WooCommerce:带优惠券的促销产品双倍折扣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70063577/
我有三个 double 组列表。我想将数组1复制到数组2并将其放入第三个数组中。 // ArrayList 1 [0.0, 1.0, 0.0, 1.0] // ArrayList 2 // Rando
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在编写一个带有变量 long balance 的银行程序将美分存入账户。当用户输入金额时,我有一种方法可以将美元转换为美分: public static long convertFromUsd (
如何 DRY(不要重复自己)RSpec 中的 double?例如: let(:s1) {instance_double(Class1, :att
我是 Java 新手,我想知道为什么当你有双 10/4 时却得到 2? double 是否总是必须有小数才能得到正确的答案?谢谢。 public class Super { public st
我正在用 Python 为 Fortran 库编写前端。 Python 模块应该可以在 32 位和 64 位机器上运行;适用于 Windows、Linux 和 Mac。 我想了解一些数据类型的字节宽度
我有两个ajax json。一个用于聊天用户列表列,另一个用于每个用户的消息历史记录。当单击 $('.member_list ul li') 元素时...第二个 ajax 将 li 的值作为 json
从 GHCi 中的 .hs 文件加载 a = 2+2.0 并执行 :t a 显示 a::Double。 另一方面,在 GHCi 中执行 let b = 2+2.0 和 :t b 会显示 b::Frac
我是一名优秀的程序员,十分优秀!