WooCommerce is a powerful eCommerce platform that allows store owners to sell products globally. However, tailoring pricing strategies for different countries can significantly improve customer engagement and conversions. In this tutorial, we will demonstrate how to apply country-specific discounts dynamically using WooCommerce’s geolocation features.
By the end of this guide, you’ll be able to:
- Detect the user’s country.
- Apply discounts based on their location.
- Ensure smooth integration into your WooCommerce setup.
Step 1: Enable Geolocation in WooCommerce
Before implementing country-specific discounts, you need to ensure geolocation is enabled in your WooCommerce settings:
- Navigate to WooCommerce > Settings > General.
- Locate the “Default Customer Location” setting.
- Set it to Geolocate (with page caching support).
This setting allows WooCommerce to determine the user’s location based on their IP address.
Step 2: Create a Plugin for Country-Based Discounts
To manage country-specific discounts effectively, we will create a custom plugin. This approach ensures that your changes are organized, reusable, and do not interfere with theme updates.
Full Plugin Code
Save the following code as country-based-discounts.php
and upload it to your /wp-content/plugins/
directory. Then activate the plugin from the WordPress admin panel.
<?php
/**
* Plugin Name: WooCommerce Country-Based Discounts
* Plugin URI: https://yourwebsite.com
* Description: Apply dynamic discounts to WooCommerce products based on the user's country.
* Version: 1.0
* Author: Kishores
* Author URI: https://upnrunn.com
* License: GPLv2 or later
* Text Domain: wc-country-discounts
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class WC_Country_Based_Discounts
*/
class WC_Country_Based_Discounts {
public function __construct() {
// Hook into WooCommerce price filters
add_filter( 'woocommerce_product_get_price', [ $this, 'adjust_price_based_on_country' ], 10, 2 );
add_filter( 'woocommerce_product_get_regular_price', [ $this, 'adjust_price_based_on_country' ], 10, 2 );
// Ensure geolocation is enabled
add_action( 'init', [ $this, 'ensure_geolocation_enabled' ] );
}
/**
* Adjust product price based on the user's country.
*
* @param float $price Original product price.
* @param object $product WooCommerce product object.
*
* @return float Adjusted product price.
*/
public function adjust_price_based_on_country( $price, $product ) {
// Get the user's country
$country = $this->get_user_country();
// Define country-specific discounts
$country_discounts = [
'US' => 5, // Discount amount for United States
'IN' => 3, // Discount amount for India
'UK' => 7, // Discount amount for United Kingdom
];
// Check if the country exists in the discounts array
if ( array_key_exists( $country, $country_discounts ) ) {
$discount = $country_discounts[ $country ];
$price -= $discount; // Apply the discount
$price = max( 0, $price ); // Ensure the price does not go below 0
}
return $price; // Return the adjusted price
}
/**
* Get the user's country using WooCommerce Geolocation.
*
* @return string User's country code (e.g., 'US', 'IN').
*/
private function get_user_country() {
if ( class_exists( 'WC_Geolocation' ) ) {
$location = WC_Geolocation::geolocate_ip();
return $location['country'] ?? '';
}
return '';
}
/**
* Ensure geolocation is enabled in WooCommerce settings.
*/
public function ensure_geolocation_enabled() {
$options = get_option( 'woocommerce_default_customer_address' );
if ( $options !== 'geolocation' && $options !== 'geolocation_ajax' ) {
update_option( 'woocommerce_default_customer_address', 'geolocation_ajax' );
}
}
}
// Initialize the plugin
new WC_Country_Based_Discounts();
Step 3: Customize Discounts
In the $country_discounts
array within the plugin, you can specify discount amounts for each country. For example:
$country_discounts = [
'US' => 5, // Discount of $5 for the United States
'IN' => 3, // Discount of $3 for India
'UK' => 7, // Discount of $7 for the United Kingdom
];
Step 4: Test Your Setup
- Activate the plugin from the WordPress admin.
- Use a VPN or geolocation testing tool to simulate visits from different countries.
- Verify that the product prices are adjusted based on the configured discounts.
Optional Enhancements
- Admin Interface: Add a settings page in the WordPress admin to configure discounts without modifying the code.
- Dynamic Discounts: Calculate discounts as percentages of the product price instead of fixed amounts.
- Logging and Analytics: Track geolocation data and discount usage for better insights.
With this implementation, you can dynamically offer country-specific discounts in WooCommerce, enhancing your store’s global appeal and conversion rates. If you have any questions or need further customization, feel free to ask!
Leave a Reply