bleroy / Nwazet.Commerce

Idiomatic commerce module for Orchard CMS.
BSD 3-Clause "New" or "Revised" License
26 stars 21 forks source link

Workflow Iterate Over Order Products #37

Closed bleroy closed 8 years ago

bleroy commented 10 years ago

Originally reported by: Jeffrey Olmstead (Bitbucket: ems, GitHub: ems)


Scenario: I have products that contain a registration URL field. I would like to use a workflow and send out the registration URL on the product for each product that was ordered.

Dilemma: There doesn't appear to be a way to iterate over the Order IEnumerable items within a Workflow.

Ideally: I would be able to iterate over products, expand them using something like Order.Items:1.Product.Fields.Foo.Bar to snag a field value send off one (or multiple) emails with further information around the product that was just purchased. Of course this would assume I actually know how many products are in the cart (and is thus not a perfect solution).

I have looked at C# Scripting module and am getting no where that way. Any chance anyone has ever had to do this? Any alternative approaches that work?


bleroy commented 10 years ago

Original comment by Jeffrey Olmstead (Bitbucket: ems, GitHub: ems):


Addressed (at least for Order Status Changed) in pull request https://bitbucket.org/bleroy/nwazet.commerce/pull-request/13/major-change-of-adding-promotion-tracking/diff

bleroy commented 10 years ago

Original comment by Bertrand Le Roy (Bitbucket: bleroy, GitHub: bleroy):


Looks fine to me. We'll have to add similar events for the shopping cart eventually, for analytics and other similar applications.

bleroy commented 10 years ago

Original comment by Jeffrey Olmstead (Bitbucket: ems, GitHub: ems):


Here is one simple (though very useful) solution. Create a new workflow event in OrderActivity then add this in on OrderPartDriver:

#!c#
foreach (var item in part.Items) {
    var content = _orchardServices.ContentManager.Get(item.ProductId);
    if (content != null) {
        _workflowManager.TriggerEvent("OrderStatusChangedProduct", content, 
            () => new Dictionary<string, object> {
                {"Content", content},
                {"Order", part}
            });
    }                     
}

This will fire off a workflow event for each row in the order. The "Content" will be the product and access is still given to the "Order" tokens. Will be testing the effectiveness of this for a while and recommending a pull if it seems appropriate. Feedback welcome.