The code in this article is to be considered a customisation. As such, it's outside the scope of our support service. Should you need assistance implementing the code, please feel free to contact us to request a consultation.


The Prices by Country plugin allows you to specify that one or more products are not available for purchase for customers in specific regions. When a product is unavailable, a notice is displayed where the "Add to cart" button would normally appear, to informing the customer. The default notice shows the following message: <Product title> is not available to <Country>.


In some cases, one might want to add instructions to contact another supplier, or to send an email. The Prices by Country plugin allows to customise the message to show more information, by implementing a filter for the wc_aelia_pbc_product_unavailable_to_country_notice hook. The filter should return the HTML that shows the desired message, and it can contain two placeholders:

  • {country_name}: this token will be replaced by the country selected by the customer (or detected automatically).
  • {product_title}: this token will be replaced by the product title/name.

Below is an example of how to implement the filter to customise the product unavailability message. To use the code, simply add it to your theme's functions.php file. See How to Safely Add Custom Code to WordPress Sites for more information on how to do that.


1. Customise the product unavailability message

/**
 * Replaces the product unavailability notice with some custom HTML.
 *
 * @param string message_html The original message. It contains the following HTML.
 *
 * Simple products
 * <span class="product_name">{product_title}</span> is not available to <span class="country">{country_name}</span>.
 *
 * Variations
 * This option is not available to <span class="country">{country_name}</span>.
 * 
 * @param WC_Product product A product, which is unavailable for the specified
 * country.
 * @param string country A country code.
 * @return string An HTML string that will replace the original message.
 * @author Aelia <support@aelia.co>
 */
function custom_product_unavailability_notice($message_html, $product, $country) {
  /* Let's replace the original message with an invitation to get in touch. The
   * tokens in the message (i.e. {product_title} and {country_name}) will be
   * replaced automatically with the appropriate values before the message is
   * displayed to the customer.
   */
  $message_html = 'We are sorry, we cannot deliver <span class="product_name">' .
                  '{product_title}</span> to <span class="country">{country_name}</span>. ' .
                  'Please <a href="//aelia.co/contact">contact us by email</a>, or call us ' .
                  'at 0123456789 and we will put in contact with one of our local distributors';
  
  return $message_html;
}
add_filter('wc_aelia_pbc_product_unavailable_to_country_notice', 'custom_product_unavailability_notice', 10, 3);

 

2. Hide prices for unavailable products

In some cases, it might be desirable to hide prices for products that are not available to a specific region or country. The following code will check each product's availability, and hide prices for products that current customer can't purchase.

/**
 * Hides the price on the catalogue and product page for unavailable products.
 *
 * @param string price An HTML string containing a formatted price.
 * @param WC_Product product A product instance.
 * @return string The original price, or an empty string if the product is not
 * purchasable.
 * @author Aelia <support@aelia.co>
 */
function aelia_hide_prices_for_unavailable_products($price, $product) {
  if(!$product->is_purchasable()) {
    $price = '';  
  }
  return $price;
}
add_filter('woocommerce_get_price_html', 'aelia_hide_prices_for_unavailable_products', 10, 2);
add_filter('woocommerce_variable_sale_price_html', 'aelia_hide_prices_for_unavailable_products', 10, 2);
add_filter('woocommerce_variable_price_html', 'aelia_hide_prices_for_unavailable_products', 10, 2);


You can purchase the Prices by Country plugin from our online shop.