In this topic, we’ll provide an overview of extending WC_Data and then delve into some of the key considerations and a ‘cookbook’ process for extending each of the WC_Data families.
There’s a lot of information to cover here, don’t feel like you need to memorize all this–that’s a lot of extra brain space. Instead, refer back to this information frequently, whenever you need to customize the behavior of one or more data class families.
Let’s get started
This is one of the most frequently extended classes. Be sure to checkout the WooCommerce Store before you reinvent the wheel with your new product type Many businesses offer unique items for sale to their customers that don’t fit well within the standard define WC_Product family. In this case, it can be advantageous to create a custom WC_Product Family type
*Note: Steps 1 and 2 are the absolute minimum to add a custom product type. The product factory will use WC_Product_Simple as it’s default class. You can fully customize behavior from there, using conditional action and filter hooks, if you choose.
Sometimes the way a customer purchases a product doesn’t map cleanly to WooCommerce’s Cart/Purchase Order System. A popular example of this is in a subscription scenario. In these cases, it can be efficient to extend the WC_Order family to implement that custom functionality
Finally, to actually use your custom order type on the front end, hook into the checkout process with the ‘woocommerce_create_order’ filter with your custom logic.
Standard WooCommerce Order Item types include products, coupons, fees, shipping, and taxes– basically anything directly involved in the calculation of the order’s total. Generally, these default fields are sufficient to manage most situations. However, if necessary, implementing your own custom order-item class is fairly easy.
In order to set up custom payment gateways with saved customer payment tokes you may to set up a custom Payment Token class to match that gateway’s requirements. Fortunately, WooCommerce offers a robust payment token API.
If, for some reason, you do not want to use the standard naming nomenclature, you may override default behavior using the ‘woocommerce_payment_token_class’ filter
WC_Coupon is involved in a series of interactions between WC_Cart and WC_Discounts.
For this reason, developers should avoid directly extending the WC_Coupon class and build custom coupon behavior either by creating a custom data store or with the following filters:
The WC_Customer_Download constructor is directly called by WC_Download_Handler. Extending is not recommended. Instead, use these hooks to customize behavior:Found in the WC_Download_Handler class and the Data Store
Actions | Filters |
---|---|
woocommerce_download_product | woocommerce_file_download_filename |
woocommerce_download_file_{file_download_method} | woocommerce_file_download_method |
woocommerce_grant_product_download_access | woocommerce_download_file_xsendfile_file_path |
woocommerce_download_file_xsendfile_lighttpd_file_path | |
woocommerce_download_file_xsendfile_x_accel_redirect_file_path | |
woocommerce_customer_get_downloadable_products | |
woocommerce_downloadable_file_permission_data | |
woocommerce_downloadable_file_permission_format |
Alternately, you could customize behavior by implementing a custom data store, with logic that result in the desired behavior
The situation is even worse with WC_Customer_Download_Log. Your only hooks for customizing this class’s behavior are these filters:
The WC_Customer constructor is directly called by the WooCommerce final singleton class and
loaded as it’s $customer property.
For this reason, directly extending the WC_Customer class is not advisable.
Instead, use a
custom data store to customize behavior and data, or use the following filters:
The WC_Shpping_Zone constructor is directly called by WC_Shipping_Zones. Custom behavior can be generated with the following hooks:
Actions | Filters |
---|---|
woocommerce_shipping_zone_method_added | woocommerce_shipping_zone_shipping_methods A final cut on which shipping methods will be allowed in a given cart |
woocommerce_shipping_zone_method_deleted | woocommerce_valid_location_types |
woocommerce_shipping_zone_loaded | woocommerce_get_zone_criteriaUsed on $wpdb WHERE clauses for getting zones |
The wc-webhook-functions.php file loads the webhook API and directly calls the WC_Webhook constructor. To customize this behavior you should focus on the following filters:
and the following actions:
Wew! That’s a lot of info. You’re not expected to remember all that. Just remember that the information is here, and can be used as a cheat sheet anytime you want. And be sure to use the resources list to help dive deeper into a topic if you need more info.
You should now have confidence that, whatever WC_Data customization task is part of your next project, you’re armed for the task.
In the next section, we’re going to go one layer deeper–customizing the Data Stores.
Hint: Since this topic is mostly a “cookbook” the meta answers are in the front matter
0 of 4 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 4 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
Which of the following are best customized by extending the default classes?
When creating a custom WC_Product, the class name will be WC_Product_{Product_Type}. What must {Product_Type} relate to?
How do you incorporate your custom WC_Order Type into the checkout process?
Best practice customization would use hooks in which of the following?