This question was originally posted in the drupalcommerce.org site, but have had no responses, so hopefully this is an appropriate forum: (original post: http://www.drupalcommerce.org/comment/6634#comment-6634)

Background / Setup:

I am attempting to use rules to modify the price of a product based on the value of a field in a referenced entity. I cannot get the field to show up in the rules conditions.

I'm using Drupal 7, commerce, registration, and commerce_registration.

I have a Content type "Event" that has a Product reference field "Register".
My Product reference field references a product type "Event Registration".
My "Event Registration" Product type has a Registration field "Event registration".
To collect registrant data, I have put fields on my Registration type, and these fields show up when proceeding to "Checkout", allowing users to provide their registration information.
These fields also show up on the Commerce review pane, allowing customers to review their registration information (very nice).

I run into trouble with the client requirement "Allow for different price points for types of tickets". E.g. General tickets cost $15, Student tickets cost $10, etc... I cannot seem to figure out how to get the fields into scope so I can use rules' conditions and actions to modify the price.

It *seems* like I need to implement hook_entity_property_info_alter() to tell rules "Hey! I've got these fields, and here's where they are in the database.". I created a mini-module that will dpm() the properties provided by hook_entity_property_info_alter(), but I'm not really sure where to go next. Am I able to update the entity metadata here so that rules will know about it? Ugh.

I've read:
http://drupal.org/node/1053850 (which has more subscribers than our local newspaper!)
http://www.trellon.com/content/blog/creating-own-entities-entity-api (um...just out of my reach without a bit of help)
http://drupal.org/node/1081800#comment-4240420 and followed the steps (#13 - I've tried to follow along, but I simply can't get the referenced fields into scope)

Comments

jameswoods’s picture

I ended up patching the registration module to add hook_registration_event_count_alter() and then wrote a small module that counts up all the registrations so I can have my price points AND limited seating.

/**
* Implementation of hook_registration_event_count_alter().
* This hook was added to registration/registration.module approx line 628.
* I'd love to claim credit, but I got the hook from mlsamuelson 
* http://drupal.org/node/1904412
* 
* One of the requirements of this site is to have multiple price points 
* available when registering for an event (e.g. General $15, Student $10, etc.).
* AND limit the number total number of seats (not simply a limit per price 
* point).
* 
* Due to the "Why can't I access my custom fields in Rules?" bug discussed at
* http://www.drupalcommerce.org/comment/6634 I was unable to use the Rules 
* module to adjust the prices based on the type of ticket being purchased.
*
* I was able to use this new hook and EntityFieldQuery to find all products
* that share the same registration type and sum up all of the registrations.
* Setting $count to that total has the effect of allowing us to have a 
* limited number of "seats" at an event and still offer differnt price points.
* 
* $count is the number of registrations from all products that share the same 
* registration type.
*/
function regtweaks_registration_event_count_alter (&$count, &$context) {
  // Ge the entity of the event we are acting on
  $entity_id = $context['entity_id'];
  $node = entity_load($context['entity_type'], array($context['entity_id']));
  $registration_type = $node[$entity_id]->field_event_registration['und'][0]['registration_type'];
  
  // Find all the entities that use the same registration type
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'commerce_product', '=')
    ->fieldCondition('field_event_registration', 'registration_type', 
      $registration_type, '=')
    ->addMetaData('account', user_load(1));

  $result = $query->execute();

  if (isset($result['commerce_product'])) {
    $product_nids = array_keys($result['commerce_product']);
  }

  // Sum up all the registrations on all the entities that share
  // the same registration type
  $query = db_query("SELECT COUNT(registration_id) as 'registrations'
                       FROM registration
                      WHERE entity_id IN (:ids)", 
                      array(':ids' => $product_nids)
                   );  
  $result = $query->fetchAll();
  if (isset($result)) {
    $count = $result[0]->registrations;
  }
}

TravisJohnston’s picture

Hello James,

I stumbled on your post while looking for a way to bring a Product Display into scope in a Rule after checkout completes. After reading your question, even though this is months later, did you happen to look at https://drupal.org/project/commerce_pricing_attributes?

jameswoods’s picture

I did...and I don't remember the particulars. I do know that the registration, commerce_registration, and commerce_pricing_attributes modules didn't work together in a way that suited my needs. Sorry I don't have details for ya 8^(

blacklabel_tom’s picture

Status: Active » Closed (won't fix)

Hi,

This is a very old issue, but I would suggest you have different products at different prices rather than changing the price on checkout.

Additionally getting fields into scope in Rules isn't something this module can fix as you will need to check the entity is of type bundle or that the specific field exists in Rules before you can use it.

Cheers

Tom