1

I wonder how I can access all of the line item properties in Rules?

For example in a Rules event "Before saving a line item" I can show a debug message of the line item. It has different properties such as commerce_display_path and data->context->entity which however aren't available in Rules ie. I cannot set them as variable for later processing.

How could I make these properties available to Rules? I've tried to add different conditions to make sure the "whole entity" is loaded to Rules but still I cannot access these properties.

EDIT 1

I've now tried to develop a solution to provide this data to Rules. Following enables only entity(field collection item) id and entity revision and nothing else. I thought that it would provide the whole entity and it's properties (or at least so I understood from documentation that all that entity__property_info returns).

EDIT 2

Now I can set a message on site that presents host entity properties. However, I cannot use those properties in any other rules functionality eg. I cannot create variable from field regarding field collection item i.e. I still cannot make use of them. Why the properties are available for message but not for anything else?

function mymodule_entity_property_info_alter(&$info)
{
  if (isset($info['commerce_line_item'])) {
    $info['commerce_line_item']['properties']['host_entity'] = array(
      'label' => 'Host entity',
      'type' => 'field_collection_item',
      'description' => t('Host entity of a line item.'),
      'getter callback' => 'mymodule_get_host_entity_callback',
      'setter callback' => 'entity_property_verbatim_set',
      'computed' => true,

    );


 $bundles['field_collection_item'] = array('label'=>'Field collection item', 'properties'=>entity_get_all_property_info('field_collection_item'));
    dpm($info['commerce_line_item'], 'line item in entity info alter');
  }
}

function mymodule_get_host_entity_callback($entity, array $options, $name, $entity_type)
{
  dpm($entity, 'line item entity in Rules callback');
  $wrapper = entity_metadata_wrapper($entity_type, $entity);

  $host_entity_id = $wrapper->data->context->entity->entity_id->value();
  $host_entity_type = $wrapper->data->context->entity->entity_type->value();

  $host_wrapper = entity_metadata_wrapper($host_entity_type, $host_entity_id);
  $host = $host_wrapper->value();

  return $host;
}

function mymodule_set_host_entity_callback($entity, array $options, $name, $entity_type)
{
  $wrapper = entity_metadata_wrapper($entity_type, $entity);

  return $wrapper->host_entity->value();
}
0

Below is a possible approach you can use to solve your question. It provides the details about how I was able to have a set of Drupal messages being shown at checkout time, which look similar to this testcase I used (for a cart with 3 line items):

Current line item has label 'USB-BLU-08' and id '17'
Current line item has label 'TSH4-BLK-MD' and id '18'
Current line item has label 'MES1-BLU-OS' and id '19'

To get this to work, I created a custom rule, which looks like so (use the Rules UI to import it in your own site):

{ "rules_access_line_items_at_checkout" : {
    "LABEL" : "Access line items at checkout",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "commerce_checkout" ],
    "ON" : { "commerce_checkout_complete" : [] },
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "commerce-order:commerce-line-items" ] },
          "ITEM" : { "line_item" : "Current line item" },
          "DO" : [
            { "drupal_message" : { "message" : "Current line item has label \u0027[line-item:line-item-label]\u0027 and id \u0027[line-item:line-item-id]\u0027" } }
          ]
        }
      }
    ]
  }
}

Some details about it:

  • Rules Event: After completing the checkout process.
  • Rules Condition: none.
  • Rules Action: Create a loop, to iterate over each line item. And for each line item, show a Drupal message, starting with "Current line item has ...". Obviously, at this point you could display any of the properties related to a line item.

Easy, no?

PS: I'm assuming this question is about D7 ...

  • Hi! I need this data before saving a line item i.e. data is used for customization of line item and to be more specific to change the price. DAta is purely used in backend no need to show it via dsm except for debugging purposes. – user1271930 Jul 26 '17 at 17:51
0

There was no answer for using "API functions" and the only way to solve this was to add extra functionality to Rules via custom module.

I had to provide new data to Rules and use it in the event of "Calculate sell price". I'm not sure that if I could have triggered this event programmatically or something similar but at least now it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.