Creating a full, production-ready WooCommerce upselling plugin involves several steps, including setting up the plugin structure, creating the necessary hooks and filters, and ensuring the plugin is secure and efficient. Below is a basic example to get you started. This example will focus on adding upsell products to the product page.
Step 1: Plugin Structure
Create a directory for your plugin, e.g., woocommerce-upselling-plugin. Inside this directory, create the following files:
woocommerce-upselling-plugin.php (main plugin file)
includes (directory for additional files)
includes/class-upsell.php (class file for handling upsell logic)
includes/class-upsell-admin.php (class file for admin settings)
includes/class-upsell-frontend.php (class file for frontend display)
Step 2: Main Plugin File
Create woocommerce-upselling-plugin.php:
<?php
/*
Plugin Name: WooCommerce Upselling Plugin
Description: A plugin to add upsell products to WooCommerce product pages.
Version: 1.0
Author: Your Name
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
require_once plugin_dir_path( __FILE__ ) . 'includes/class-upsell.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/class-upsell-admin.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/class-upsell-frontend.php';
function woocommerce_upselling_plugin_init() {
if ( class_exists( 'WooCommerce' ) ) {
new WooCommerce_Upsell();
new WooCommerce_Upsell_Admin();
new WooCommerce_Upsell_Frontend();
}
}
add_action( 'plugins_loaded', 'woocommerce_upselling_plugin_init' );
Step 3: Upsell Class
Create includes/class-upsell.php:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class WooCommerce_Upsell {
public function __construct() {
// Initialize the plugin
}
}
Step 4: Admin Class
Create includes/class-upsell-admin.php:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class WooCommerce_Upsell_Admin {
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
add_action( 'admin_init', array( $this, 'register_settings' ) );
}
public function add_admin_menu() {
add_options_page( 'Upsell Settings', 'Upsell Settings', 'manage_options', 'woocommerce-upsell-settings', array( $this, 'create_admin_page' ) );
}
public function register_settings() {
register_setting( 'woocommerce-upsell-settings-group', 'woocommerce_upsell_number_of_products' );
}
public function create_admin_page() {
?>
<div class="wrap">
<h1>Upsell Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields( 'woocommerce-upsell-settings-group' );
do_settings_sections( 'woocommerce-upsell-settings-group' );
?>
<table class="form-table">
<tr valign="top">
<th scope="row">Number of Upsell Products</th>
<td><input type="number" name="woocommerce_upsell_number_of_products" value="<?php echo get_option( 'woocommerce_upsell_number_of_products' ); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
}
Step 5: Frontend Class
Create includes/class-upsell-frontend.php:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class WooCommerce_Upsell_Frontend {
public function __construct() {
add_action( 'woocommerce_after_single_product_summary', array( $this, 'display_upsell_products' ), 30 );
}
public function display_upsell_products() {
global $product;
$upsell_ids = $product->get_upsell_ids();
$number_of_products = get_option( 'woocommerce_upsell_number_of_products', 4 );
if ( ! empty( $upsell_ids ) ) {
$args = array(
'post_type' => 'product',
'posts_per_page' => $number_of_products,
'post__in' => $upsell_ids,
'orderby' => 'post__in'
);
$upsell_query = new WP_Query( $args );
if ( $upsell_query->have_posts() ) {
echo '<div class="upsell-products">';
echo '<h2>You may also like...</h2>';
echo '<ul class="products">';
while ( $upsell_query->have_posts() ) {
$upsell_query->the_post();
wc_get_template_part( 'content', 'product' );
}
echo '</ul>';
echo '</div>';
wp_reset_postdata();
}
}
}
}
Step 6: Activation and Deactivation Hooks
Add activation and deactivation hooks to woocommerce-upselling-plugin.php:
register_activation_hook( __FILE__, 'woocommerce_upselling_plugin_activate' );
register_deactivation_hook( __FILE__, 'woocommerce_upselling_plugin_deactivate' );
function woocommerce_upselling_plugin_activate() {
// Activation logic, if any
}
function woocommerce_upselling_plugin_deactivate() {
// Deactivation logic, if any
}
Step 7: Security and Best Practices
- Sanitize and Validate Inputs: Ensure all user inputs are sanitized and validated.
- Error Handling: Add proper error handling to manage unexpected issues.
- Localization: Make the plugin translatable.
- Documentation: Provide clear documentation for users and developers.
Step 8: Testing
Thoroughly test the plugin in a staging environment before deploying it to production. Ensure it works with different themes and plugins.
Conclusion
This example provides a basic structure for a WooCommerce upselling plugin. Depending on your requirements, you may need to add more features, improve the UI, or handle edge cases. Always follow best practices for WordPress plugin development to ensure security and performance.