M
M
mischael12018-10-11 09:35:37
WooCommerce
mischael1, 2018-10-11 09:35:37

How to display custom attribute after product name on shop page in woocommerce?

There is a solution to the problem, but not a complete one.
Add code to functions.php

function my_template_loop_product_title(){
    global $product;
    echo '<h3 itemprop="name" class="product_title entry-title">';
    $versionvalues = get_the_terms( $product->id, 'pa_model');
 
    foreach ( $versionvalues as $versionvalue ) {
         echo $versionvalue->name;
    }
    echo '</h3>';
}
add_action( 'woocommerce_shop_loop_item_title', 'my_template_loop_product_title', 10 );

But this code must be inserted in the place where the attribute should be displayed:
<?php
 
$subheadingvalues = get_the_terms( $product->id, 'pa_model');
 
if ($subheadingvalues): ?>
<b>Название:<b>
    <?php foreach ( $subheadingvalues as $subheadingvalue ): ?>
           <?php echo $subheadingvalue->name; ?>
     <?php endforeach; ?>
<?php endif; ?>

But where is this place?
In woocommerce source codes (archive-product.php, content-product.php ) output is done via do_action( 'woocommerce_before_shop_loop_item_title' ); and how to insert the necessary code between them is not clear.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pychev Anatoly, 2018-10-11
@mischael1

But this code must be inserted in the place where the attribute should be displayed:

so it is already inserted in your "my_template_loop_product_title" function.
You don't need to go into any files. You connect to the required section of the code through a hook, which is what you did above.
And here you can visually see where the hook is. It helps me

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question