WooCommerce: Add a Custom Action in Tools

In WooCommerce, if you go to System Status > Tools you can see a list of useful default tools such as “Clear Transients”, “Recount Terms”, or “Install WooCommerce Pages”. But do you know that you can create your own custom buttons there? It can be very useful when you create a plugin that needs advanced tools.

In the code below you’ll see that adding a button linked to a custom action is pretty easy. To make the code easier to test, I embed the code in a plugin.

The idea is to register a new button using a hook (filter) called woocommerce_debug_tools and then create the callback function that will be triggered when the button is clicked.

The function called debug_button_action() will be where you will have to create custom coding. You can for example send an email, delete en option, create content etc… just be creative!

Final Result:

woocommerce-custom-tools-button

Code:

That’s it! Simple and useful right?

WooCommerce: Hide coupon box on checkout page if a coupon is already applied

Here is a quick snippet to make things a bit less confusing for your customers in your WooCommerce store. When you enable the use of coupons, WooCommerce adds a field on the cart page, and a box notice on the checkout page. But, if you enter a coupon in the cart page, and apply that coupon to the order, the notice is still displayed on the checkout page. That could be confusing for your customers, and they could think that the coupon has NOT been applied, which is not correct. So, here is a snippet that will hide that box on the checkout page if a coupon has already been applied to cart:

Simply place that snippet in functions.php within your theme folder!

WooCommerce: Free Shipping on a Per Product Basis in the Same Cart

This is a common request from WooCommerce users:

I want to ship specific products for free while some others will have shipping costs, but when a product having free shipping is in cart with another product that has shipping costs, free shipping isn’t available at all, what to do?

There’s a solution to that! And you don’t need any plugins to solve this issue. Based on a post by Mike Jolley, I’d like to explain how to use different shipping methods on a per product basis in the same cart. Let’s say you have two products, one that must be shipped for free, and a second one having a flat shipping cost (that would work with rates based on APIs as well,  like UPS, USPS, Fedex, Canada Post, Australia Post etc…). The 1st step is to create 2 shipping classes, in Products > Shipping Classes:

shipping-classes

Create at least two shipping classes

You need to create a “Free” shipping class. You can create as many other shipping classes as ou want.

The next step is to assign classes to each product:

product1

1st Product Settings

product2

2nd Product settings

Now that you assigned classes to products, you need to define flat rates per classes in WooCommerce > Settings > Shipping > Flat Rate:

flat-rates

Shipping classes flat rates

Make sure that you setup a cost equal to zero for the free shipping class.

Finally you need to filter the shipping package using custom code and separate products from one package to two packages having each its own shipping method. Add the following snippet in the file called functions.php in your theme folder:

Please note that you need to use the free shipping method slug at line 14. And the final result in the cart is the following:

cart

Shipping packages filtered in cart

That’s it! I hope that will save you some headaches! If you enjoyed this post, make sure you share it on Twitter!

Similar posts by awesome folks:

Fixing Shipment Tracking & WooCommerce Subscriptions issues

If you use WooCommerce you probably know Subscriptions, that awesome plugin that allows anyone to sell subscriptions. Today, I had a ticket about an issue between Suscriptions and Shipment Tracking, a great extension to track your shippings.

Here is what the customer was facing:

I’ve been using Shipment Tracking with Subscriptions for a while now, and every month I run into the same problem with shipment tracking. If I add shipment info in January for the order that month, the email goes out correctly to the customer with the tracking info.

But if I don’t manually remove the tracking info and set it back to “Custom Provider” before the renewal purchase order is placed, the renewal order email that goes to the customer includes the old tracking info at the top and it’s very confusing.

After a quick talk with guys from Skyverge (thanks Matt!),  we came up to a nice solution to fix small issue between these two great plugins. The following code snippet will stop the unwanted tracking meta from transferring from the previous subscription order to the new renewal order. You will need to change the _tracking_meta_key within the code below to reflect the exact tracking meta key stored in the database for this to work properly. And boom (don’t forget: this snippet needs to be added in functions.php in the theme folder)!