- This topic has 8 replies, 3 voices, and was last updated 5 years, 4 months ago by
tractor-1.
-
AuthorPosts
-
May 9, 2020 at 3:32 pm #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
May 9, 2020 at 4:59 pm #1277315Leo
StaffCustomer SupportHi 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 🙂
May 9, 2020 at 7:21 pm #1277368tractor-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
May 10, 2020 at 3:29 am #1277585David
StaffCustomer SupportHi 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"]
May 10, 2020 at 4:55 pm #1278381tractor-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
May 11, 2020 at 3:40 am #1278791David
StaffCustomer SupportNovel 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 ofdummy
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 ); }
May 11, 2020 at 3:45 pm #1279915tractor-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
May 12, 2020 at 1:52 am #1280393David
StaffCustomer SupportGive 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 ); }
May 12, 2020 at 1:49 pm #1281475tractor-1
That worked. You’re awesome!
Huge thanks for the whole thing. -
AuthorPosts
- You must be logged in to reply to this topic.