Developing a Dynamic Multi-Regional Pricing Plugin for WooCommerce

Case Study: Developing a Dynamic Multi-Regional Pricing Plugin for WooCommerce

Getting your Trinity Audio player ready...

Developing a Dynamic Multi-Regional Pricing Plugin for WooCommerce


Objective: Create a WooCommerce plugin to enable dynamic, multi-regional pricing for a single product based on the user’s location.
Plugin Name: MoizWooCommerce Multiple Price

Overview

In the competitive world of eCommerce, one-size-fits-all pricing can often be a limitation, especially when businesses cater to customers from diverse geographical regions. The challenge is to create a pricing solution that can dynamically adjust based on the user’s location without requiring separate products for each region. This is particularly crucial for services where regional pricing can vary significantly due to local market conditions.


photo credit: freepik

Our client, approached us to develop a WooCommerce plugin that would offer dynamic pricing based on the customer’s location for regions such as the USA, UK/Europe, and Pakistan. This plugin would not only display the correct price but also adjust the currency symbol accordingly. Here’s a deep dive into the challenges we faced and the solutions we implemented to build a robust, user-friendly solution.


Goals and Requirements

The primary requirements of the plugin were as follows:

  1. Dynamic Price Display: Display different prices based on the customer’s country, specifically for the USA, UK/Europe, and Pakistan.
  2. Currency Symbol Adjustment: Show the correct currency symbol (e.g., $, £, or PKR) alongside the price for a localized experience.
  3. Comprehensive Coverage: Ensure that the pricing is correctly displayed across all WooCommerce views, including product pages, shop pages, cart, checkout, invoices, backend order details, and email notifications.
  4. Custom Shortcode: Create a shortcode that outputs the regional price format, such as $30 / hr, for easy insertion in other content areas of the site.

Challenges and Solutions

Challenge 1: Dynamic Location-Based Pricing

WooCommerce’s default setup allows for only one price per product. Since this plugin required three different prices for the same product, we had to create custom fields to store and retrieve prices based on the user’s location.

Solution:
We added custom price fields for each region (USA, UK/Europe, and Pakistan) on the WooCommerce product edit page. These fields allowed the store administrator to input unique prices for each region. To make this dynamic, we leveraged WooCommerce’s built-in WC_Geolocation class, which detects the user’s country based on IP address, enabling us to retrieve and display the correct price based on the user’s location.

Code Implementation:

add_action('woocommerce_product_options_pricing', 'moiz_add_custom_price_fields');
function moiz_add_custom_price_fields() {
    woocommerce_wp_text_input(array(
        'id' => '_price_usa',
        'label' => __('Price for USA ($)', 'woocommerce'),
        'data_type' => 'price',
    ));
    woocommerce_wp_text_input(array(
        'id' => '_price_uk',
        'label' => __('Price for UK/Europe (£)', 'woocommerce'),
        'data_type' => 'price',
    ));
    woocommerce_wp_text_input(array(
        'id' => '_price_pk',
        'label' => __('Price for Pakistan (PKR)', 'woocommerce'),
        'data_type' => 'price',
    ));
}

This approach allowed the plugin to fetch the appropriate price for the user based on their IP location.


Challenge 2: Displaying Dynamic Prices Across WooCommerce Views

Displaying the correct price on the product page was only part of the solution. The plugin also needed to display the dynamic prices across various views, including the cart, checkout, emails, and backend order details.

Solution:
We utilized WooCommerce’s filters and hooks, such as woocommerce_product_get_price, woocommerce_product_get_regular_price, and woocommerce_cart_item_price, to adjust the price output dynamically based on the user’s location. We also made sure to update the cart items to reflect the correct price, ensuring a seamless experience for customers from different regions.

Example Code:

add_filter('woocommerce_product_get_price', 'moiz_get_custom_price', 10, 2);
function moiz_get_custom_price($price, $product) {
    $country_code = moiz_get_country_code();
    if ($country_code === 'US') { $price = get_post_meta($product->get_id(), '_price_usa', true); }
    elseif ($country_code === 'GB' || $country_code === 'EU') { $price = get_post_meta($product->get_id(), '_price_uk', true); }
    elseif ($country_code === 'PK') { $price = get_post_meta($product->get_id(), '_price_pk', true); }
    return $price;
}

This ensured that the correct prices were displayed across the WooCommerce store, including in emails, invoices, and backend views.


Challenge 3: Adjusting the Currency Symbol Dynamically

While displaying different prices was a core requirement, displaying the correct currency symbol alongside the price was equally crucial. WooCommerce uses a global currency setting, which meant we had to override the currency symbol dynamically.

Solution:
We created a filter for woocommerce_currency_symbol to set the currency symbol based on the detected country code. By combining this with woocommerce_currency, we could ensure that the correct currency was displayed on all relevant pages, including product pages, cart, and checkout.

Code Implementation:

add_filter('woocommerce_currency_symbol', 'moiz_custom_currency_symbol', 10, 2);
function moiz_custom_currency_symbol($currency_symbol, $currency) {
    $country_code = moiz_get_country_code();
    switch ($country_code) {
        case 'US': $currency_symbol = '$'; break;
        case 'GB': case 'EU': $currency_symbol = '£'; break;
        case 'PK': $currency_symbol = 'PKR'; break;
    }
    return $currency_symbol;
}

This provided customers with a more localized experience, displaying £, PKR, or $ depending on their location.


Challenge 4: Creating a Shortcode for Flexible Display

To allow the store owner to insert a dynamically formatted price string (e.g., $30 / hr) anywhere on the site, we needed a shortcode that would output this information based on the user’s location.

Solution:
We created a shortcode [price_per_hour] that generates the correct price per hour string based on the user’s location. This shortcode can be placed in pages, posts, widgets, or anywhere shortcode support is available.

Code Implementation:

function moiz_price_per_hour_shortcode() {
    $default_product_id = 123; // Replace with a valid product ID
    return moiz_get_price_per_hour_string($default_product_id);
}
add_shortcode('price_per_hour', 'moiz_price_per_hour_shortcode');

Results

The plugin successfully met all requirements, providing a dynamic pricing solution that adapts to user location, displays appropriate currency symbols, and supports flexible display via a shortcode. Key benefits achieved include:

  1. Improved Localization: The plugin offers a localized experience by displaying prices and currency symbols familiar to users from different regions.
  2. Ease of Use for Store Owners: Store administrators can set prices for each region within the product edit screen, making it easy to manage regional pricing.
  3. Enhanced Flexibility with Shortcodes: The shortcode feature allows the store owner to easily display the price per hour in different contexts across the site.

Conclusion

Building the MoizWooCommerce Multiple Price plugin posed several challenges, from dynamically adjusting prices and currency symbols to ensuring compatibility across various WooCommerce views. By leveraging WooCommerce’s built-in geolocation features, hooks, and filters, we successfully created a solution that enhances the shopping experience for international customers.

This project exemplifies how custom WooCommerce solutions can address specific business needs, helping clients like MoizWordPress provide a tailored and professional eCommerce experience. The MoizWooCommerce Multiple Price plugin stands as a robust solution for businesses seeking multi-regional pricing without the complexity of managing separate products for each region.

This case study highlights the importance of adapting eCommerce strategies to meet the demands of a global customer base, and we’re proud to have developed a solution that aligns with MoizWordPress’s vision for an optimized, user-friendly WooCommerce store.

Similar Posts

Leave a Reply