最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WooCommerce set a min order price for a single country

programmeradmin5浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

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;
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

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;
Share Improve this question edited Nov 2, 2015 at 18:29 sb0k asked Nov 2, 2015 at 16:49 sb0ksb0k 838 bronze badges 3
  • Where are you putting this code ie: functions.php or plugin? Are you requiring customers to be logged in before shopping? – Scriptonomy Commented Nov 2, 2015 at 17:56
  • @Scriptonomy in functions.php .. i solved with the suggestion of ItsMePN. You can see the right code. – sb0k Commented Nov 2, 2015 at 18:30
  • 3rd party plugin dev support is offtopic and not in this stacks scope – Tom J Nowell Commented Oct 1, 2020 at 13:13
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论