选择“本地取件"时,不要对正在销售的产品给予折扣.在WooCommerce中 [英] Do not give discounts for on sale products when selecting "local pickup" in WooCommerce

查看:58
本文介绍了选择“本地取件"时,不要对正在销售的产品给予折扣.在WooCommerce中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 本地取件Woocommerce中的运输选项自定义百分比折扣 答案代码,该代码在选择本地取件"时会添加折扣在购物车中并在结帐时.

I use Local pickup shipping option custom percentage discount in Woocommerce answer code that adds a discount when choosing "Local Pickup" in the cart and at checkout.

我已将折扣设置为20%.

I have set the discount to 20%.

如果产品已经打折,如何更改此代码以将产品从计算中排除?

How can I change this code to exclude products from calculations if they are already at a discount?

例如,购物车中有3种产品:2种具有基本价格的产品和1种具有折扣的产品.选择本地取件"时如何确保20%的折扣?仅适用于具有基本价格的产品?

For example, there are 3 products in the cart: 2 products with a base price and 1 product with a discount. How to make sure that a 20% discount when choosing "Local Pickup" applies only to products with a base price?

有帮助吗?

推荐答案

而不是使用 $ cart-> get_subtotal()

$ cart_item ['line_subtotal'] 已添加到 $ line_subtotal 如果产品不是 is_on_sale()(折扣)

$cart_item['line_subtotal'] is added to the $line_subtotal, if the product is not is_on_sale() (discount)

/**
* Discount for Local Pickup
*/
function custom_discount_for_pickup_shipping_method( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = 20; // Discount percentage

    $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
    $chosen_shipping_method    = explode(':', $chosen_shipping_method_id)[0];

    // Only for Local pickup chosen shipping method
    if ( strpos( $chosen_shipping_method_id, 'local_pickup' ) !== false ) {

        // Set variable
        $new_subtotal = 0;

        // Loop though each cart items and set prices in an array
        foreach ( $cart->get_cart() as $cart_item ) {

            // Get product
            $product = wc_get_product( $cart_item['product_id'] );

            // Product has no discount
            if ( ! $product->is_on_sale() ) {
                // line_subtotal
                $line_subtotal = $cart_item['line_subtotal'];

                // Add to new subtotal
                $new_subtotal += $line_subtotal;
            }
        }

        // Calculate the discount
        $discount = $new_subtotal * $percentage / 100;

        // Add the discount
        $cart->add_fee( __('Discount') . ' (' . $percentage . '%)', -$discount );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_pickup_shipping_method', 10, 1 );

这篇关于选择“本地取件"时,不要对正在销售的产品给予折扣.在WooCommerce中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆