Using Drupal Commerce for node subscription - part II: creating the order
In part I we described the requirements and what we found out in the existing modules.
In this part we will describe How we build the Drupal Commerce order to be paid.
So back to the requirements.
"Data structure"Each created node from a given content type should have several products possible for subscription. To simplify this, let's say there is a content type "business", and after a user creates "my business" node out of it, he can "subscribe" it weekly or monthly.
Our solution uses the Entity reference module, creating "field_monthly_product" and "field_weekly_product" fields in the content type, but with the most important configurations:
- Those fields has a ready-made default values, refering to existing products.
- Those fields permissions are set to "view only" for the users.
Using those settings, when the user creates the node, those fields refer to the products pre-defined, and he as the node owner can't control it.
Another field needed in the content type is the publish expiration date, which will control whether this node will be seen in the view results or not. This field is also a "view only" field, and its default value can be set as needed - for the node creation date, or even 10 days ahead (giving some automatic trial time for each node).
Now, while the content type seems to be ready, there should be added additional information:
- Product publishing interval - each product should specify the length of publishing it provides to the node. To do so we added an Interval field (well, again, made by Lee...) to the product variation we are working with.
- Line item node reference - each order in Drupal Commerce is built from line items, when line items are in most cases refer just for a product being sold and quantity. In our case, we would need to refer from the order (via the line item) to the node, so when the order is paid, we will know which node to update.To do so, we added an entity reference field to the line item type we are using, and when we add the product to the cart, we will need to update the node as reference as well.
It seems that data-structure wise, everything is ready. All we need to do is to find a simple way to add the node to the cart, with the selected subscription product the user selects.
Add to cart ?!?
In order to add a product to the cart, especially with the separation between "product display" and "product" (each display can have different variations, which actually seems to be the case we have as well), it seems that using the "add to cart" form can be a simple solution.
Our goal is to make things as simple as possible, and it includes the need to show the user a simple view of its "busineses", their expiration dates, and the ability to subscribe each of them easily.
An article like that even made us think it is possible and even simple, but in the end we decided to look for another solution for couple of reasons:
- While we show list of nodes, we want to "add to cart" the products referenced from it. The existing "add to cart" mechanism does not suit it nicely.
- It requires coding.While coding is fun and we "know" coding, we always try to simplify things to a level of a non-programmer. All the "hook_" solutions is great, as long as in the end the non-programmer who manage the site can make changes in the logic without the need to change code.
"Like always", the solution seems to come from Rules. We found the Button field - add it to your content type, configure it, and what you get is an image or HTML button that pressing on it will trigger a Rules event. Voila :-)
So we added to our "business" content type two buttons - one for "subscribe weekly" and one for "subscribe monthly", and accordingly created two rules.
In the rule, we do as follows:
- In the condition we verify which button is pressed, and whether the node of the pressed button has the needed product reference.
- We create new "line item" entity, set the product reference to the one we want, and the node reference as well, and then...
- Oops, there is no "Add a line item to the cart" action !"You want to add a line item to the cart ? Give us the SKU, we will do it for you !"Really ?!?So, this is exactly the place where we do code, as long as this code creates a reusable and needed generic code...This patch adds "Add a line item to the cart" action, seems to work as expected, and we hope it will be reviewed and added to the module soon.
- Ready ? Then redirect the user to the /checkout
And this is it !
(almost)
Next we will describe how to make a standard product a subscription one in Paypal (spoiler: Rules!), and how to handle recurring payments.
Part I: the requirements and existing modules Part III: Paypal subscription and recurring payments
Ralates to: Drupal Planet