【问题标题】:WooCommerce: Double discount on sale products with couponWooCommerce:带有优惠券的促销产品双倍折扣
【发布时间】: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


【解决方案1】:

您可以将woocommerce_coupon_get_discount_amount 挂钩与以下优惠券设置结合使用:

  • 正确设置您的优惠券代码:doublediscount
  • 折扣类型:Percentage
  • 金额:15

此答案中应用的步骤:

  • 仅当特定优惠券代码匹配并且产品正在销售时
  • 如果产品未在售,则不会应用折扣(通过 else 条件等于 0。但是,如果不适用,您可以简单地删除 else 条件强>)
  • 计算当前销售产品的百分比折扣。 如果这小于最大附加折扣 (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 );

【讨论】:

    最近更新 更多