Skip to content

Instantly share code, notes, and snippets.

@suhag10
Last active April 30, 2026 21:22
Show Gist options
  • Select an option

  • Save suhag10/ccdfa995879492d896150a2df9152843 to your computer and use it in GitHub Desktop.

Select an option

Save suhag10/ccdfa995879492d896150a2df9152843 to your computer and use it in GitHub Desktop.

Get Product Category

global $product;

$product_id  = get_the_ID();
$product_cat = get_the_terms( $product_id, 'product_cat' );

echo esc_html(implode(', ', wp_list_pluck($product_cat, 'name')));

// OR

$last_cat = count($product_cat) - 1;
foreach ($product_cat as $key => $cat) {
    printf(
        '<span class="product-category"><a href="%s">%s</a></span>',
        esc_url(get_category_link($cat->term_id)),
        esc_html($cat->name) . ($key !== $last_cat ? ', ' : '')
    );
}

// OR

<span><?php echo _n( 'Category:', 'Categories:', count( $product->get_category_ids() ), 'woocommerce' ); ?></span>
<p class="product-details-category">
	<?php
        echo wc_get_product_category_list(
            $product->get_id(),
            ', '
        );
	?>
</p>

Get Product TAG

<span><?php echo _n( 'Tag:', 'Tags:', count( $product->get_tag_ids() ), 'woocommerce' ); ?> </span>
<?php 
    echo wc_get_product_tag_list(
        $product->get_id(),
        ', ',
        '<p class="tagged_as">',
        '</p>'
    );
?>

Get Product SKU

<span><?php esc_html_e( 'SKU:', 'woocommerce' ); ?> </span>
<p class="sku_wrapper">
    <span class="sku"><?php echo ( $sku = $product->get_sku() ) ? $sku : esc_html__( 'N/A', 'woocommerce' ); ?></span>
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment