Creating a WooCommerce reseller plugin involves several steps. Below is a structured guide to help you build a basic reseller system. This guide assumes you have a working knowledge of WordPress/WooCommerce development, PHP, and MySQL.
---
### **1. Plugin Structure**
Create a new folder in `wp-content/plugins/` (e.g., `reseller-plugin`), and add the following files:
- `reseller-plugin.php` (Main plugin file)
- `includes/` (Folder for helper functions)
- `admin/` (Admin-facing code)
- `public/` (Frontend-facing code)
---
### **2. Core Features**
Your plugin should include:
- **Reseller Registration/Management**
- **Commission Calculation**
- **Product Assignment to Resellers**
- **Order Tracking**
- **Dashboard for Resellers**
- **Payment Handling**
---
### **3. Step-by-Step Implementation**
#### **Step 1: Register a Reseller User Role**
Add a custom user role (`reseller`) with specific capabilities.
```php
// In reseller-plugin.php
function register_reseller_role() {
add_role(
'reseller',
'Reseller',
array(
'read' => true,
'edit_posts' => false,
'delete_posts' => false,
'manage_woocommerce' => true,
'view_woocommerce_reports' => true,
)
);
}
register_activation_hook(__FILE__, 'register_reseller_role');
```
---
#### **Step 2: Add Reseller Commission Settings**
Allow resellers to set their commission rate (e.g., in their profile).
```php
// Add commission field to user profile
function reseller_commission_field($user) {
if (in_array('reseller', $user->roles)) {
?>
<h3>Reseller Settings</h3>
<table class="form-table">
<tr>
<th><label for="commission_rate">Commission Rate (%)</label></th>
<td>
<input type="number" name="commission_rate" id="commission_rate"
value="<?php echo esc_attr(get_user_meta($user->ID, 'commission_rate', true)); ?>"
class="regular-text" min="0" max="100" step="0.1">
</td>
</tr>
</table>
<?php
}
}
add_action('show_user_profile', 'reseller_commission_field');
add_action('edit_user_profile', 'reseller_commission_field');
// Save commission rate
function save_reseller_commission_field($user_id) {
if (current_user_can('edit_user', $user_id)) {
update_user_meta($user_id, 'commission_rate', sanitize_text_field($_POST['commission_rate']));
}
}
add_action('personal_options_update', 'save_reseller_commission_field');
add_action('edit_user_profile_update', 'save_reseller_commission_field');
```
---
#### **Step 3: Assign Products to Resellers**
Add a meta box to WooCommerce products to link them to a reseller.
```php
// Add reseller dropdown to product editor
function reseller_product_meta_box() {
add_meta_box(
'reseller_product_meta',
'Reseller Settings',
'reseller_product_meta_callback',
'product',
'side',
'default'
);
}
add_action('add_meta_boxes', 'reseller_product_meta_box');
function reseller_product_meta_callback($post) {
$resellers = get_users(array('role' => 'reseller'));
$selected_reseller = get_post_meta($post->ID, '_reseller_id', true);
?>
<label for="reseller_id">Assign to Reseller:</label>
<select name="reseller_id" id="reseller_id" class="widefat">
<option value="">None</option>
<?php foreach ($resellers as $reseller) : ?>
<option value="<?php echo $reseller->ID; ?>" <?php selected($selected_reseller, $reseller->ID); ?>>
<?php echo $reseller->display_name; ?>
</option>
<?php endforeach; ?>
</select>
<?php
}
// Save reseller assignment
function save_reseller_product_meta($post_id) {
if (isset($_POST['reseller_id'])) {
update_post_meta($post_id, '_reseller_id', absint($_POST['reseller_id']));
}
}
add_action('save_post_product', 'save_reseller_product_meta');
```
---
#### **Step 4: Calculate Commission on Order Completion**
Hook into WooCommerce order completion to calculate commissions.
```php
function calculate_reseller_commission($order_id) {
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item) {
$product_id = $item->get_product_id();
$reseller_id = get_post_meta($product_id, '_reseller_id', true);
if ($reseller_id) {
$commission_rate = get_user_meta($reseller_id, 'commission_rate', true);
$total = $item->get_total();
$commission = ($total * $commission_rate) / 100;
// Store commission in a custom table or option
update_user_meta($reseller_id, 'pending_commission', $commission, true);
}
}
}
add_action('woocommerce_order_status_completed', 'calculate_reseller_commission');
```
---
#### **Step 5: Create a Reseller Dashboard**
Add a shortcode for resellers to view their earnings and products.
```php
// Shortcode for reseller dashboard
function reseller_dashboard_shortcode() {
if (!current_user_can('reseller')) return;
$reseller_id = get_current_user_id();
$commission = get_user_meta($reseller_id, 'pending_commission', true);
$products = get_posts(array(
'post_type' => 'product',
'meta_key' => '_reseller_id',
'meta_value' => $reseller_id,
));
ob_start();
?>
<div class="reseller-dashboard">
<h2>Your Commission: $<?php echo $commission; ?></h2>
<h3>Your Products</h3>
<ul>
<?php foreach ($products as $product) : ?>
<li><?php echo $product->post_title; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php
return ob_get_clean();
}
add_shortcode('reseller_dashboard', 'reseller_dashboard_shortcode');
```
---
#### **Step 6: Handle Payouts (Manual for Now)**
Create an admin page to mark commissions as paid.
```php
// Add admin menu for reseller payouts
function reseller_payouts_menu() {
add_submenu_page(
'users.php',
'Reseller Payouts',
'Payouts',
'manage_options',
'reseller-payouts',
'reseller_payouts_page'
);
}
add_action('admin_menu', 'reseller_payouts_menu');
function reseller_payouts_page() {
// Logic to display and process payouts
}
```
---
### **4. Extend Functionality**
- **Automated Payouts**: Integrate PayPal/Stripe API for automatic payments.
- **Reports**: Use WooCommerce's `WC_Admin_Report` class for sales reports.
- **Multi-Level Resellers**: Add support for tiers (e.g., resellers can have sub-resellers).
---
### **5. Testing & Security**
- Test with different user roles and product types.
- Sanitize all inputs and use nonces for forms.
- Use WordPress transients for caching commission data.
---
### **6. Documentation**
- Provide setup instructions for users.
- Explain how resellers can manage their products and track earnings.
---
### **Final Notes**
This is a basic framework. For a production plugin, you’ll need to:
- Add error handling.
- Optimize database queries.
- Follow WordPress coding standards.
- Consider using namespaces and autoloading.
Would you like me to elaborate on any specific part (e.g., automated payouts, reporting, or security)?
中文翻译原文输出
最新发布