Hi all,

For a client we are building a Drupal 8 website with Commerce and Content-moderation.
I would like to ask you all for advice about architectural setup!
Simply stated; users can create content and it will be published after a payment has been done.

Use-case

1. User registers an account for free (drupal core)
2. Registered users can create a specific content-type (drupal core)
2a. This content-type has a taxonomy-field with several terms. Each term includes a price. (drupal core + price)
2b. This content-type has a simple content-moderation workflow attached: created/payed/published.
3. After creating the content-item, the user can start full-filling an order specific for the content-item. The order-price is the sum of the selected taxonomy-terms.

What it should become

As an admin when checking an order we expect order items a-like "Publication of node 123" by "User Y", state "Payed". Then the admin will publish the node manually.

Our initial ideas

Preferable we would love to keep all the Node and Content Moderation goodies and only use commerce/order (without commerce/product). We would like to set this up without adding to many dependency modules.

Custom Purchasable Entity

We thought to create a custom entity that implements PurchasableEntityInterface. It will be created upon the Node::postCreate event and contains a reference to the Node. This is kinda minimal and the first I will gonna start coding on.

Content Moderation

We discussed that it might be possible to use "Content Moderation" and for a specific transition create/trigger an order. When this order is payed, we trigger the content-moderation transition. This feels complex. Not only because State Machine and Content Moderation do similar things.

Commerce Product

An alternative approach might be to create a Product for every Node (1:1). The Product has a reference to the Node. We drop the Content Moderation flow. In our case we do expect a lot of different Nodes, but still it is a bit strange to create an Order and a Product for every Node created (3 entities every time).

Questions / food-for-thought

First thing that I don't get yet is what should be the "Purchasable entity"?
Is it the "Node" itself, the "Content Moderation Transition", a specific Content moderation state i.e. "Payed for publication" or a "Product" with a reference to the Node?

Second thing is that Drupal Commerce "State Machine" and Drupal Core "Content Moderation" have very similar naming (Draft/...). Not sure if we should combine those.

How do you think about this? Can this be done in a clean way?

Comments

ndf created an issue. See original summary.

ndf’s picture

Have been working on it this week.
Expect halfway February I can refactor and publish it.

bojanz’s picture

State machine is used for Commerce workflows.
Content Moderation is used for node workflows.
So if you have a node workflow, use Content Moderation, that's fine.
(We actually refactored the workflow part of Content Moderation into a state_machine-like workflows module in Drupal 8.3.x but it's too late for Commerce to use it).

We thought to create a custom entity that implements PurchasableEntityInterface. It will be created upon the Node::postCreate event and contains a reference to the Node. This is kinda minimal and the first I will gonna start coding on.

I'd go with a custom order item type that's not backed by a purchasable entity type, and has an entity_reference field for the node.
Then a custom "pay" form can be shown on the node, on submission it can create the order item, reference the node, then add the order item to the cart.
An event subscriber can react on the order being placed, take any referenced nodes, and publish them.

ndf’s picture

Had a flu last week, so everything moved a week.

Your idea with the custom order item type is a lot simpler. Thanks for the advice Bojanz!

bojanz’s picture

Status: Active » Fixed

You can also look at the DonationForm in https://github.com/bojanz/donation_example as an example of a custom form that creates a custom order item.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

_dcre_’s picture

Is it possible to use Content moderation or workflow module for products?

Use case:
A product should pass through several states until it published and therefore enabled to be bought.

How would the above case be implemented?

BR

wengerk’s picture

Hey @_dcre_,

I don't know if it could help you & other people struggling with the same issue but here is an awesome article about State Machine. You can apply the same implementation for commerce_product entity.

https://www.flocondetoile.fr/blog/set-workflows-state-machine-drupal-8

Happy coding !