WooCommerce 中除特定运输方式外的最小订单金额 [英] Minimum order amount except for specific shipping method in WooCommerce

查看:32
本文介绍了WooCommerce 中除特定运输方式外的最小订单金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WooCommerce 中,我使用以下代码设置最小订单量:

In WooCommerce, I use the following code to set a minimum order amount:

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );

function wc_minimum_order_amount() {

    $minimum = 50; // Hier gibst du den Mindestbestellwert ein
    
    if ( WC()->cart->total < $minimum ) { 
        if( is_cart() ) { 
            wc_print_notice( sprintf( 'Der Mindestbestellwert beträgt %s pro Bestellung. Der aktuelle Bestellwert beträgt %s.' , // Text fuer Warenkorb 
                wc_price( $minimum ), 
                wc_price( WC()->cart->total )
            ), 'error' );
        } else {
            wc_add_notice( sprintf( 'Der Mindestbestellwert beträgt %s pro Bestellung. Der aktuelle Bestellwert beträgt %s.' , // Text fuer Kasse
                wc_price( $minimum ),
                wc_price( WC()->cart->total )
            ), 'error' );
        }
    }
}

现在如果客户选择自取"(本地取件运输方式),我不想要任何最低要求的订单金额.

Now if the customer chooses "self pickup" ("Local pickup shipping method), I don't want any minimum required order amount.

如何设置除本地取货"外的最小订单金额?WooCommerce 中的送货方式?

How can I set Minimum order amount except for "Local pickup" shipping method in WooCommerce?

推荐答案

基于在结帐页面获取免费送货"方法的最低订单金额 答案代码以及在 WooCommerce 中设置最小订单金额 答案代码,这里是除了 WooCommerce 中的特定运输方式外,设置最低订单金额的正确方法:

Based on Getting minimum order amount for 'Free Shipping' method in checkout page answer code and also Set a minimum order amount in WooCommerce answer code, here is the correct way to set a Minimum order amount except for specific shipping method in WooCommerce:

add_action( 'woocommerce_check_cart_items', 'wc_minimum_required_order_amount' );
function wc_minimum_required_order_amount() {

    // HERE Your settings
    $minimum_amount     = 50; // The minimum cart total amount
    $shipping_method_id = 'local_pickup'; // The targeted shipping method Id (exception)

    // Get some variables
    $cart_total     = (float) WC()->cart->total; // Total cart amount
    $chosen_methods = (array) WC()->session->get( 'chosen_shipping_methods' ); // Chosen shipping method rate Ids (array)

    // Only when a shipping method has been chosen
    if ( ! empty($chosen_methods) ) {
        $chosen_method  = explode(':', reset($chosen_methods)); // Get the chosen shipping method Id (array)
        $chosen_method_id = reset($chosen_method); // Get the chosen shipping method Id
    }

    // If "Local pickup" shipping method is chosen, exit (no minimun is required)
    if ( isset($chosen_method_id) && $chosen_method_id === $shipping_method_id ) {
        return; // exit
    }

    // Add an error notice is cart total is less than the minimum required
    if ( $cart_total < $minimum_amount ) {
        wc_add_notice( sprintf(
            __("The minimum required order amount is %s (your current order amount is %s).", "woocommerce"), // Text message
            wc_price( $minimum_amount ),
            wc_price( $cart_total )
        ), 'error' );
    }
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

或者你也可以使用:

add_action( 'woocommerce_checkout_process', 'wc_minimum_required_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_required_order_amount' );
function wc_minimum_required_order_amount() {

    // HERE Your settings
    $minimum_amount     = 100; // The minimum cart total amount
    $shipping_method_id = 'local_pickup'; // The targeted shipping method Id (exception)

    // Get some variables
    $cart_total     = (float) WC()->cart->total; // Total cart amount
    $chosen_methods = (array) WC()->session->get( 'chosen_shipping_methods' ); // Chosen shipping method rate Ids (array)

    // Only when a shipping method has been chosen
    if ( ! empty($chosen_methods) ) {
        $chosen_method  = explode(':', reset($chosen_methods)); // Get the chosen shipping method Id (array)
        $chosen_method_id = reset($chosen_method); // Get the chosen shipping method Id
    }

    // If "Local pickup" shipping method is chosen, exit (no minimun is required)
    if ( isset($chosen_method_id) && $chosen_method_id === $shipping_method_id ) {
        return; // exit
    }

    // Add an error notice is cart total is less than the minimum required
    if ( $cart_total < $minimum_amount ) {
        $text_notice = sprintf(
            __("The minimum required order amount is %s (your current order amount is %s).", "woocommerce"), // Text message
            wc_price( $minimum_amount ),
            wc_price( $cart_total )
        );
        
        if ( is_cart() ) {
            wc_print_notice( $text_notice, 'error' );
        } else {
            wc_add_notice( $text_notice, 'error' );
        }
    }
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

这篇关于WooCommerce 中除特定运输方式外的最小订单金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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