WooCommerce: Adding Product Variation Handling Fees

In a Business Bloomer Club Slack thread, a member sought guidance on implementing per product variation handling fees in WooCommerce.

With the challenges of finding compatible plugins, particularly those that support High-Performance Order Storage (HPOS), the community discussed potential coding solutions to streamline the process. If you’re facing similar issues, here’s a breakdown of how you can efficiently add handling fees to your products.

The Challenge of Adding Handling Fees

Many WooCommerce store owners wish to impose handling fees based on product variations but struggle to find suitable plugins that meet their needs and are compatible with the latest updates.

The goal is to apply a fixed handling fee for each product, ensuring that if multiple products are ordered, only the highest fee is charged.

Proposed Solution: Coding Your Own Handling Fee

Custom Product Field
First, create a custom field for each product variation where you can specify the handling fee. You can label this field “Product Fee.” This allows you to set a different handling fee for each product variation without relying on external plugins.

Implementing the Logic
Once you have your custom field set up, use the following approach to calculate the maximum handling fee during checkout:

  • Loop through the products in the cart.
  • Retrieve the value of the “Product Fee” custom field for each product variation.
  • Determine the highest fee and apply it to the order.

Sample Code Snippet
Here’s a simplified example of how to implement this:

   add_action('woocommerce_cart_calculate_fees', 'add_handling_fee');

   function add_handling_fee() {
       $max_fee = 0;
       foreach (WC()->cart->get_cart() as $cart_item) {
           $product_id = $cart_item['variation_id'];
           $product_fee = get_post_meta($product_id, 'product_fee', true); // Retrieve the custom field value
           if ($product_fee > $max_fee) {
               $max_fee = $product_fee; // Set the highest fee
           }
       }

       if ($max_fee > 0) {
           WC()->cart->add_fee('Handling Fee', $max_fee); // Add the handling fee to the cart
       }
   }

Conclusion

By utilizing a custom field and a bit of coding, you can effectively manage handling fees for product variations in your WooCommerce store without needing to rely on potentially incompatible plugins.

This solution not only offers flexibility but also ensures that your store remains compatible with HPOS. If you’re new to coding, the resources shared in the thread can help guide you through the implementation process.

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Reply

Your email address will not be published. Required fields are marked *