'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 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | LoicTheAztec |