How to Add Custom Commission Fees to WooCommerce Products
This tutorial will show you how to add percentage-based and fixed commissions to your WooCommerce products. The commission will be automatically calculated and added to the product price during checkout.
🌟 Key Features:
- Add percentage commission (e.g., 2%)
- Add fixed commission fee (e.g., ₹20)
- Show commission breakdown on product pages
- Include commission in cart/checkout totals
- Display correct prices in orders and receipts
📌 Installation Instructions
- Copy the entire code below
- Paste it in your theme's
functions.php
file - Save the file (use a child theme for safety)
💻 Complete Code
/**
* WooCommerce Custom Commission
* Adds percentage and fixed fees to product prices
*/
// 1. Add commission fields to product admin
add_action('woocommerce_product_options_pricing', 'add_commission_fields');
function add_commission_fields() {
woocommerce_wp_text_input(array(
'id' => '_commission_percentage',
'label' => __('Commission %', 'woocommerce'),
'description' => __('E.g., 2 for 2% commission', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array('step' => '0.01', 'min' => '0')
));
woocommerce_wp_text_input(array(
'id' => '_fixed_commission',
'label' => __('Fixed Fee', 'woocommerce'),
'description' => __('E.g., 20 for ₹20 fixed charge', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array('step' => '0.01', 'min' => '0')
));
}
// 2. Save commission fields
add_action('woocommerce_process_product_meta', 'save_commission_fields');
function save_commission_fields($post_id) {
$fields = array('_commission_percentage', '_fixed_commission');
foreach ($fields as $field) {
if (isset($_POST[$field])) {
update_post_meta($post_id, $field, sanitize_text_field($_POST[$field]));
}
}
}
// 3. Display commission info on product page
add_action('woocommerce_single_product_summary', 'show_commission_info', 25);
function show_commission_info() {
global $product;
$percentage = get_post_meta($product->get_id(), '_commission_percentage', true);
$fixed = get_post_meta($product->get_id(), '_fixed_commission', true);
if ($percentage || $fixed) {
echo '';
echo 'Additional Charges:
';
if ($percentage) echo ''.esc_html($percentage).'% Commission
';
if ($fixed) echo 'Fixed Fee: '.wc_price($fixed).'
';
echo '';
}
}
// 4. Add commission to cart prices
add_action('woocommerce_before_calculate_totals', 'add_commission_to_cart');
function add_commission_to_cart($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
foreach ($cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
$percentage = (float)get_post_meta($product_id, '_commission_percentage', true);
$fixed = (float)get_post_meta($product_id, '_fixed_commission', true);
$price = $cart_item['data']->get_price();
if ($percentage) $price += ($price * $percentage / 100);
if ($fixed) $price += $fixed;
$cart_item['data']->set_price($price);
}
}
// 5. Show commission breakdown in cart/checkout
add_filter('woocommerce_get_item_data', 'display_commission_details', 10, 2);
function display_commission_details($item_data, $cart_item) {
$product_id = $cart_item['product_id'];
$percentage = get_post_meta($product_id, '_commission_percentage', true);
$fixed = get_post_meta($product_id, '_fixed_commission', true);
if ($percentage) {
$original = $cart_item['data']->get_regular_price();
$item_data[] = array(
'name' => 'Commission',
'value' => $percentage.'% ('.wc_price($original * $percentage / 100).')'
);
}
if ($fixed) {
$item_data[] = array(
'name' => 'Fixed Fee',
'value' => wc_price($fixed)
);
}
return $item_data;
}
// Optional CSS for styling
add_action('wp_head', 'commission_css');
function commission_css() {
echo '<style>
.commission-notice {
background: #f8f8f8;
padding: 15px;
margin: 20px 0;
border-left: 3px solid #d33;
}
.commission-notice h4 {
margin-top: 0;
}
</style>';
}
🎨 Customization Options
To modify the commission display:
- Change the position of commission notice by adjusting the priority number in
woocommerce_single_product_summary
hook (currently 25) - Edit the CSS in the last function to match your theme's design
- Change text labels (like "Commission" or "Fixed Fee") to match your business terms
🔧 Troubleshooting
If commissions aren't showing:
- Clear your WooCommerce cache
- Test with a clean cart (remove all items first)
- Check that you've saved the commission fields in product admin
✅ Conclusion
This solution provides a complete way to add commission fees to your WooCommerce products without using plugins. The commission becomes part of the final price automatically, ensuring correct totals in orders and receipts.
For advanced users: You could extend this to support different commission rates per product category or user role by adding additional conditional logic.
0 Comments