Home » WooCommerce: Hide Product Price & Stock From Google

WooCommerce: Hide Product Price & Stock From Google

by Tutor Aspire

The WooCommerce Plugin is also developed with SEO in mind and provides your website with the schema markup for products (as well as other microdata useful for search engines).

This means by default your products are going to show on Google together with other data such as review stars, stock status, number of reviews and – you saw that coming – the product price.

In certain case scenario, however, you may want to hide WooCommerce product prices from Google search results (and all the other search engines of course). For example, because your prices are only visible to logged in users; or maybe because you don’t want to display your prices until potential customers go to your website and read all the product benefits as opposed to having them make a price-only decision.

Either way, let’s see how it’s done. And once again, it’s one line of code. Enjoy!

These are 3 products for sale on WooCommerce.com. By default, their price is visible within the search result. In this tutorial we’ll see how to hide it!

PHP Snippet: Remove WooCommerce Product Prices From Google Search Results

/**
 * @snippet       Hide Price & Stock @ Google
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire, BusinessBloomer.com
 * @testedwith    WooCommerce 6
 * @donate $9     https://tutoraspire.com
 */

add_filter( 'woocommerce_structured_data_product_offer', '__return_empty_array' );

That’s it!

Please note, here’s the full code from WooCommerce core in case you want to manipulate it in a more specific way e.g. only the price:

if ( '' !== $product->get_price() ) {
// Assume prices will be valid until the end of next year, unless on sale and there is an end date.
$price_valid_until = gmdate( 'Y-12-31', time() + YEAR_IN_SECONDS );

if ( $product->is_type( 'variable' ) ) {
$lowest  = $product->get_variation_price( 'min', false );
$highest = $product->get_variation_price( 'max', false );

if ( $lowest === $highest ) {
$markup_offer = array(
'@type'              => 'Offer',
'price'              => wc_format_decimal( $lowest, wc_get_price_decimals() ),
'priceValidUntil'    => $price_valid_until,
'priceSpecification' => array(
'price'                 => wc_format_decimal( $lowest, wc_get_price_decimals() ),
'priceCurrency'         => $currency,
'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false',
),
);
} else {
$markup_offer = array(
'@type'      => 'AggregateOffer',
'lowPrice'   => wc_format_decimal( $lowest, wc_get_price_decimals() ),
'highPrice'  => wc_format_decimal( $highest, wc_get_price_decimals() ),
'offerCount' => count( $product->get_children() ),
);
}
} else {
if ( $product->is_on_sale() && $product->get_date_on_sale_to() ) {
$price_valid_until = gmdate( 'Y-m-d', $product->get_date_on_sale_to()->getTimestamp() );
}
$markup_offer = array(
'@type'              => 'Offer',
'price'              => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ),
'priceValidUntil'    => $price_valid_until,
'priceSpecification' => array(
'price'                 => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ),
'priceCurrency'         => $currency,
'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false',
),
);
}

$markup_offer += array(
'priceCurrency' => $currency,
'availability'  => 'http://schema.org/' . ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ),
'url'           => $permalink,
'seller'        => array(
'@type' => 'Organization',
'name'  => $shop_name,
'url'   => $shop_url,
),
);

$markup['offers'] = array( apply_filters( 'woocommerce_structured_data_product_offer', $markup_offer, $product ) );
}

You may also like