Categories

Monday, December 23, 2024
#919814419350 therichposts@gmail.com
WoocommerceWoocommerce HooksWordpress

WooCommerce Set Minimum Order Amount based on cities

WooCommerce Set Minimum Order Amount based on cities
WooCommerce Set Minimum Order Amount based on cities
WooCommerce Set Minimum Order Amount based on cities

To set a minimum order amount based on cities in WooCommerce, you’ll need to add some custom code to your site. This typically involves writing a function in PHP that checks the customer’s shipping city during the checkout process and sets a minimum order amount accordingly. Here’s a step-by-step guide on how you can implement this:

Step 1: Identify Your Theme’s Functions.php File

You will need to add custom PHP code to your WordPress theme. It’s recommended to use a child theme to avoid losing your changes when the main theme updates.

Step 2: Add Custom PHP Code

Open the functions.php file of your theme and add the following code at the end of the file. This example sets different minimum order amounts for two cities, “City A” and “City B“:

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

function set_minimum_order_amount_based_on_city() {
    $minimum_amount_city_A = 50; // Minimum amount for City A
    $minimum_amount_city_B = 100; // Minimum amount for City B
    $current_city = WC()->customer->get_shipping_city();

    if ($current_city == "City A" && WC()->cart->total < $minimum_amount_city_A) {
        wc_add_notice( sprintf( "Your current city (%s) requires a minimum order of %s.", $current_city, wc_price($minimum_amount_city_A) ), 'error' );
    } elseif ($current_city == "City B" and WC()->cart->total < $minimum_amount_city_B) {
        wc_add_notice( sprintf( "Your current city (%s) requires a minimum order of %s.", $current_city, wc_price($minimum_amount_city_B) ), 'error' );
    }
}

Explanation:

  • Hooks Used: The woocommerce_checkout_process and woocommerce_before_cart hooks are used to enforce the minimum order amount check both when the customer updates their cart and proceeds to checkout.
  • City Check: The script retrieves the customer’s shipping city with WC()->customer->get_shipping_city().
  • Conditions: Based on the city, it checks if the total cart amount is less than the specified minimum for that city.
  • Error Message: If the condition is true, it displays an error notice using wc_add_notice.

Step 3: Customize for More Cities

If you need to set different minimum amounts for more cities, expand the conditional statements (if and elseif) to include more cities and corresponding minimum amounts.

Step 4: Test Your Changes

After adding the code:

  • Clear your site cache if you’re using caching plugins.
  • Test by adding products to your cart and trying to checkout using addresses from the specified cities to ensure that the minimum order amount is enforced correctly.

This code should be adjusted according to your specific needs and the structure of your WooCommerce setup. Make sure to backup your website before making changes to the code. If you’re not comfortable editing PHP files, consider hiring a developer or consulting with a WooCommerce expert.

Jassa

Thanks

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

4 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.