Selling Per-Book Access With Drupal Commerce

Selling Per-Book Access With Drupal Commerce

Outcome required was: Once checkout process gets completed User should get the access of perticular book for which he/she has ordered.

Best help I got is from the http://commerceguys.com/blog/selling-node-access-drupal-commerce which sells the Node and I had to sell the whole Book(bunch of nodes).
Initially I went with the flow of the above link of commerceguys and I could achieve exactly what they have mentioned in their tutorial.

This was the positive sign for me that I am going on the right path through which I will be able to get the exact result what I want.But what happens in the above tutorial is they only sell a node and not the whole book.You can say a bunch of nodes together.

And for Selling all nodes together issue I got help from the module https://drupal.org/project/node_access_book.Which resolved my issue completely and helped me to get the behavior what I wanted.

Step By Step process how I got this working.

You can watch the tutorial(http://commerceguys.com/blog/selling-node-access-drupal-commerce) which will give you 60% of the things working.

==>Before Starting go to /admin/people/permissions/roles and Create a new role called "Subscribed".

1) We have Content Access and its submodule Content Access Rules enabled.
Also download and enable node_access_book module.

We need 3 things:
a) Product Display
b) Products
c) Book Page

2) Enable book module.
3) Create a content type called Product display.
4) Go to store->products->Product types and go to manage fields of "products".

** Do not forget to do this ** :
go to content type-> Book Page-> Access Control.
Remove all the access from every user except for the admin and select the checkbox for "Enable per content node access control settings".

Basically we need a page which refers to the Store Product & Store Product should refer to a book's Parent Node.
For this,

5) Go to Content type->Product display & create a field "Product reference"(You can use any name), Field type "Product reference" and widget "Select List".(Keep Number of Values 1 only).

6) Go to store->products->Product types->Products->Manage Fields & create a field "Premium Node"(You can use any name), Field type "Node reference" and widget "Checkbox/Radio".
--> Select "Book Page" for Content types that can be referenced & Keep Number of Values 1 only.

Now, We need to set the Rules.(We will Create Two Rules)

Rule.1)

7) Go to Config->Rules and Add new Rule.
Name : Change the Order Status on Order Completion (You can use any name).
Tags : Not Required.
React on Event : Completing the checkout process
Click on "Save".

8) No need of giving any conditions.
In the Actions,click on "+ Add Loop" and you should be getting "commerce-order:commerce-line-items",just after that in "Variable label *" type "Line Item" & in "Variable name *" type "line_item".
Click on "Save".

Loop will check all the line Items available in the perticular order but what should be done in the action when it enters into the loop will be done in the Components.

9) Go to Config->Rules->components and Add new Component.
Select "Rule" and Click on "Continue".
Name: Component to Change Order Status (You can use any name).

Below you can see the table.
--Data Type : Commerce Line Item
--Label : Line Item
--Machine Name : line_item

Click on "Save Changes".

10) Conditions(2 conditions needs to be added):
--1) Click on "+ Add Condition" and select "Entity has Field", Data Selector: line-item , Field Value: commerce_product and click on "Save".
--2) Click on "+ Add Condition" and select "Entity has Field", Data Selector: line-item:commerce_product , Field Value: field_premium_node and click on "Save".
NOTE: field_premium_node is the field which is in Store->product types->products and which refers to the book's parent node.So select the field which is showing in your Products Manage fields and saved as Node Reference.

11) Actions:
--1) Click on "+ Add Action" and select "Add user role", Data Selector: site:current-user , Roles Value: Subscribed and click on "Save".
--2) Click on "+ Add Action" and select "Update the order status", Data Selector: line-item:order , Order Status Value: Completed and click on "Save".

Now the order Status will be updated to "Completed" state and we can now set the permission to the node referenced by the Product Node.

13) Go to rule you just created now.where the Loop was added in action.Click on "add action" which is next to Loop and select "The_component_you_just_created" in Select the action to add and click on "Save".

Fig: Rule1.

Fig: Component For Rule1.

Rule.2)

14) Go to Config->Rules and Add new Rule.
Name : Grant Access to Book on Order Completion (You can use any name).
Tags : Not Required.
React on Event : Completing the After updating an existing commerce order
Click on "Save".

15) Click on "+ Add Condition" and select "Data comparison", Data to compare: commerce-order:state , Data value: Completed and click on "Save".

In the Actions,click on "+ Add Loop" and you should be getting "commerce-order:commerce-line-items",just after that in "Variable label *" type "Line Item" & in "Variable name *" type "line_item".
Click on "Save".

16) Repeat Steps 9 and 10 here.
but change the Name of component when you create.

17) Actions:
Click on "+ Add Action" and select "Grant access by user", Content: line-item:commerce-product:field-premium-node:book , GRANT VIEW ACCESS : line-item:order:owner, and remove everything from GRANT UPDATE ACCESS & GRANT DELETE ACCESS
and click on "Save".

18) Perform the Step 13 again for this rule.

Fig: Rule2.

Fig: Component For Rule2.

Done with the configuration of everything.

Create a book, Product Display content and a product content.
--Product display will refer to the Product(Store Product)
--Product(Store Product) will refer to the book.

Hope this will be helpful.