如果折扣产品的价格不超过10%,请从优惠券中添加折扣

发布于 2025-01-23 08:46:01 字数 2517 浏览 0 评论 0原文

如果折扣产品的价格不超过10%,我必须从优惠券中享受折扣。现在,我有:

function action_woocommerce_before_calculate_totals( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
    // Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
    $excluded_categories = array( 'czekolada-na-goraco', 'bita-smietana', 'czekoladziarki', 'czesci-zamienne', 'dystrybutory-do-napojow', 'gofrownice', 'kostkarki-luskarki', 'urzadzenia-cukiernicze', 'maszyny-do-lodow', 'maszyny-kawa-mrozona', 'miksery-lody-flurry' );
    // Initialize
    $coupon_flag = false;
    // Loop through applied coupons
    foreach ( $cart->get_applied_coupons() as $coupon_code ) {
        // Get an instance of the WC_Coupon Object
        $coupon = new WC_Coupon( $coupon_code );
        // Only for certain types, several can be added, separated by a comma
        if ( in_array( $coupon->get_discount_type(), array( 'percent', 'percent_product' ) ) ) {
            $coupon_flag = true;
            break;
        }
    }
    if ( $coupon_flag ) {
        // Loop through cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            // Get product ID in
            $product_id = $cart_item['product_id'];
            // NOT contains the definite term
            if ( ! has_term( $excluded_categories, 'product_cat', $product_id ) ) {
                // Get regular price
                $regular_price = $cart_item['data']->get_regular_price();
                // Get promo price
                $promo_price = $cart_item['data']->get_price();
                $percentage = 10;
                $percent_price = $regular_price - ($regular_price * ($percentage / 100));
                // Set new price
                if ( $percent_price < $promo_price ) {
                    $cart_item['data']->set_price( $regular_price );
                } else {
                    $cart_item['data']->set_price( $promo_price ); 
                }
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );

如果促销价格高于常规价格-10%,则以上代码将基本价格折扣。但是,如果促销价格较低,它将收取促销价格。如果促销价格低于常规价格-10%,则不应将优惠券的百分比降低。我怎么能得到它?

编辑 - 一些解释:

产品A: 正常价格200欧元, 促销价格100欧元, 折扣50%。

产品B: 正常价格200欧元, 促销价格190欧元, 折扣5%。

我希望折扣仅适用于促销价格不超过10%的产品。因此,优惠券应仅适用于产品B,因为当前折扣小于10%。

I have to do a discount from the coupon if the price of the discounted product isn't more than 10%. Now I have:

function action_woocommerce_before_calculate_totals( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
    // Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
    $excluded_categories = array( 'czekolada-na-goraco', 'bita-smietana', 'czekoladziarki', 'czesci-zamienne', 'dystrybutory-do-napojow', 'gofrownice', 'kostkarki-luskarki', 'urzadzenia-cukiernicze', 'maszyny-do-lodow', 'maszyny-kawa-mrozona', 'miksery-lody-flurry' );
    // Initialize
    $coupon_flag = false;
    // Loop through applied coupons
    foreach ( $cart->get_applied_coupons() as $coupon_code ) {
        // Get an instance of the WC_Coupon Object
        $coupon = new WC_Coupon( $coupon_code );
        // Only for certain types, several can be added, separated by a comma
        if ( in_array( $coupon->get_discount_type(), array( 'percent', 'percent_product' ) ) ) {
            $coupon_flag = true;
            break;
        }
    }
    if ( $coupon_flag ) {
        // Loop through cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            // Get product ID in
            $product_id = $cart_item['product_id'];
            // NOT contains the definite term
            if ( ! has_term( $excluded_categories, 'product_cat', $product_id ) ) {
                // Get regular price
                $regular_price = $cart_item['data']->get_regular_price();
                // Get promo price
                $promo_price = $cart_item['data']->get_price();
                $percentage = 10;
                $percent_price = $regular_price - ($regular_price * ($percentage / 100));
                // Set new price
                if ( $percent_price < $promo_price ) {
                    $cart_item['data']->set_price( $regular_price );
                } else {
                    $cart_item['data']->set_price( $promo_price ); 
                }
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );

The above code applies a discount for the basic price if the promotional price is higher than the regular price -10%. However, if the promotional price is lower, it will charge the promotional price. If the promotional price is lower than the regular price -10%, it shouldn't be reduced by a percentage of the coupon. How can I get it?

EDIT - A little explanation:

Product A:
Regular price 200 euros,
Promotion price 100 euros,
Discount 50%.

Product B:
Regular price 200 euros,
Promotion price 190 euros,
Discount 5%.

I would like the discount to apply only to products whose promotional price doesn't exceed 10%. So the coupon should only work for product B as the current discount is less than 10%.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文