I try to create an order through the rules module (create entity > order). On execution this leads me always to this error:

Notice: Undefined index: uid in _commerce_order_create() (line 299 of .../modules/commerce/modules/order/commerce_order.module).

Here is the equivalent source code:

<?php
function _commerce_order_create($values = array()) {
  // Create a new order owned by the specified user.
  $order = commerce_order_new($values['uid']); // <- line 299
  unset($values['uid']);
  [...]
?>

The problem is that the $values['uid'] is not set and can not be set in the rules configuration form. The entity will not be created. The other thing, I think is strange here, is that the Order Id is required. For my opinion this should be optional (is set when the order is created). Or is this way to create an order with rules completely wrong?

Comments

rszrama’s picture

Title: Error on creating entity order » Errors with creating an Order entity via Rules action "Create an entity"
Component: Order » Rules integration

Interesting... I hadn't tried this yet. Several things do appear to need changing:

  1. It's asking for an Order Number, not an Order ID, but that's no better... it shouldn't be asking for this at all, but the good thing is whatever you put in there will be ignored for now. Let's find a way to hide that, though.
  2. The order status list is pretty awful... we need them to be grouped by state and in their weight orders.
  3. As the original post indicates, we should be properly locating and passing a uid to the order creation method. Since the action requires you to specify an owner, not a uid, I'm guessing we need to extract it from there.
  4. When the action is executed, I get the failure notice "5.535 ms Unable to create entity commerce_order: Entity property type doesn't support writing." Will have to determine which property is actually causing this notice.

I'm sure we'll have similar problems with other entities...

rszrama’s picture

Just committed a patch solving #2 and #4 (it was the type property lacking a setter callback), and I've implemented a check on $values['uid'] to stop #3 from giving the PHP notice. The problem, though, was that I'm actually not being passed any of the action's settings, so I don't get any order status or owner information passed to the creation function. However, it still seems to be setting the uid properly since I'm using a separate setter callback - I tested this by setting it explicitly to user 2 and also using the data selector for site:current-user (I was logged in as user 1) and it worked fine.

I guess that really leaves figuring out how to hide the Order Number field and also how to get the Order E-mail field to show up as editable / selectable.

hunziker’s picture

rszrama thank you for your work on this. I think this module becomes one of the greatest Drupal 7 modules.

I have apply your changes from the last commit. The creation of new orders works now. But as you mentioned above the problem exists also on other entities (line-items).

I build a good test case. I try to implement a cloning mechanism for orders in Rules. Here is my test case:

{ "rules_order_clone" : {
    "LABEL" : "order clone",
    "PLUGIN" : "action set",
    "REQUIRES" : [ "rules", "commerce_line_item" ],
    "USES VARIABLES" : { "original_order" : { "label" : "original order", "type" : "commerce_order" } },
    "ACTION SET" : [
      { "entity_create" : {
          "USING" : {
            "type" : "commerce_order",
            "param_order_number" : "0",
            "param_status" : [ "original-order:status" ],
            "param_type" : [ "original-order:type" ],
            "param_owner" : [ "original-order:owner" ]
          },
          "PROVIDE" : { "entity_created" : { "new_order" : "new order" } }
        }
      },
      { "entity_save" : { "data" : [ "new-order" ], "immediate" : 1 } },
      { "LOOP" : {
          "USING" : { "list" : [ "original-order:commerce-line-items" ] },
          "DO" : [
            { "entity_create" : {
                "USING" : {
                  "type" : "commerce_line_item",
                  "param_order" : [ "new-order" ],
                  "param_type" : [ "list-item:type" ],
                  "param_line_item_label" : [ "list-item:line-item-label" ],
                  "param_quantity" : [ "list-item:quantity" ]
                },
                "PROVIDE" : { "entity_created" : { "new_order_line" : "new order line" } }
              }
            },
            { "entity_save" : { "data" : [ "new-order-line" ] } },
            { "commerce_line_item_unit_price_currency_code" : {
                "line_item" : [ "new-order-line" ],
                "currency_code" : [ "list-item:commerce-unit-price:currency-code" ]
              }
            }
          ]
        }
      }
    ],
    "PROVIDES VARIABLES" : [ "new_order" ]
  }
}

If this works we covered all features that should be available. Because all fields need to be read and written in the test case. Perhaps we can build up on this a new test case. The following list covers the errors which blocks the cloning of the order:

  1. Line Items can not be saved.
  2. The profile (address) of the order can not be assigned to the order. (This is not critical, but it should be possible.)
  3. The amount and the currency code can not be set directly in the entity creation form. This muss be done with a separate function. I have to remove the copying of the amount , because of the absence of the line item entity; it throws an exception.

Additionally there exists another error in the above use case. It is not possible to provide the new order to other rules. But this is a rules error. So I will post it in there issue queue.

derekw’s picture

Subscribing, I just got to this point.

derekw’s picture

Are orders appearing in the commerce_orders table with your action?

This is not resulting in any order for me:

Create a new entity

Parameter: Entity type: Order, Order number:0, Status: Shopping cart, Type: Order, Owner:0
Provides variables: Created order (commerce_order)

Save entity

Parameter: Entity: [order-created], Force saving immediately: true

hunziker’s picture

The order appears in my test in the list, but there are other problems. So actually it does not work.

derekw’s picture

Yes if I skip adding line items the order does get entered into the database.

rszrama’s picture

Issue tags: +dcsprint6

Just updating to confirm that yes, orders will be created and show up in your Orders View (assuming you set the status to a non-shopping cart status like Pending). Additionally, the remaining issues I point to in comment #2 above still remain.

I've imported hunziker's component, and it actually doesn't error out for me if I execute it - it just seems to ignore the line item cloning altogether. It clones the base order object fine, but that's it.

I'll mark this for review at the Commerce Camp sprint if I don't get back to it before then. In the meantime, I'd just recommend doing this in code instead of trying to construct it with Rules if it's mission critical for your site. You can always back the cloning functionality into a custom Rules action.

rszrama’s picture

Title: Errors with creating an Order entity via Rules action "Create an entity" » Errors with creating entities (i.e. an Order with Line items) via Rules

Whoops, meant to update the title, since the original issue pertaining to Orders explicitly have mostly been resolved.

rszrama’s picture

Category: bug » support

Can anyone confirm the problems on the latest versions of Entity API and Rules?

videographics’s picture

I've come across a number of problems related to setting a data value on reference fields that refer to Commerce Order entities that seem like the might be related to this issue.

I've tried setting the value of fields that are supposed to take an entity as a value but when I try to use a Commerce Order as a value, I get:

"The data type of the configured argument does not match the parameter's value requirement."

This happens with the Relation module (http://drupal.org/project/relation) and also with Entity Reference module.

The Relation module IS able to create the relation using the "Entity Collector" (but that's really awkward and only really practical for testing) and I AM able to reference the orders using Entity Reference field on the edit page of the node as well.

This is with commerce 7.x-1.0, rules 7.x-2.0, entity 7.x-1.0-beta11. I'm going to undertake testing all this with dev versions in the next day or so. Can someone tell me if they think the dev versions might address this?

videographics’s picture

SO sorry. My issues seem to be related to trying to insert a single entity into a variable where rules is expecting a list. You can pretty much ignore everything I've been saying. (gulp)

rszrama’s picture

Status: Active » Postponed (maintainer needs more info)

No worries, it's good to get that confirmation.

videographics’s picture

Actually, the array/single value issue only resolved problems with the node reference and entity reference modules. (This actually gets me what I need for my current application.) But, I'm still having trouble with Rules creating Relations between nodes and Order entities.

rszrama’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)