Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #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

    #1097855

    Hey 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

    #1098149

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

    #1098156

    oops 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)…

    #1098650

    Hi,

    Why not you try convert this as a shortcode and then import it?

    Best regards,
    Basilis

    #1100203

    Good 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!!

    #1101120

    nevermind figures it out on my own. Thanks alot for your help.

    #1101224

    Hi,

    Great! We’ll close the thread now. Please feel free to open a new thread if you need anything else.

    Have a nice day.

    Best regards,
    Ismael

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘stars & number of reviews below title – single product page using adv editor’ is closed to new replies.