[Resolved] Create a "View All" link with product count

Home Forums Support Create a "View All" link with product count

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1277259
    tractor-1

    Hi,

    I’m creating a 10-product preview for each product category using shortcodes on my custom frontpage and need a seamless “View All (product count here)” link to the respective category at the end of the product list in the previews like this Screenshot.

    Is there any plugin that does this?
    Any help, snippet, or clues would be highly appreciated.

    Thanks

    #1277315
    Leo
    Staff
    Customer Support

    Hi there,

    I’m not aware of a plugin that offers this option unfortunately.

    Can you link me to the example site in question?

    I can try to see if they are using a plugin or not.

    Let me know 🙂

    #1277368
    tractor-1

    I can’t find an option to make the link private, so hoping it would be okay to post it like this.

    ** Admin moved to Site URL Field

    #1277585
    David
    Staff
    Customer Support

    Hi there,

    couldn’t find any specific plugin to do that or a way of making the existing Woo shortcodes do it either. But did cobble this PHP Snippet together:

    add_shortcode( 'products-counter', 'products_counter' );
    function products_counter( $atts ) {
        $atts = shortcode_atts( [
            'category' => '',
        ], $atts );
    
        $taxonomy = 'product_cat';
        if ( is_numeric( $atts['category'] ) ) {
            $cat = get_term( $atts['category'], $taxonomy );
        } else {
            $cat = get_term_by( 'slug', $atts['category'], $taxonomy );
        }
    
        $catName = $cat->name;
        $catLink = get_term_link( $cat , $taxonomy );
        $catCount = $cat->count;
    
        if ( $cat && ! is_wp_error( $cat ) ) {
            return '<a class="category-counter" href="' . $catLink . '">' . $catName . '<span class"count">(' . $catCount . ')<span></a>';
        }
        return '';
    }

    Now you have a shortcode for displaying Category name, link and counter eg.

    [products-counter category="albums"]

    #1278381
    tractor-1

    Thank you so much for the snippet.

    I was using WooCommerce shortcode [product_categories ids="xyz"] to output category links with product count and then hiding the “category image” with CSS. Your nice snippet saved me the CSS step.

    Just a little more help will make things almost perfect for me.

    I use the shortcode [products ids="835,836,838,839,1231,(my-dummy-product-id)" columns="1" orderby="popularity" ] to get a list of handpicked products from a category where the last product id (my-dummy-product-id) is a dummy product that only mimics a product card and lets me use the shortcode you coded in its “short description” .

    I enabled the short product description, and rendering of shortcodes in it, on my homepage using this:

    add_action( 'woocommerce_after_shop_loop_item', 'frontpage_shortcodes_short_description', 6 );
    function frontpage_shortcodes_short_description() {
        global $product;
        if ( is_home() || is_page('my-frontpage-products') ) {
            $wc_product = wc_get_product( $product );
            if ( ! $wc_product ) {
                return false;
        }
            $short_description = $wc_product->get_short_description();
            if ( '' !== $short_description ) {
            echo '<div itemprop="description">' . do_shortcode( wpautop( wptexturize( $short_description ) ) ) . '</div>';
            }
        }
    }

    Problem is it enabled the short product description for all the products when I only need it for specific ids (dummy products). How to get it to do that?

    Thanksss

    #1278791
    David
    Staff
    Customer Support

    Novel idea using a dummy product – in that case you could scrap the shortcode altogether and do something like this.
    Give all your Dummy products a Tag of dummy for example.
    Then in your hook you can get the category links if the term exists like so:

    if ( has_term( 'dummy', 'product_tag' ) ) {
        $cat = get_the_terms( $post->ID, 'product_cat' )[0];
    
        printf(
            '<a class="category-counter" href="%1$s">%2$s <span class="counter">(%3$s)<span></a>',
    	$catLink = get_term_link( $cat , $taxonomy ),
            $catName = $cat->name,
            $catCount = $cat->count
        );
        
    }
    #1279915
    tractor-1

    Now this method is much simpler except the snippet doesn’t include the products in subcategories when counting. 🙁
    How to get it to give the total count of all the products in a parent category + its subcategories?

    Thank you

    #1280393
    David
    Staff
    Customer Support

    Give this a shot:

    if ( has_term( 'dummy', 'product_tag' ) ) {
    
        // Get Parent Category and its count
        $taxonomy = 'product_cat';
        $cat = get_the_terms( $post->ID, $taxonomy )[0];
        $count = (int) $cat->count;
    
        // Loop through children categories
        $child_cats = get_term_children( $cat->term_id, $taxonomy);
        foreach ($child_cats as $child_cat) {
            $term = get_term( $child_cat, $taxonomy );
            $count +=$term->count;
        }
        
        printf(
            '<a class="category-counter" href="%1$s">%2$s <span class="counter">(%3$s)<span></a>',
            $catLink = get_term_link( $cat , $taxonomy ),
            $catName = $cat->name,
            $count
        );
        
    } 
    #1281475
    tractor-1

    That worked. You’re awesome!
    Huge thanks for the whole thing.

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.