2
Answers
Vote up!
2
Vote down!

How do I Fetch Product Author from Commerce Order With Rules

I am trying to fetch the author of products found in an order to alert them they have a new order to confirm. I am trying to use rules to create new content or send them an email. I have my current attached rules export here to view. Right now i am just trying to create a new basic node to test the concept of grabbing the product's author.

{ "rules_notify_tour_operator" : {
    "LABEL" : "Notify Author",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "Orders" ],
    "REQUIRES" : [ "rules", "entity" ],
    "ON" : [ "commerce_order_insert" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "commerce-order" ], "field" : "commerce_product" } }
    ],
    "DO" : [
      { "entity_fetch" : {
          "USING" : { "type" : "commerce_product", "id" : [ "commerce-order:order-id" ] },
          "PROVIDE" : { "entity_fetched" : { "fetched_product" : "Fetched Product" } }
        }
      },
      { "entity_fetch" : {
          "USING" : { "type" : "user", "id" : [ "fetched-product:creator:uid" ] },
          "PROVIDE" : { "entity_fetched" : { "fetched_author" : "Fetched Author" } }
        }
      },
      { "entity_create" : {
          "USING" : {
            "type" : "node",
            "param_type" : "page",
            "param_title" : "Test Message",
            "param_author" : [ "fetched-author" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      },
      { "entity_save" : { "data" : [ "entity-created" ] } }
    ]
  }
}

So far no luck any help would be greatly appreciated.
Originally found here https://drupal.org/node/2050565

Asked by: xbrianx
on July 26, 2013

2 Answers

Vote up!
0
Vote down!

I see a few issues with this rule for the purpose described. First of all, it's operating when a new order is saved, which happens in the initial Add to Cart. I think a better event for you would be when the customer completes the checkout process.

Next, you're trying to load a product using the order ID. This just won't work - products are represented on orders using line items and do not correspond to order IDs at all.

Finally, your rule assumes there would just be one product on the order. However, all orders are assumed able to contain multiple line items, so instead of just loading and acting on a single product, you're going to need to "loop" over all the line items and operate on them.

What you need to do is first create a Rules component that takes a line item parameter. This component can be responsible for ensuring the line item has the commerce_product reference field and then loading the commerce-line-item:commerce-product:creator user to notify him or her about the purchase.

Then you need to create your checkout completion rule that loops over commerce-order:commerce-line-items and passes them one by one to the component that actually determines whether or not to notify the product creator.

This process isn't for the faint of heart, so if you're new to Rules, you should check out the Rules Mastery video series on YouTube. It walks through everything there is to know about Rules and should specifically address creating and calling rules components from other rules to handle advanced use cases like this.

You may also be able to find real-time support in the IRC channels for Drupal Commerce or Rules. For more information, refer to https://drupal.org/irc.

Ryan Szrama
Answer by: Ryan Szrama
Posted: Jul 30, 2013

Comments

It took a little bit of trial and error with the correct data selectors, but I managed to get it working thanks to your guidance. Thanks!

- xbrianx on July 31, 2013