phpwordpresswoocommercetaxonomy-terms

Get product category and tag terms as meta keyword in WooCommerce


I have been using below code for using Tags & categories as META Keywords for my wordpress posts.

function wcs_add_meta_keywords() {
global $post;
if ( is_single() ) {
    $cats = get_the_category( $post->ID );
    $tags = get_the_tags( $post->ID );
    $keywords = '';
    foreach ( $cats as $cat ) {
        $keywords .= $cat->cat_name . ", ";
    }
    foreach ( $tags as $tag ) {
        $keywords .= $tag->name . ", ";
    }
    echo '<meta name="keywords" content="' . $keywords . '" />' . "\n";
}}add_action( 'wp_head', 'wcs_add_meta_keywords' , 2 );

and below code for using Product description as META description.

function wcs_add_meta_description_tag() {
global $post;
if ( is_single() ) {
    $meta = strip_tags( $post->post_content );
    $meta = strip_shortcodes( $post->post_content );
    $meta = str_replace( array("\n", "\r", "\t"), ' ', $meta );
    $meta = mb_substr( $meta, 0, 125, 'utf8' );
    echo '<meta name="description" content="' . $meta . '" />' . "\n";
}}add_action( 'wp_head', 'wcs_add_meta_description_tag' , 2 );

But now i want to achieve the same for my products in woocommerce. I have learnt and came to know that woocommerce use taxonomies instead so i tried using get_terms() and product_tag, product_cat in place of get_the_category and get_the_tag. But it does not work.

Can anyone help with the correct usage of the variables for the both codes.

thanks in advance


Solution

  • For WordPress and WooCommerce term taxonomies on single post (or custom post), you can better use wp_get_post_terms(), which allows the "fields" argument to target term "names", so the code will be more compact and efficient:

    For both WooCommerce and Wordpress you will use:

    add_action( 'wp_head', 'wcs_add_meta_keywords' , 2 );
    function wcs_add_meta_keywords() {
        // For WordPress single posts with categories and tags
        if ( is_single() && ! is_product() ) {
            $cats = (array) wp_get_post_terms( get_the_id(), 'category', array('fields' => 'names') );
            $tags = (array) wp_get_post_terms( get_the_id(), 'post_tag', array('fields' => 'names') );
        }
        //  For WooCommerce single product (product categories and product tags)
        elseif ( is_product() ) {
            $cats = (array) wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'names') );
            $tags = (array) wp_get_post_terms( get_the_id(), 'product_tag', array('fields' => 'names') );
        }
        if ( ! empty( $cats ) || ! empty( $tags ) ){
            echo '<meta name="keywords" content="' . implode( ', ', array_merge( $cats, $tags ) ) . '" />' . "\n";
        }
    }
    

    For WooCommerce only use:

    add_action( 'wp_head', 'wcs_add_meta_keywords', 2);
    function wcs_add_meta_keywords() {
        if ( is_product() ) {
            $product_cats = (array) wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'names') );
            $product_tags = (array) wp_get_post_terms( get_the_id(), 'product_tag', array('fields' => 'names') );
        }
        if ( ! empty( $product_cats ) || ! empty( $product_tags ) ){
            echo '<meta name="keywords" content="' . implode( ', ', array_merge( $product_cats, $product_tags ) ) . '" />' . "\n";
        }
    }
    

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