How to get the current woocommerce product category

et_the_terms( $post->ID, ‘product_cat’ );
$nterms = get_the_terms( $post->ID, ‘product_tag’ );
foreach ($terms as $term ) {
$product_cat_id = $term->term_id;
$product_cat_name = $term->name;
break;
}
Follow the woocomer steps to import the current product category (3 main steps). The following are the steps taken by woocomer to obtain the current product category.
First get the current product ID. Together with the product_cat classification, pass the product ID as two parameters to the get_the_terms() function. Step 2 returns an array containing all the details. Use the foreach loop to repeat the data to get the required category name or other details of the current product category. Now, just as we pointed out the woommerce redirection example after category based checkout, you can print or use categories in other logic. You have added this code to the hook in the header of the sample site and confirmed that the category has been successfully displayed in the image below.
In order to display categories in the WordPress title, the complete code snippet added to the custom storefront theme is as follows:
add_action(‘wp_head’, ‘get_current_product_category’);
function get_current_product_category(){
global $post;
$terms = get_the_terms( $post->ID, ‘product_cat’ );
$nterms = get_the_terms( $post->ID, ‘product_tag’ );
foreach ($terms as $term ) {
$product_cat_id = $term->term_id;
$product_cat_name = $term->name;
break;
}
echo $product_cat_name;
}
Conclusion in this post, we emphasize how to obtain the current product woocommerce category in stages. As shown in the above example, you only need to know the current product ID. You can use the default WordPress function to get the condition and move it through the ring opposite to the current woocomer category.
How to copy products from woocommerce in similar articles, how to add capcha woocommerce login, and what is the best payment gateway for woocommerce? Method of adding social login in woocommerce social login MailChimp method of integrating with woocommerce all selected methods except the last sub item in CSS? Display the method of woocommerce products according to the example category where CSS is not the last sub item. Add sub category woocommerce category shortcut code woocommerce Display method woocommerce buy one get one set method page add method of woocommerce product woocommerce custom product category page method woocommerce get product brand name method programming method create product method woocommerce product category Title woocommerce delete method woocommerce The method of setting related goods into the same category adds more than 100 Tips on the woocomer settlement page, and the wocomer redirection after check-out of tric and snip ultimate woocomer hide Guide: redirect to the customized audit page, and the wocomer redirection after logout [Ultimate Guide]

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *