【问题标题】:Minimum order amount except for specific shipping method in WooCommerce除 WooCommerce 中的特定运输方式外的最低订单金额
【发布时间】:2020-07-07 10:40:26
【问题描述】:

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

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' );
        }
    }
}

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

除了 WooCommerce 中的“本地取货”送货方式外,我如何设置最低订单金额?

【问题讨论】:

    标签: php wordpress woocommerce cart checkout


    【解决方案1】:

    基于 Getting minimum order amount for 'Free Shipping' method in checkout page 答案代码和 Set a minimum order amount in WooCommerce 答案代码,这是设置最低订单金额的正确方法,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 文件中。经过测试并且可以工作。

    或者你也可以使用:

    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 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-29
      • 2021-05-08
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 2016-02-11
      相关资源
      最近更新 更多