0

I'm trying through this code to set a minimum price for orders:

// Set a minimum dollar amount per order
add_action( 'woocommerce_check_cart_items', __NAMESPACE__ . '\\spyr_set_min_total' );
function spyr_set_min_total() {
    // Only run in the Cart or Checkout pages
    if ( is_cart() || is_checkout() ) {
        global $woocommerce;

        // Set minimum cart total
        $minimum_cart_total = 10;

        // Total we are going to be using for the Math
        // This is before taxes and shipping charges
        $total = WC()->cart->subtotal;

        // Compare values and add an error is Cart's total
        // happens to be less than the minimum required before checking out.
        // Will display a message along the lines of
        // A Minimum of 10 USD is required before checking out. (Cont. below)
        // Current cart total: 6 USD 
        if ( $total <= $minimum_cart_total  ) {
            // Display our error message
            wc_add_notice( 
                sprintf( 
                    'You have not yet reached the minimum order of € 10,00.' . 
                        'Your order is: %s %s',
                    $total,
                    get_option( 'woocommerce_currency' ) 
                ),
                'error' 
            );
        }
    }
}

So everything works, but I need to enable it only for orders placed in UK.

I thought that the solution could be adding these variables:

$states = array( 'UK' );
$shippingaddr = WC()->customer->shipping_state;

if ( is_cart() || is_checkout() || $shippingaddr = $states ) {

but it's not working. Any suggestions?

EDIT.

CORRECT CODE

// Only run in the Cart or Checkout pages
    global $woocommerce;
    if( ( is_cart() || is_checkout()) && "UK" == WC()->customer->shipping_country ) 
    // Set minimum cart total
    $minimum_cart_total = 10;
  • Where are you putting this code ie: functions.php or plugin? Are you requiring customers to be logged in before shopping? – Scriptonomy Nov 2 '15 at 17:56
  • @Scriptonomy in functions.php .. i solved with the suggestion of ItsMePN. You can see the right code. – sb0k Nov 2 '15 at 18:30
1

Looking at your code I think you have enabled checkout only for logged in users because you won't get the country otherwise. If that's the fact then you just need to change your if condition from if( is_cart() || is_checkout() ) to if( ( is_cart() || is_checkout()) && "UK" == WC()->customer->shipping_country ) and put global $woocommerce before the if condition. Rest of the code is correct & working fine.

| improve this answer | |
  • now works perfectly! It was not so wrong..maybe I am becoming an expert! – sb0k Nov 2 '15 at 18:16
  • You only need one single truth: in_array(WC()->customer->shipping_state, $states) – Scriptonomy Nov 2 '15 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.