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.

Comments

rszrama’s picture

Status: Active » Closed (won't fix)

This sounds like a perfect question for http://www.drupalcommerce.org/questions. If you repost it there and provide a link in this issue, I'll be happy to come propose a solution.

xbrianx’s picture

I originally was going to post in the answers section, you can find the re-post here: http://www.drupalcommerce.org/questions/8897/how-do-i-fetch-product-auth...

But then I saw how little responses questions got and thought I might get more traction in the drupal.org forums.

clevername’s picture

I just did something similar with a custom function. This assumes there is only one product author for all items in the cart and only takes the uid from the first product in the array.

function commerce_get_seller_access_token ($order) {
  // Get product id from first order line item.
  $orders = commerce_order_load_multiple(array(), array('status' => 'pending'), TRUE);
  
foreach($orders as $order) {
  foreach ($order->commerce_line_items['und'] as $line) {
    $line_item = commerce_line_item_load($line['line_item_id']);
    $product_id = $line_item->commerce_product['und'][0]['product_id'];
  }
  //Load first product from order line item and fetch author uid
  $product_item = commerce_product_load($product_id);
  $product_uid = $product_item->uid;
}
  return $product_uid;
}
krystlc’s picture

This took me a while to figure out so I'll post my solution. Too many steps, so I'll just dump these rules for you to use at your own risk.

As explained here http://www.drupalcommerce.org/questions/8897/how-do-i-fetch-product-auth... you'll need to create a new rule component and then loop it with the line item list in your new checkout rule.

Create a new rule component admin/config/workflow/rules/components and import this rule.

{ "rules_fetch_product_author" : {
    "LABEL" : "Fetch Product Author",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "line_id" : { "label" : "Line ID", "type" : "commerce_line_item" } },
    "IF" : [
      { "data_is" : { "data" : [ "line-id:type" ], "value" : "product" } },
      { "data_is" : { "data" : [ "line-id:commerce-product:type" ], "value" : "course" } }
    ],
    "DO" : [
      { "mail" : {
          "to" : [ "line-id:commerce-product:creator:mail" ],
          "subject" : "You have a new student!",
          "message" : "Congratulations! [line-id:order:owner:name] signed up for you class!",
          "from" : "[site:mail]",
          "language" : [ "site:current-user:language" ]
        }
      }
    ]
  }
}

Secondly, create your new checkout rule here admin/commerce/config/checkout/rules with this:

{ "rules_send_email_to_mentor" : {
    "LABEL" : "Send email to mentor",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "Commerce Checkout" ],
    "REQUIRES" : [ "rules", "commerce_checkout" ],
    "ON" : { "commerce_checkout_complete" : [] },
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "commerce-order:commerce-line-items" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "component_rules_fetch_product_author" : { "line_id" : [ "list-item" ] } }
          ]
        }
      }
    ]
  }
}

Note: disregard that second data comparison in the rule component.

jOksanen’s picture

To fetch only the first product (which was perfect in my case, you can use a simpler rule).

{ "rules_send_email_to_product_owner" : {
    "LABEL" : "Send email to product owner",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "commerce_checkout" ],
    "ON" : { "commerce_checkout_complete" : [] },
    "IF" : [
      { "data_is" : {
          "data" : [ "commerce-order:commerce-line-items:0:type" ],
          "value" : "product"
        }
      }
    ],
    "DO" : [
      { "mail" : {
          "to" : [ "commerce-order:commerce-line-items:0:commerce-product:creator:mail" ],
          "subject" : "Test mail",
          "message" : "Test mail nr 1",
          "from" : "Sender",
          "language" : [ "commerce-order:state" ]
        }
      }
    ]
  }
}
nellngun’s picture

Hi krystlc, trying to make it work... What do u mean by Note: disregard that second data comparison in the rule component? I import both rules with no results.

nellngun’s picture

Hi still me, do you know how to fetch all product infrormation (all fields)?