I wrote a new plugin this week. It’s a WooCommerce extension that adds highly customized “wish list” — actually an “Interested in a Quote” list — for a store that doesn’t do any online purchases.
Why an online store that doesn’t do online purchases? This particular client sells stuff in a highly competitive and price-sensitive market. Prices are volatile and can vary from one customer to the next depending on their holding particular government contracts or being part of certain sectors: healthcare, education, etc. So WooCommerce products are used for displaying products, but no price information is displayed and all the cart and checkout functionality is disabled. Instead, potential customers are directed to a “get a quote” form where they can indicate what they’re interested in. A sales rep calls back and takes it from there.
Without the WooCommerce cart, there has been no way for customers to easily select products, or compile them into a list. The client’s customers have to note their products on their own and then type them into the quote request form. We considered activating the regular cart without price functions and then modifying the checkout functions to send a quote request instead of an order. But because down the line the customer might actually want to turn on the cart and checkout for some products and keep other items on the “ask for a quote” basis, we didn’t want to hack the cart and cut off that possibility for the future. There had to be a way for both “products in a quote list” and products in a cart.
We looked at several “wish list” extensions already on the market, but they did way more than, and not quite, what we had in mind. We’d have to do major surgery on any of them to do exactly what we needed, so we decided to roll our own.
I’ve been doing a lot over the past year with Test Driven Development (TDD) and particularly as it applies to WordPress development. I wanted to apply all that I’ve been learning in this project. I wrote the unit tests first. I also wrote integration tests (sometimes concurrently with the unit tests before writing the code, sometimes writing a corresponding integration test as soon as the unit test passed), so the plugin has a complete test suite out of the gate. The extension is written completely in Object Oriented PHP using PSR–4.
The list works by inserting a new number input and “Add to Quote List” button to product detail pages. When the button is clicked it adds the item ID and indicated quantities to a cookie. On the quote request page, a script reads the cookie, grabs the product names via ajax, and enables the list to be inserted on the page using a shortcode. The list allows users to remove items and to update item quantities. The list is also inserted into a hidden form field on the page (and is updated whenever the user removes or updates a quantity) that gets submitted with the form. Finally, when the list is submitted, the cookie is cleared allowing the user to start again with a new list.
Sharing What I’ve Learned:
As I built this extension, I found myself having to solve a number of common problems. (I assume they’re common because they arose during the building of a fairly routine bit of plugin customization.)
- Thinking through OOP architecture in a WordPress plugin,
- Doing Unit and Integration Testing in WordPress,
- Doing it TDD in WordPress,
- Writing a plugin that depends on and extends another plugin,
- Building an asset loader that can tell when it should load a minimized version and when it should load a development version.
- Loading scripts only when you need them.
- Doing ajax in WordPress.
- Baking your own cookie.
Over the next few weeks I’m planning on writing all of this up into a series of posts on how I did it. Not that anyone is going to want to do exactly what I did; this was a specific solution for a particular set of requirements. But maybe it will help someone (myself 6 months from now) who is dealing with some of the “common problems” encountered here. And, it’s also likely that someone may have a better idea how to approach and solve these things than what I did – and I’d be glad to know what I can do better.