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.
When a product is not available for a specific region, its price is normally still visible to the customer. If you would like to hide it, you can do so with a simple filter, like in the example below. To use the code, simply add it to your theme's functions.php file.
function hide_price_for_unavailable_products($price_html, $product) {
$product_purchasable = $product->is_purchasable();
/* Due to how WooCommerce is designed, a product with children may be returned
* as "purchasable" even if none of its children are. This is the case with
* variable products.
* We want to hide prices for variable products that don't have any available
* variation, therefore we have to check each variation's availability.
*/
if($product_purchasable && $product->has_child()) {
$product_purchasable = false;
foreach($product->get_children(true) as $child_product_id) {
$child_product = wc_get_product($child_product_id);
// If any variation is purchasable, then the variable product is
// purchasable as well, and we can stop here
if($child_product->is_purchasable()) {
$product_purchasable = true;
break;
}
}
}
// Hide price if product is not purchasable
if(!$product_purchasable) {
$price_html = '';
}
return $price_html;
}
// Hide price for products that are not purchasable
add_filter('woocommerce_get_price_html','hide_price_for_unavailable_products', 9, 2);
// Optional - Hide price for variations that are not purchasable
add_filter('woocommerce_get_variation_price_html','hide_price_for_unavailable_products', 9, 2);
You can purchase the Prices by Country plugin from our online shop.