Last updated on October 18, 2023

WooCommerce product recommendation plugins

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Improve user experience with WooCommerce recommendations.

A WooCommerce product recommendation plugin can be a valuable addition to an online store as it helps to increase sales by suggesting relevant products to customers. Here’s an example of how you can create a simple product recommendation plugin for WooCommerce:

/**
 * Plugin Name: WooCommerce Product Recommendations
 * Description: Adds a product recommendation section to WooCommerce product pages.
 * Version: 1.0.0
 * Author: Your Name
 * Author URI: https://yourwebsite.com
 */

// Add a new section to the WooCommerce product page
function wpsnippets_product_recommendation_section() {
    // Retrieve the recommended products
    $recommended_products = wpsnippets_get_recommended_products();

    // Display the recommended products
    if ( ! empty( $recommended_products ) ) {
        echo '<div class="product-recommendation-section">';
        echo '<h2>Recommended Products</h2>';
        echo '<ul>';
        foreach ( $recommended_products as $product ) {
            echo '<li><a href="' . get_permalink( $product->ID ) . '">' . get_the_title( $product->ID ) . '</a></li>';
        }
        echo '</ul>';
        echo '</div>';
    }
}
add_action( 'woocommerce_after_single_product_summary', 'wpsnippets_product_recommendation_section', 15 );

// Retrieve recommended products based on current product category
function wpsnippets_get_recommended_products() {
    global $product;

    // Get the current product category
    $categories = wp_get_post_terms( $product->get_id(), 'product_cat', array( 'fields' => 'ids' ) );

    // Query recommended products
    $args = array(
        'post_type'      => 'product',
        'post_status'    => 'publish',
        'posts_per_page' => 5,
        'orderby'        => 'rand',
        'tax_query'      => array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'term_id',
                'terms'    => $categories,
            ),
        ),
        'post__not_in'   => array( $product->get_id() ),
    );

    $query = new WP_Query( $args );

    // Return the recommended products
    return $query->get_posts();
}

This code creates a simple WooCommerce product recommendation plugin that adds a new section to the WooCommerce product page. The section displays a list of recommended products based on the current product’s category. The recommended products are retrieved using the wpsnippets_get_recommended_products() function, which queries the database for products in the same category as the current product. The recommended products are then displayed in a list format using HTML markup.

Examples

Example #1: Display Related Products on Single Product Page

This use case demonstrates how to display related products on the single product page using a WooCommerce product recommendation plugin. The code example below shows how to use the woocommerce_output_related_products() function to output related products.

<?php
if ( function_exists( 'woocommerce_output_related_products' ) ) {
    woocommerce_output_related_products();
}
?>

The woocommerce_output_related_products() function is a built-in WooCommerce function that generates the HTML markup for displaying related products. By calling this function, you can easily show related products on the single product page.

Example #2: Display Recommended Products on Shop Page

This use case shows how to display recommended products on the shop page using a WooCommerce product recommendation plugin. The code example below demonstrates how to use the woocommerce_product_loop_start() and woocommerce_product_loop_end() functions to wrap the recommended products loop.

<?php
if ( function_exists( 'woocommerce_product_loop_start' ) ) {
    woocommerce_product_loop_start();

    // Loop through recommended products
    while ( have_posts() ) {
        the_post();
        wc_get_template_part( 'content', 'product' );
    }

    woocommerce_product_loop_end();
}
?>

The woocommerce_product_loop_start() and woocommerce_product_loop_end() functions are WooCommerce functions that provide the necessary markup to start and end the product loop on the shop page. By using these functions, you can easily integrate the recommended products loop into the shop page.

Example #3: Display Cross-Sell Products on Cart Page

This use case demonstrates how to display cross-sell products on the cart page using a WooCommerce product recommendation plugin. The code example below illustrates how to use the woocommerce_cross_sell_display() function to output cross-sell products.

<?php
if ( function_exists( 'woocommerce_cross_sell_display' ) ) {
    woocommerce_cross_sell_display();
}
?>

The woocommerce_cross_sell_display() function is a built-in WooCommerce function that generates the HTML markup for displaying cross-sell products. By calling this function, you can easily show cross-sell products on the cart page.

Last updated on October 18, 2023. Originally posted on January 17, 2024.

Leave a Reply

Your email address will not be published. Required fields are marked *