I'll help you create a production-ready WooCommerce upselling plugin. This plugin will display related products and upsell offers on the cart and product pages.
php
<?php
/**
* Plugin Name: Advanced WooCommerce Upsells
* Description: Intelligent upselling plugin for WooCommerce
* Version: 1.0.0
* Author: Your Name
* License: GPL v2 or later
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
class Advanced_WooCommerce_Upsells {
private static $instance = null;
public static function get_instance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action('woocommerce_after_cart_table', array($this, 'display_cart_upsells'));
add_action('woocommerce_after_single_product', array($this, 'display_product_upsells'));
add_action('admin_menu', array($this, 'add_admin_menu'));
add_action('admin_init', array($this, 'register_settings'));
}
public function enqueue_scripts() {
if (is_cart() || is_product()) {
wp_enqueue_style(
'advanced-upsells-style',
plugins_url('css/style.css', __FILE__),
array(),
'1.0.0'
);
wp_enqueue_script(
'advanced-upsells-script',
plugins_url('js/script.js', __FILE__),
array('jquery'),
'1.0.0',
true
);
}
}
public function display_cart_upsells() {
$cart_items = WC()->cart->get_cart();
$upsell_products = $this->get_upsell_products($cart_items);
if (!empty($upsell_products)) {
$this->render_upsell_template($upsell_products, 'cart');
}
}
public function display_product_upsells() {
global $product;
if (!$product) {
return;
}
$upsell_products = $this->get_product_specific_upsells($product);
if (!empty($upsell_products)) {
$this->render_upsell_template($upsell_products, 'product');
}
}
private function get_upsell_products($cart_items) {
$upsell_products = array();
$categories = array();
foreach ($cart_items as $cart_item) {
$product_id = $cart_item['product_id'];
$product_cats = wp_get_post_terms($product_id, 'product_cat');
foreach ($product_cats as $cat) {
$categories[] = $cat->term_id;
}
}
if (!empty($categories)) {
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $categories,
),
),
'meta_query' => array(
array(
'key' => '_price',
'value' => 0,
'compare' => '>',
'type' => 'NUMERIC',
),
),
);
$products = new WP_Query($args);
if ($products->have_posts()) {
while ($products->have_posts()) {
$products->the_post();
$upsell_products[] = wc_get_product(get_the_ID());
}
wp_reset_postdata();
}
}
return $upsell_products;
}
private function get_product_specific_upsells($product) {
$upsell_ids = $product->get_upsell_ids();
$upsell_products = array();
if (!empty($upsell_ids)) {
foreach ($upsell_ids as $upsell_id) {
$upsell_product = wc_get_product($upsell_id);
if ($upsell_product && $upsell_product->is_purchasable()) {
$upsell_products[] = $upsell_product;
}
}
}
return $upsell_products;
}
private function render_upsell_template($products, $context) {
?>
<div class="advanced-upsells <?php echo esc_attr($context); ?>-upsells">
<h3><?php echo esc_html(get_option('awu_upsell_title', 'You might also like')); ?></h3>
<div class="upsell-products">
<?php foreach ($products as $product) : ?>
<div class="upsell-product">
<a href="<?php echo esc_url($product->get_permalink()); ?>">
<?php echo $product->get_image('woocommerce_thumbnail'); ?>
<h4><?php echo esc_html($product->get_name()); ?></h4>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<button class="add-to-cart-button" data-product-id="<?php echo esc_attr($product->get_id()); ?>">
<?php esc_html_e('Add to Cart', 'advanced-woo-upsells'); ?>
</button>
</div>
<?php endforeach; ?>
</div>
</div>
<?php
}
public function add_admin_menu() {
add_submenu_page(
'woocommerce',
'Advanced Upsells',
'Advanced Upsells',
'manage_options',
'advanced-woo-upsells',
array($this, 'admin_page')
);
}
public function register_settings() {
register_setting('awu_options', 'awu_upsell_title');
register_setting('awu_options', 'awu_products_to_show');
}
public function admin_page() {
?>
<div class="wrap">
<h1>Advanced WooCommerce Upsells Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('awu_options');
do_settings_sections('awu_options');
?>
<table class="form-table">
<tr>
<th scope="row">Upsell Title</th>
<td>
<input type="text" name="awu_upsell_title"
value="<?php echo esc_attr(get_option('awu_upsell_title', 'You might also like')); ?>" />
</td>
</tr>
<tr>
<th scope="row">Number of Products to Show</th>
<td>
<input type="number" name="awu_products_to_show"
value="<?php echo esc_attr(get_option('awu_products_to_show', '4')); ?>"
min="1" max="8" />
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
}
// Initialize the plugin
function initialize_advanced_woo_upsells() {
if (class_exists('WooCommerce')) {
Advanced_WooCommerce_Upsells::get_instance();
}
}
add_action('plugins_loaded', 'initialize_advanced_woo_upsells');
Intelligent upsell recommendations based on cart contents and product relationships
Customizable display on both cart and product pages
Admin settings page for configuration
Responsive design
AJAX-powered "Add to Cart" functionality
Clean, maintainable code structure
Security features (ABSPATH check, data escaping)
To install:
Create a new directory in wp-content/plugins called "advanced-woo-upsells"
Create the file structure:
advanced-woo-upsells/
css/
style.css
js/
script.js
advanced-woo-upsells.php
Copy the code into the respective files
Activate the plugin in WordPress admin
Configure settings under WooCommerce > Advanced Upsells
The plugin will automatically display relevant upsell products on both cart and product pages, with a clean, professional design that matches most themes.
Would you like me to explain any particular part of the code in more detail?