<?php
/**
* Plugin Name: WooCommerce Custom Upsell Plugin
* Plugin URI: https://example.com
* Description: A production-ready plugin for adding custom upsell products in WooCommerce. Allows admins to set and display custom upsells per product.
* Version: 1.0.0
* Requires at least: 5.0
* Requires PHP: 7.0
* Author: Your Name
* Author URI: https://example.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: wc-upsell-plugin
* Domain Path: /languages
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Check if WooCommerce is active.
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) && ! is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) {
add_action( 'admin_notices', 'wc_upsell_plugin_wc_missing_notice' );
return;
}
/**
* Display an admin notice if WooCommerce is not active.
*/
function wc_upsell_plugin_wc_missing_notice() {
$class = 'notice notice-error';
$message = __( 'WooCommerce Custom Upsell Plugin requires WooCommerce to be installed and active.', 'wc-upsell-plugin' );
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), wp_kses_post( $message ) );
}
// Define the main plugin class.
class WC_Upsell_Plugin {
/**
* Constructor: Set up hooks.
*/
public function __construct() {
// Add metabox for upsell products in product edit screen.
add_action( 'add_meta_boxes', array( $this, 'add_upsell_metabox' ) );
// Save metabox data when product is saved.
add_action( 'save_post_product', array( $this, 'save_upsell_meta' ) );
// Display custom upsell products on the single product page.
add_action( 'woocommerce_after_single_product_summary', array( $this, 'display_upsell_products' ), 15 );
// Enqueue styles for frontend display.
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
}
/**
* Add the custom upsell metabox to the product edit screen.
*/
public function add_upsell_metabox() {
add_meta_box(
'wc_upsell_metabox',
__( 'Custom Upsell Products', 'wc-upsell-plugin' ),
array( $this, 'render_upsell_metabox' ),
'product',
'side',
'default'
);
}
/**
* Render the upsell metabox content.
*
* @param WP_Post $post The current post object.
*/
public function render_upsell_metabox( $post ) {
// Add nonce for security.
wp_nonce_field( 'wc_upsell_meta_box', 'wc_upsell_meta_nonce' );
// Get the current upsell product IDs.
$upsell_ids = get_post_meta( $post->ID, '_wc_upsell_custom_ids', true );
if ( ! is_array( $upsell_ids ) ) {
$upsell_ids = array();
}
// Query all published products, excluding the current one.
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'post__not_in' => array( $post->ID ),
'orderby' => 'title',
'order' => 'ASC',
);
$products = get_posts( $args );
// Output the select field for upsell products.
?>
<p>
<label for="wc_upsell_custom_ids"><?php esc_html_e( 'Select products to upsell (hold Ctrl/Command to select multiple):', 'wc-upsell-plugin' ); ?></label>
<select name="wc_upsell_custom_ids[]" id="wc_upsell_custom_ids" multiple="multiple" style="width:100%;">
<?php foreach ( $products as $product ) : ?>
<option value="<?php echo esc_attr( $product->ID ); ?>" <?php selected( in_array( $product->ID, $upsell_ids, true ), true ); ?>>
<?php echo esc_html( $product->post_title ); ?>
</option>
<?php endforeach; ?>
</select>
</p>
<?php
}
/**
* Save the upsell meta data when the product is saved.
*
* @param int $post_id The ID of the post being saved.
*/
public function save_upsell_meta( $post_id ) {
// Check if this is an autosave or revision.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Verify the nonce for security.
if ( ! isset( $_POST['wc_upsell_meta_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['wc_upsell_meta_nonce'] ), 'wc_upsell_meta_box' ) ) {
return;
}
// Check user capabilities.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Check if the upsell IDs are set and sanitize them.
if ( isset( $_POST['wc_upsell_custom_ids'] ) && is_array( $_POST['wc_upsell_custom_ids'] ) ) {
$upsell_ids = array_map( 'absint', $_POST['wc_upsell_custom_ids'] ); // Sanitize to integers.
update_post_meta( $post_id, '_wc_upsell_custom_ids', $upsell_ids );
} else {
// If no upsells are selected, delete the meta.
delete_post_meta( $post_id, '_wc_upsell_custom_ids' );
}
}
/**
* Display custom upsell products on the single product page.
*/
public function display_upsell_products() {
global $post;
// Get the custom upsell product IDs.
$upsell_ids = get_post_meta( $post->ID, '_wc_upsell_custom_ids', true );
if ( ! empty( $upsell_ids ) && is_array( $upsell_ids ) ) {
// Query the upsell products.
$args = array(
'post_type' => 'product',
'post__in' => array_map( 'absint', $upsell_ids ), // Ensure IDs are integers.
'posts_per_page' => -1,
'orderby' => 'post__in', // Maintain the order of selection.
'post_status' => 'publish',
);
$upsell_query = new WP_Query( $args );
if ( $upsell_query->have_posts() ) {
// Output the upsell section.
echo '<section class="upsell-products woocommerce">';
echo '<h2>' . esc_html__( 'You may also like', 'wc-upsell-plugin' ) . '</h2>';
echo '<ul class="products columns-<?php echo esc_attr( wc_get_loop_prop( 'columns' ) ); ?>">';
while ( $upsell_query->have_posts() ) {
$upsell_query->the_post();
wc_get_template_part( 'content', 'product' ); // Use WooCommerce's product template for consistency.
}
echo '</ul>';
echo '</section>';
wp_reset_postdata(); // Reset post data to avoid conflicts.
}
}
}
/**
* Enqueue styles for the upsell display.
*/
public function enqueue_styles() {
wp_enqueue_style( 'wc-upsell-plugin-style', plugin_dir_url( __FILE__ ) . 'style.css', array(), '1.0.0', 'all' );
}
}
// Instantiate the plugin class.
new WC_Upsell_Plugin();