WooCommerce特定国家/地区(不包括当地提货)的最低订单金额

时间:2020-07-02 作者:Philipp D

我正试图为WooCommerce制定一个代码,为特定国家/地区设定最低订单金额,但如果他们选择“我想让他们下单”;本地取货“;作为他们的运输方式。然而,我不能让它工作。我试着去查,但找不到解释它的帖子。我希望你能给我一些见解。

提前感谢!

// Set a minimum amount of order based on shipping zone & shipping method before checking out

add_action( \'woocommerce_check_cart_items\', \'cw_min_num_products\' );

// Only run in the Cart or Checkout pages
function cw_min_num_products() {
    
    if( is_cart() || is_checkout() ) {
        global $woocommerce;

        // Set the minimum order amount, shipping zone & shipping method before checking out
        $minimum = 75;
        $county     = array(\'CH\') || array(\'LI\');
        $shipping_method    = \'local_pickup\';
        
        // Defining var total amount      
        $cart_tot_order = WC()->cart->total;
        
        // Compare values and add an error in Cart\'s total amount
        // happens to be less than the minimum required before checking out.
        // Will display a message along the lines 
     
        if( $cart_tot_order < $minimum && in_array( WC()->customer->get_shipping_country(), $county ) && WC()->shipping->get_shipping_methods() != $shipping_method ) {
            // Display error message
            wc_add_notice( sprintf( \'<strong>Sie haben die Mindestbestellmenge von $%s EUR noch nicht erreicht. Geringerer Bestellwert ist nur bei Selbstabholung möglich.</strong>\' 
                . \'<br />Ihre derzeitige Bestellmenge ist: $%s.\',
                $minimum,
                $cart_tot_order ),
            \'error\' );
        }
    }
}

1 个回复
SO网友:Philipp D

嘿,我只是想让你知道!我能够用这段代码实现我想要的结果!

// Set a minimum amount of order based on shipping zone & shipping method before checking out

add_action( \'woocommerce_check_cart_items\', \'cw_min_num_products\' );

// Only run in the Cart or Checkout pages
function cw_min_num_products() {
    
    if( is_cart() || is_checkout() ) {
        global $woocommerce;

        // Set the minimum order amount, shipping zone & shipping method before checking out
        $minimum = 75;
        $county     = array(\'CH\',\'LI\');
        $chosen_shipping = WC()->session->get( \'chosen_shipping_methods\' )[0];
        $chosen_shipping = explode(\':\', $chosen_shipping);
        
        // Defining var total amount      
        $cart_tot_order = WC()->cart->subtotal;
        
        // Compare values and add an error in Cart\'s total amount
        // happens to be less than the minimum required before checking out.
        // Will display a message along the lines 
     
        if( $cart_tot_order < $minimum && in_array( WC()->customer->get_shipping_country(), $county ) && $chosen_shipping[0] != \'local_pickup\') {
            // Display error message
            wc_add_notice( sprintf( \'<strong>Sie haben die Mindestbestellmenge von €%s exklusive Versandkosten noch nicht erreicht. Geringerer Bestellwert ist nur bei Selbstabholung möglich.</strong>\' 
                . \'<br />Ihre derzeitige Bestellmenge ist: €%s.\',
                $minimum,
                $cart_tot_order ),
            \'error\' );
        }
    }
}

相关推荐