【发布时间】:2021-12-31 22:10:51
【问题描述】:
我想使用优惠券代码将促销产品的折扣翻倍。
例如:产品以 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%。
我只是不知道如何添加/更改购物车商品的折扣。
【问题讨论】:
-
哦,伙计,会的......
标签: php wordpress woocommerce coupon discount