Creating custom WooCommerce product shortcodes for displaying recent products, popular products, and selected products is a handy way to extend the functionality of your WooCommerce store.
1. Recent Products Shortcode:
This shortcode will display a specified number of recent products.
function recent_products_shortcode($atts) { $args = shortcode_atts(array( 'number' => 5, // Number of products to display ), $atts); $args_query = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => $args['number'], 'orderby' => 'date', 'order' => 'DESC', ); $products = new WP_Query($args_query); ob_start(); if ($products->have_posts()) { while ($products->have_posts()) { $products->the_post(); wc_get_template_part('content', 'product'); } } else { echo 'No recent products found.'; } wp_reset_postdata(); return ob_get_clean(); } add_shortcode('recent_products', 'recent_products_shortcode');
You can use this shortcode in your posts or pages like this: .
This code defines a custom shortcode in WordPress called ‘recent_products’ that can be used to display a list of recent products from a WooCommerce-powered website. Let’s break down the code step by step:
function recent_products_shortcode($atts) { ... }
: This function is the main shortcode callback. It accepts an array of attributes ($atts
) that can be passed when using the shortcode. In this case, it accepts a single attribute, ‘number’, which specifies the number of recent products to display.$args = shortcode_atts(array(...), $atts);
: This line sets default attributes for the shortcode using theshortcode_atts
function. It combines the provided attributes ($atts
) with default values defined in the array. If the ‘number’ attribute is not provided when using the shortcode, it defaults to displaying 5 products.$args_query = array(...);
: This section defines the arguments for the query used to retrieve recent products. It sets up the query parameters such as post type, post status, number of posts to display, sorting order, and sorting criteria. Here, it queries products of the ‘product’ post type that are published, sorted by date in descending order, and limits the number of products according to the ‘number’ attribute.$products = new WP_Query($args_query);
: This line creates a new instance of theWP_Query
class with the query arguments defined in$args_query
. It’s used to fetch the recent products based on the specified criteria.ob_start();
: This function starts an output buffer. Output buffering is used to capture the HTML generated within the buffer instead of sending it directly to the browser.- The following block of code checks if there are any recent products retrieved by the query:
if ($products->have_posts()) { ... }
: If there are recent products, it enters this block.- Inside the loop,
while ($products->have_posts()) { ... }
, it iterates through each product. the_post();
is used to set up the global post data for the current product.wc_get_template_part('content', 'product');
is used to display the product using the WooCommerce template ‘content-product’.
- If there are no recent products found, it displays the message ‘No recent products found.’
wp_reset_postdata();
: After the loop, this function resets the post data to its original state.return ob_get_clean();
: Finally, the function returns the HTML content generated within the output buffer usingob_get_clean()
. This content will be inserted into the page where the shortcode ‘recent_products’ is used.add_shortcode('recent_products', 'recent_products_shortcode');
: This line registers the ‘recent_products’ shortcode with WordPress, associating it with therecent_products_shortcode
function defined earlier. This allows users to use theshortcode in their posts or pages to display recent products based on the specified criteria.
2. Popular Products Shortcode:
This shortcode will display products based on their popularity (sales).
function popular_products_shortcode($atts) { $args = shortcode_atts(array( 'number' => 5, // Number of products to display ), $atts); $args_query = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => $args['number'], 'meta_key' => 'total_sales', 'orderby' => 'meta_value_num', 'order' => 'DESC', ); $products = new WP_Query($args_query); ob_start(); if ($products->have_posts()) { while ($products->have_posts()) { $products->the_post(); wc_get_template_part('content', 'product'); } } else { echo 'No popular products found.'; } wp_reset_postdata(); return ob_get_clean(); } add_shortcode('popular_products', 'popular_products_shortcode');
You can use this shortcode in your posts or pages like this: [popular_products number="5"]
.
This PHP code defines another custom shortcode in WordPress called ‘popular_products,’ which can be used to display a list of popular products on a WooCommerce-powered website. It’s quite similar to the previous code for displaying recent products, with some differences to query and display popular products. Let’s break down the code step by step:
function popular_products_shortcode($atts) { ... }
: This function is the main shortcode callback, just like in the previous example. It accepts an array of attributes ($atts
) that can be passed when using the shortcode. In this case, it accepts a single attribute, ‘number,’ which specifies the number of popular products to display.$args = shortcode_atts(array(...), $atts);
: This line sets default attributes for the shortcode using theshortcode_atts
function. It combines the provided attributes ($atts
) with default values defined in the array. If the ‘number’ attribute is not provided when using the shortcode, it defaults to displaying 5 products.$args_query = array(...);
: This section defines the arguments for the query used to retrieve popular products. It sets up the query parameters such as post type, post status, number of posts to display, sorting order, sorting criteria, andmeta_key
to sort by total sales.$products = new WP_Query($args_query);
: This line creates a new instance of theWP_Query
class with the query arguments defined in$args_query
. It’s used to fetch the popular products based on the specified criteria.ob_start();
: This function starts an output buffer, similar to the previous example. It’s used to capture the HTML generated within the buffer instead of sending it directly to the browser.- The following block of code checks if there are any popular products retrieved by the query:
if ($products->have_posts()) { ... }
: If there are popular products, it enters this block.- Inside the loop,
while ($products->have_posts()) { ... }
, it iterates through each product. the_post();
is used to set up the global post data for the current product.wc_get_template_part('content', 'product');
is used to display the product using the WooCommerce template ‘content-product.’
- If there are no popular products found, it displays the message ‘No popular products found.’
wp_reset_postdata();
: After the loop, this function resets the post data to its original state.return ob_get_clean();
: Finally, the function returns the HTML content generated within the output buffer usingob_get_clean()
. This content will be inserted into the page where the shortcode ‘popular_products’ is used.add_shortcode('popular_products', 'popular_products_shortcode');
: This line registers the ‘popular_products’ shortcode with WordPress, associating it with thepopular_products_shortcode
function defined earlier. This allows users to use the[popular_products]
shortcode in their posts or pages to display popular products based on the specified criteria, such as total sales.
3. Selected Products Shortcode:
This shortcode will display a specific set of products by specifying their IDs.
function selected_products_shortcode($atts) { $args = shortcode_atts(array( 'ids' => '', // Comma-separated list of product IDs ), $atts); $product_ids = explode(',', $args['ids']); $args_query = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1, 'post__in' => $product_ids, 'orderby' => 'post__in', ); $products = new WP_Query($args_query); ob_start(); if ($products->have_posts()) { while ($products->have_posts()) { $products->the_post(); wc_get_template_part('content', 'product'); } } else { echo 'No selected products found.'; } wp_reset_postdata(); return ob_get_clean(); } add_shortcode('selected_products', 'selected_products_shortcode');
You can use this shortcode in your posts or pages like this: [selected_products ids="1,2,3"]
, where “1,2,3” are the IDs of the products you want to display.
This PHP code defines a custom shortcode in WordPress called ‘selected_products’ that allows you to display a list of selected products by specifying their IDs as a comma-separated list. Let’s break down the code step by step:
function selected_products_shortcode($atts) { ... }
: This function is the main shortcode callback. It accepts an array of attributes ($atts
) that can be passed when using the shortcode. In this case, it accepts a single attribute, ‘ids,’ which is used to specify a comma-separated list of product IDs.$args = shortcode_atts(array(...), $atts);
: This line sets default attributes for the shortcode using theshortcode_atts
function. It combines the provided attributes ($atts
) with default values defined in the array. The ‘ids’ attribute, which is used to specify the product IDs, defaults to an empty string.$product_ids = explode(',', $args['ids']);
: This line takes the ‘ids’ attribute, which is a comma-separated list of product IDs, and splits it into an array of individual product IDs using theexplode
function.$args_query = array(...);
: This section defines the arguments for the query used to retrieve selected products. It sets up the query parameters such as post type, post status, number of posts to display (all of them with-1
), an array of post IDs to include in the query, and sorting criteria to maintain the order of IDs provided.$products = new WP_Query($args_query);
: This line creates a new instance of theWP_Query
class with the query arguments defined in$args_query
. It’s used to fetch the selected products based on the specified criteria.ob_start();
: This function starts an output buffer, which is used to capture the HTML generated within the buffer instead of sending it directly to the browser.- The following block of code checks if there are any selected products retrieved by the query:
if ($products->have_posts()) { ... }
: If there are selected products, it enters this block.- Inside the loop,
while ($products->have_posts()) { ... }
, it iterates through each product. the_post();
is used to set up the global post data for the current product.wc_get_template_part('content', 'product');
is used to display the product using the WooCommerce template ‘content-product.’
- If there are no selected products found, it displays the message ‘No selected products found.’
wp_reset_postdata();
: After the loop, this function resets the post data to its original state.return ob_get_clean();
: Finally, the function returns the HTML content generated within the output buffer usingob_get_clean()
. This content will be inserted into the page where the shortcode ‘selected_products’ is used.add_shortcode('selected_products', 'selected_products_shortcode');
: This line registers the ‘selected_products’ shortcode with WordPress, associating it with theselected_products_shortcode
function defined earlier. This allows users to use the[selected_products]
shortcode in their posts or pages to display a custom list of selected products based on the specified product IDs.
Remember to customize the shortcode templates (wc_get_template_part('content', 'product')
) to match your theme’s design and layout for product listings. These custom shortcodes will allow you to display recent, popular, or selected products on your WooCommerce store pages easily.