Here's a production-ready WooCommerce upselling plugin that displays related products on the product page. This plugin is lightweight, secure, and follows best practices for WordPress and WooCommerce development.
🔧 Plugin Structure
Create a folder named woocommerce-upsell-plugin in your wp-content/plugins/ directory. Inside, create a file named woocommerce-upsell-plugin.php and paste the code below.
📄 woocommerce-upsell-plugin.php
<?php
/*
Plugin Name: WooCommerce Upsell Plugin
Description: A simple WooCommerce upsell plugin that displays related products on the product page.
Version: 1.0
Author: Your Name
Text Domain: upsell-plugin
License: GPL2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
// Check if WooCommerce is active
if ( ! class_exists( 'WooCommerce' ) ) {
return;
}
/**
* Display upsell products based on the same category as the current product.
*/
function display_upsell_products() {
global $product;
// Ensure we're on a single product page and the product is valid
if ( ! is_a( $product, 'WC_Product' ) ) {
return;
}
// Get the product's categories
$categories = wp_get_post_terms( $product->get_id(), 'product_cat', array( 'fields' => 'ids' ) );
if ( empty( $categories ) ) {
return;
}
// Query related products
$args = array(
'post_type' => 'product',
'posts_per_page' => 4, // Number of upsell products
'post__not_in' => array( $product->get_id() ),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $categories,
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<h2 class="upsell-title">' . esc_html__( 'You might also like...', 'upsell-plugin' ) . '</h2>';
echo '<div class="upsell-products">';
while ( $query->have_posts() ) {
$query->the_post();
wc_get_template_part( 'content', 'product' ); // Use WooCommerce's product template
}
echo '</div>';
}
wp_reset_postdata();
}
// Hook into WooCommerce's product page
add_action( 'woocommerce_after_single_product_summary', 'display_upsell_products', 10 );
/**
* Enqueue plugin styles (optional for customization)
*/
function upsell_plugin_enqueue_styles() {
wp_enqueue_style( 'upsell-plugin-style', plugins_url( 'style.css', __FILE__ ) );
}
add_action( 'wp_enqueue_scripts', 'upsell_plugin_enqueue_styles' );
🎨 Optional: Add Custom CSS
Create a style.css file in the same folder as the plugin. Add the following to style the upsell section:
.upsell-products {
display: flex;
flex-wrap: wrap;
gap: 20px;
margin-top: 30px;
}
.upsell-products .product {
flex: 1 1 200px;
min-width: 200px;
}
✅ Features
- Displays related products from the same category as the current product.
- Secure and compatible with modern WooCommerce versions.
- Lightweight and easy to extend.
- Translation-ready with the
text-domain.
📌 Usage Instructions
- Upload the
woocommerce-upsell-plugin folder to wp-content/plugins/.
- Activate the plugin from the WordPress admin.
- Visit a product page to see the upsell section.
🧠 Notes for Production
- Customization: Extend the plugin by adding settings (e.g., number of products, categories) via the WordPress admin.
- Performance: Use caching or optimize queries for large product catalogs.
- Security: Always sanitize and escape outputs (already handled in the code).
- Testing: Test with different themes and WooCommerce versions.
🚀 Next Steps
To make this plugin even more powerful:
- Add a settings page for user configuration.
- Integrate with WooCommerce's built-in upsells.
- Use a recommendation engine (e.g., AI-based product suggestions).
Let me know if you'd like an advanced version with settings or a checkout upsell!