- This topic has 5 replies, 2 voices, and was last updated 2 years, 7 months ago by
Leo.
-
AuthorPosts
-
February 20, 2023 at 12:26 pm #2540352
roadlink
I want to list best seller products’ title and price of that category under the bottom of the category page.
I know I should be able to do it with hook element but couldn’t find right code.
I tried few plugins including this one. https://wordpress.org/plugins/woo-best-selling-products/But these are not good till now. I want to have as simple as possible. Name and Price. Something like this;
Best seller cell phones (Cell Phones are a category name here)
1. iphone 13 pro max – 1299 USD
2. Samsung galaxy s22 – 999 USD 3..Asked about it in wordpress forums but no update yet.
February 20, 2023 at 12:33 pm #2540358Leo
StaffCustomer SupportHi there,
We cannot help with the actual content part as that’s completely handled by WooCommerce and unrelated to the theme/GP.
I would say that WooCommerce Blocks or shortcode is your best bet:
https://wordpress.org/plugins/woo-gutenberg-products-block/
https://woocommerce.com/document/woocommerce-shortcodes/Then you can use a hook element or block element hook to add the content with WooCommerce hooks:
https://www.businessbloomer.com/woocommerce-visual-hook-guide-archiveshopcat-page/Hope this helps 🙂
February 20, 2023 at 1:02 pm #2540377roadlink
Hi Leo,
I asked chatgpt and it gave me exactly what I want 🙂
Thanks for reply.// Get top 5 selling products for current category or archive page function get_top_selling_products() { $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'meta_key' => 'total_sales', 'orderby' => 'meta_value_num', 'posts_per_page' => 5 ); if (is_product_category()) { $product_categories = array(get_queried_object()->slug); } else if (is_post_type_archive('product')) { $product_categories = array(); } else { return; } $top_selling_products = array(); foreach ($product_categories as $category) { $args['tax_query'] = array( array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $category ) ); $products = new WP_Query($args); if ($products->have_posts()) { while ($products->have_posts()) { $products->the_post(); $top_selling_products[$category][] = array( 'title' => get_the_title(), 'price' => get_post_meta(get_the_ID(), '_regular_price', true), 'permalink' => get_permalink() ); } } wp_reset_postdata(); } return $top_selling_products; } // Display top 5 selling products for current category or archive page function display_top_selling_products() { $top_selling_products = get_top_selling_products(); if (!$top_selling_products) { return; } $output = ''; foreach ($top_selling_products as $category_slug => $products) { $category_name = get_term_by('slug', $category_slug, 'product_cat')->name; $output .= '<h2>' . $category_name . '</h2>'; foreach ($products as $product) { $output .= '<p><a href="' . $product['permalink'] . '">' . $product['title'] . '</a> - ' . $product['price'] . '</p>'; } } return $output; } // Add shortcode to call the display_top_selling_products function function top_selling_products_shortcode() { return display_top_selling_products(); } add_shortcode('top_selling_products', 'top_selling_products_shortcode');
You can now use the [top_selling_products] shortcode anywhere in your content to display the top selling products for each category.
February 20, 2023 at 1:03 pm #2540379Leo
StaffCustomer SupportAwesome! Thanks for sharing 🙂
February 20, 2023 at 2:36 pm #2540448roadlink
My next step is create accordion with these content but I couldn’t figure out how to add shortcodes to generateblocks’ new according block title.
February 20, 2023 at 2:55 pm #2540467Leo
StaffCustomer SupportPlease open a support ticket in GB pro’s ticket system:
https://generateblocks.com/support/Thanks!
-
AuthorPosts
- You must be logged in to reply to this topic.