I would like to create a new node for each line item in the order and populate fields in the new nodes with content from the line items.

I am having two problems. First, it does not seem to loop through the list items, it just does the first one. Second, an error occurs preventing me from using the commerce-unit-price.

Here is the rule I set up: (note I have created a new node type: Voucher)

Event: 
Completing the checkout process

Conditions: 
None

Actions: 
- Loop. Parameter: List: [order:commerce-line-items]
- Create a new entity. Parameter: Entity type: Node, Content type Voucher, Title: [list-item:line-item-label], Author: [site:current-user]
- Set a data value. Parameter: Data: [voucher-created:field-deal-cost ,Value: [list-item:commerce-unit-price:]

This does not work for me. In fact, I can not save the rule with the settings in "Set a data value". It gives the error:
The integrity check failed with the following error message: "action data_set: Invalid data selector list-item:commerce-unit-price: specified."

So just for testing I replaced "list-item:commerce-unit-price" with "list-item:line-item-id". This populated correctly.

Here is the export of the rule:

{ "rules_create_voucher_for_consumer" : {
    "LABEL" : "Create Voucher for Consumer",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_checkout" ],
    "ON" : [ "commerce_checkout_complete" ],
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "order:commerce-line-items" ] },
          "DO" : [
            { "entity_create" : {
                "USING" : {
                  "type" : "node",
                  "param_type" : "voucher",
                  "param_title" : [ "list-item:line-item-label" ],
                  "param_author" : [ "site:current-user" ]
                },
                "PROVIDE" : { "entity_created" : { "voucher_created" : "created voucher" } }
              }
            },
            { "data_set" : {
                "data" : [ "voucher-created:field-deal-cost" ],
                "value" : [ "list-item:line-item-id" ]
              }
            }
          ]
        }
      }
    ]
  }
}

So my questions are:

1- Any idea on what I am doing wrong in setting up the loop?

2- Any reason why I can't select fields from the line items?

Comments

rszrama’s picture

Status: Active » Fixed

The error would be the reason it's not going past the first list item. A data selector should never end with a :. This is an indication there's something else you need to select, usually a property of the item immediately preceding the :. In your cast, you should probably select amount. Once you fix that, I'd suspect your loop will work. Instead of using the site:current-user you'll probably want to use the user of the order the line item is on.

Also, the reason you can't access line item field data is explained in this FAQ:

http://www.drupalcommerce.org/faq/rules-field-data-selection

Good luck!

zeryn’s picture

Thanks.

I discovered the reason why only the first node was saved. I needed to add the Action "Save entity" after the action "Create entity" and mark it as "Force saving immediately". It was actually looping just fine but it only saved once at the end.

(For others like me who are new to Rules, using the action, "Show a message on the site", and turning on "Debug rules evaluation" in Rules Settings was very helpful.)

I'm still trying to solve getting info from the line items. I imported your sample but it is based on the event "Calculating the sell price of a product" that has the line items available. My Rule is in "Completing the checkout process" when I try to add a "Data comparison" in the conditions I can't select the "line-item:type" to compare with "Product". In addition, when I try to use "Entity has field" I can't select the field I want.

I need to get the line items available in the "Completing the checkout process" (I assume). I tried "Fetch entity by id" for "Line item" and feeding it the "[list-item:line-item-id]" thinking the data would be there. But I was not successful. I'm sure I'm just not understanding Rules correctly.

I don't mind using PHP to access the content directly but I was hoping to do it with Rules if it is possible. I just don't know enough about Rules to know if I should keep trying or just do it with PHP.

rszrama’s picture

You can't check the type of the list-item perhaps? Perhaps you can just use "Entity has field" on the list-item? I didn't mean for you to import the Rule there, just understand the need to ensure a field exists w/ the "Entity has field" before trying to use it.

zeryn’s picture

Thank you so much for pouring your time and skill into this awesome code. Thank you even more for your patience to work through it with those of us that are clueless.

I added the "Entity has field" but it still didn't work. I saw the "Add a variable" and decided to see if I could create a variable with the right info in it. It worked. Then when I tried to set my field to the value of that variable it didn't show up in the list either! So I copied the actual name that it gave me (list-item:commerce-unit-price:amount) when I set the variable and I pasted into my field. That is when I got an error that showed me my problem. It is embarrassing, it said something like: "Not an integer". My field was defined as an integer and the price amount obviously needed a float. I'm sure that is why it would not show it to me in the lists. Sheesh.

So to others that find this, if your fields don't show up in the list, make sure your data types match.

Thanks for sticking with me as I waded through finding my silly error.

rszrama’s picture

No worries. Rules is a little more uptight about variable types than PHP is, that's for sure. I've been burned by integer vs. float, too. Interestingly, if you've specified in Rules that something is a float (well, what Rules calls a decimal), it will work fine as an integer, too... it would be so easy for it to just round the other way around. : P

Status: Fixed » Closed (fixed)

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

amirtaiar’s picture

That's really grate and help me so much! Thank you fo that.
I have succeed in creating node for each item in the cart but didnt manage to do so with the field and help will be grate.

I have created a term reference field in both my product entity and the content type.
It's a size one.
From what I understand I need to apply the choose I have made during purchase and apply it on the new node created for this purchase.
So I added an action "set a data value" >>
In the data to be modified I choose the term reference in the new content type I have, set to - [entity-created:field-size-type].
Then the misunderstood part - In the data value I need to set the term from the product, yeh? Couse I cant find this field here! I can only see - entity-created:field-size-type which isn't helping me here...

jcdub’s picture

I had the same issue trying to create multiple nodes. Thanks for sharing.

zephirus’s picture

Thanks! This saved my day (actually, my night :D)