-
AuthorPosts
-
May 6, 2019 at 10:07 pm #1097835
I am building single product page with advanced layout editor but how do I get stars and number of reviews to appear under product title?
THANKS
May 7, 2019 at 12:18 am #1097855Hey whtmsting1,
Add this into functions.php:
add_action('<a href='https://refer.wordpress.com/r/84/woocommerce/' target='_blank' rel="nofollow">woocommerce</a>_after_shop_loop_item', 'my_print_stars' ); function my_print_stars(){ global $wpdb; global $post; $count = $wpdb->get_var(" SELECT COUNT(meta_value) FROM $wpdb->commentmeta LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID WHERE meta_key = 'rating' AND comment_post_ID = $post->ID AND comment_approved = '1' AND meta_value > 0 "); $rating = $wpdb->get_var(" SELECT SUM(meta_value) FROM $wpdb->commentmeta LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID WHERE meta_key = 'rating' AND comment_post_ID = $post->ID AND comment_approved = '1' "); if ( $count > 0 ) { $average = number_format($rating / $count, 2); echo '<div class="starwrapper" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">'; echo '<span class="star-rating" title="'.sprintf(__('Rated %s out of 5', '<a href='https://refer.wordpress.com/r/84/woocommerce/' target='_blank' rel="nofollow">woocommerce</a>'), $average).'"><span style="width:'.($average*16).'px"><span itemprop="ratingValue" class="rating">'.$average.'</span> </span></span>'; echo '</div>'; } }
Note that you may need to change woocommerce_after_shop_loop_item to a different hook depending on your design and where exactly you want the stars to show up.
This page lists WooCommerce action hooks: http://wcdocs.woothemes.com/codex/extending/hooks/
Best regards,
Jordan Shannon- This reply was modified 5 years, 7 months ago by Jordan Shannon.
May 7, 2019 at 5:57 pm #1098149Thanks Jordan – woo action hooks page examples was not found… Would love to take a look. I added the function and an extra echo to see where it is placed but although it didnt break my page I can’t see that it is working.
Maybe because I am using advanced editor so loop is not being called? Can this hook into the Plugin additions for ALB?
Below is revised code and link to page (changed a couple of apostrophes to quotes in function)…
May 7, 2019 at 6:23 pm #1098156oops I was logged in as another user so will redo this post…
Thanks Jordan – woo action hooks page examples was not found… Would love to take a look. I added the function and an extra echo to see where it is placed but although it didnt break my page I can’t see that it is working.
Maybe because I am using advanced editor so loop is not being called? Can this hook into the Plugin additions for ALB?
Below is revised code and link to page (changed a couple of apostrophes to quotes in function)…
May 8, 2019 at 9:59 pm #1098650Hi,
Why not you try convert this as a shortcode and then import it?
Best regards,
BasilisMay 13, 2019 at 6:36 pm #1100203Good suggestion. I’m almost there. I have stars showing but I need the rest – how do I get something like “(3 customer reviews)” added after the stars – so number of reviews that is clickable to the review section. Shortcode is below in private content along with page and password. THANK YOU!!
May 15, 2019 at 4:55 pm #1101120nevermind figures it out on my own. Thanks alot for your help.
May 16, 2019 at 5:05 am #1101224 -
AuthorPosts
- The topic ‘stars & number of reviews below title – single product page using adv editor’ is closed to new replies.