Danish Muneer
Danish Muneer

Reputation: 580

Remove structured data from Woocommerce single product pages

I am trying to remove the structured data that Woocommerce adds to the product pages.

I did some research and found that WC_Structured_Data::generate_product_data() generates the structured data markup. It's hooked in the woocommerce_single_product_summary action hook in the woocommerce/templates/content-single-product.php template file.

I tried by adding the following code to the functions.php

remove_action( 'woocommerce_single_product_summary', 'WC_Structured_Data::generate_product_data()', 60 );

So structured data wouldn't be added by Woocommerce, but it doesn't work

Am I doing something wrong? Is there another way to do what I am trying to achieve?

Upvotes: 1

Views: 4252

Answers (4)

LoicTheAztec
LoicTheAztec

Reputation: 254363

Instead, you can use dedicated filter hook 'woocommerce_structured_data_product' that is located in WC_Structured_Data for generate_product_data() method nulling the structured data output in single product pages:

add_filter( 'woocommerce_structured_data_product', 'structured_data_product_nulled', 10, 2 );
function structured_data_product_nulled( $markup, $product ){
    if( is_product() ) {
        $markup = [];
    }
    return $markup;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Upvotes: 4

alari
alari

Reputation: 11

Add to functions.php:

add_action('wp_loaded', function() {
    remove_action('woocommerce_single_product_summary', [$GLOBALS['woocommerce']->structured_data, 'generate_product_data'], 60);   
});

Unhooks WC_Structured_Data::generate_product_data(). Will not waste resources on generating product data first for no reason and then "nulling" that same generated data a moment later using a filter.

Upvotes: 0

František Heča
František Heča

Reputation: 425

This is how you can remove hooks associated with instantiated object method. You have to find the variable that holds the new Object instance.

In this case the main WooCommerce object is accessible as $GLOBALS['woocommerce'] and it has public property $structured_data which holds an instance of the WC_Structured_Data object.

Hence, to remove the hook in the question you can write this code:

remove_action( 'woocommerce_before_main_content', array( $GLOBALS['woocommerce']->structured_data, 'generate_website_data' ), 30 );

Upvotes: 0

Bjorn
Bjorn

Reputation: 759

I suspect people want to remove the default tabs and they come here after they see the Woocommerce content-single-product.php template. In this template you see that generate_product_data() is hooked with priority 60.

After inspecting the hooks that run on woocommerce_single_product_summary.

You can easily remove the tabs with:

remove_action( 'woocommerce_single_product_summary', 'woocommerce_output_product_data_tabs', 60 );

I think Woocommerce forgot to mention this add_action.

Upvotes: 0

Related Questions