Answer the question
In order to leave comments, you need to log in
How to display product category and its price in custom loop in woocommerce?
Good afternoon! I display products according to my own template and stumble a little on insufficient knowledge of php.
To output product categories in a loop, I use the following code:
function tutsplus_product_subcategories() {
global $product;
$product_category = $product->get_categories;
echo '
<div class="product-cat">
' . $product_category . '
</div>
';
}
add_action( 'woocommerce_after_shop_loop_item_title', 'tutsplus_product_subcategories', 50 );
function woocommerce_template_loop_price(){
global $product;
$product_id = $product->id;
$product = wc_get_product( $product_id );
echo '
<div class="product-price">
'. $product->get_price_html .'
</div>
';
}
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_price', 50 );
Answer the question
In order to leave comments, you need to log in
Understood.
Price:
function woocommerce_template_loop_price(){
global $product;
$product = get_product( get_the_ID() );
if ( $price_html = $product->get_price_html() ) :
echo '
<div class="product-price">
'. $price_html .'
</div>
';
endif;
}
function tutsplus_product_subcategories() {
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 '
<div class="product-cat">
' . $product_cat_name . '
</div>
';
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question