【问题标题】:Do not give discounts for on sale products when selecting "local pickup" in WooCommerce在 WooCommerce 中选择“本地取货”时,不要给打折产品打折
【发布时间】:2020-08-12 11:52:19
【问题描述】:

我使用 Local pickup shipping option custom percentage discount in Woocommerce 回答代码,在购物车和结帐时选择“本地取货”时添加折扣。

我已将折扣设为 20%。

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

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

有什么帮助吗?

【问题讨论】:

    标签: php wordpress woocommerce cart discount


    【解决方案1】:

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

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

    /**
    * 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 );
    

    【讨论】:

    • 非常感谢!一切都很好!告诉我,如何给客户提示“提货优惠不适用于打折商品”?
    • 使用:wc_print_notice(),请参阅以下示例如何使用:stackoverflow.com/a/56235957/11987538 - add_action( 'woocommerce_cart_calculate_fees', 'add_custom_surcharge', 10, 1 );
    猜你喜欢
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    相关资源
    最近更新 更多