S
S
Sergey Shilov2017-07-12 17:29:08
PHP
Sergey Shilov, 2017-07-12 17:29:08

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 );

And to display the price, the following:
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 );

As you might guess, both do not work, however, in my opinion, everything is spelled out correctly. What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Shilov, 2017-07-12
@Olivoin

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;
}

Category:
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 question

Ask a Question

731 491 924 answers to any question