Discussions

Set multiple field values with rules... (Order ref. on Product)

Hi. I'm trying to create an Order reference on Products when ever a new order is created.

I have installer the "Order Reference" module and created a Order reference filed on my produce type.

Next i created the following rule:

Event:

1) After saving a new commerce line item

Conditons:

1) Data comparison ( [commerce-line-item:type], Data value: Product )
2) Data comparison ( [commerce-line-item..., Data value: NAME OF TYPE )

Actions:

1) Set data value [commerce-line-item..., Value: [commerce-line-item:order]

The above rule works fine as long as i limit the Order Reference field to 1 value, but as soon as it is set to multiple the rule breaks.. i get the following error for the action:

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

I'm new to Rules and are kind a lost here.. Have tried to solve this for several days now, so any help would be much appreciated! :)

Posted: Mar 3, 2014

Comments

RasmusKnabe on March 3, 2014

Below the exported Rule in case that helps:

{ "rules_test_test_order_ref" : {
"LABEL" : "test test order ref",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "entity" ],
"ON" : { "commerce_line_item_insert" : [] },
"IF" : [
{ "data_is" : { "data" : [ "commerce-line-item:type" ], "value" : "product" } },
{ "data_is" : {
"data" : [ "commerce-line-item:commerce-product:type" ],
"value" : "iq_test_event"
}
}
],
"DO" : [
{ "data_set" : {
"data" : [ "commerce-line-item:commerce-product:field-order-reference" ],
"value" : [ "commerce-line-item:order" ]
}
}
]
}
}

RasmusKnabe on March 13, 2014

As a new approach i have tried to create the action programatically.. Think I'm on the right track but can't figure out how to set the field value of the order reference field on the product.. It's a multiple value field showing checkboxes..

Here my code:

function commerce_set_product_order_ref($line_item, $order) {
if (in_array($line_item->type, commerce_product_line_item_types())) {

$sku = $line_item->line_item_label;
$product = commerce_product_load_by_sku($sku);
$orderID = $line_item->order_id;

$wrapper = entity_metadata_wrapper('commerce_product', $product);

$wrapper->field_order_reference - AND THEN WHAT?
$result = $wrapper->save();

}
}

RasmusKnabe on March 18, 2014

Figuret out how to do the action in code.. Below the code if anyone else should need it :)

function example_rules_action_info() {
$actions = array();

$actions['commerce_set_product_order_ref'] = array(
'label' => t('Link an order to the purchased product'),
'group' => t('Custom group'),
'parameter' => array(
'commerce_line_item' => array(
'type' => 'commerce_line_item',
'label' => t('Line item'),
),
),
);

return $actions;
}

function commerce_set_product_order_ref($line_item, $order) {
if (in_array($line_item->type, commerce_product_line_item_types())) {

$sku = $line_item->line_item_label;
$product = commerce_product_load_by_sku($sku);
$orderID = $line_item->order_id;
$wrapper = entity_metadata_wrapper('commerce_product', $product);
$curr_list = $wrapper->field_order_reference->value();
$new_ref = commerce_order_load($orderID);

if (!$curr_list){
$curr_list = array();
}

$curr_list[] = $new_ref;
$wrapper->field_order_reference->set($curr_list);

$result = $wrapper->save();

}
}