Search code examples
phpwordpresswoocommercefacebook-opengraphmeta-tags

Add an OpenGraph tag in WooCommerce single product pages


I have to add Product SKU to Woocommerce metatags product page (Facebook OpenGraph). I've tried to add

global $product;
echo "<meta property='og:id' content='".$product->get_sku()."'/>";

in functions.php file but it does show nothing in content. How i can get SKU of products?

Thanks


Solution

  • Instead use the following to add an opengraph meta tag with the product sku on single products:

    add_action( 'wp_head', 'opengraph_id_single_product_sku', 9 );
    function opengraph_id_single_product_sku(){
        if ( is_product() ) {
            $product = wc_get_product( get_the_ID() );
    
            echo '<meta property="og:id" content="'.$product->get_sku().'" />';
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and Works.