Howdy,

I'm very impressed with the thought that has gone into the new commerce module. I've successfully setup a price rule to discount the unit price on a product based on the line-item quantity. Nice.

What I'd like to do, is to be able to use a product field in the rule condition for use in a discount. A specific example is to calculated a 20% if the product is being purchased 30 days before it's 'commencement date'; i.e. an Early Bird discount rule for a conference.

Currently, in the data selector on the pricing rule condition, there are a set of the line-item fields that can be used. As far as I can tell, there is no way to pick a product field for use in the data selector.

I guess that there is some interface that Commerce is exposing to Rules for Rules to be able to use line-item fields in it's data selector.

I'd love to have a go to extend this to for data selector fields against a product. I'm new to commerce and rules, but willing to chip in if I can.

Any pointers on where to get started with this? What files in commerce or rules to start looking at?

Cheers,
Simon

Comments

rszrama’s picture

Status:Active» Closed (works as designed)

Hey Simon, it's actually already possible. The thing with Rules is just that for accessing field data in data selectors, you first have to ensure the field exists. You can do this one of two ways:

  1. Add an "Entity has field" condition to check to make sure your line item field has the commerce_product field (i.e. the product reference field on the product line item).
  2. Add a "Data comparison" condition that checks to see if the line item's type is "Product". By checking the bundle property of an entity, all of its fields will be available for subsequent data selectors.

A word about price pre-calculation, too... if you need to be able to "compile" a table of pre-calculated product prices so you can do things like sort a View based on the calculated price, you actually can't reference the product in the conditions. To do this, you'd have to use a Rules component to essentially create a subroutine that checks the product and applies the discount in the component rule's actions. We'll have more documentation on that available soon. ; )

thesurfinganalyst’s picture

Hi Ryan,

Thanks for the info.

I tried the check technique described in the NodeOne screencast ("http://nodeone.se/blogg/learn-rules-with-nodeone-part-7-conditions-on-lo..." ) which didn't seem to populate the fields in the data selector. I can't remember specifically why it didn't work. Again, probably PEBKAC.

I don't have requirement to build a Product view that includes or filters on price. From what I understand, I don't need to worry about pre-calculation at all. Is that right?

I'll try your suggestions for populating the data-selector today.

Cheers,
Simon.

rszrama’s picture

Yep, you shouldn't need pre-calculation. The important thing to remember at the moment is that it's not a caching / performance mechanism so much as a functionality mechanism (though I suppose it could be used as a performance mechanism... haven't benchmarked or explored that possibility yet). This is subject to change of course, but the immediate problem we were trying to solve with it is that of querying against calculated prices.

maestrojed’s picture

I am pretty sure this post is the key to my issue but I just can't get it to work. Maybe someone can help me out.

I have added fields to my product entities called "North American price" and "European price". I needed these different fields because the difference in price is not a consistent formula. In order words I just can't apply a 10% discount or tax.

I have used domain access which is how I will know which price to show. I am fairly sure I can work through that with conditional rules.
I know I can make a rule to set the price to a specific value.
I also know there is a bug that keeps rules from seeing these custom fields but the explanation above is suppose to be a work around.

Here is what I have tried:
I added a conditional "Entity has Field"
The data selector is "line-item:
The Field is "field_product_price_north_america"

I added a second conditional "Data Comparison"
The data selector is "line-item:type"
equals Product

When I add an action to change the price to a specific value
The data selectors does not include "field_product_price_north_america" like I thought it would.

Any suggestions?

I am also considering this approach http://drupal.org/node/1104068

Thanks,
maestrojed

MickL’s picture

i dont get my custom product fields too. i tried #1.

i would like to have 2 different prices for each product. same problem like #4 :(

MickL’s picture

Status:Closed (works as designed)» Closed (cannot reproduce)
MickL’s picture

Status:Closed (cannot reproduce)» Active
rszrama’s picture

Status:Active» Closed (works as designed)

@benchi, let's not re-open this issue. It is in fact working as designed. The specific follow-up is really a misunderstanding of the instructions. You should never use a data selector ending in a colon, like "line-item:". That's technically not selecting anything. The particular field you're looking for is attached to the product on the line item. To then find fields on the product once you know it exists, you'd have to check the type of the product itself just like you do for the line item.

So the logic goes like this:

  1. Check to make sure the line item is a product line item. This gives you access to commerce-line-item:commerce-product.
  2. Check to make sure the product has the field you're looking for.

This may or may not work within a single rule. Submit a support request in the Rules issue tracker if it doesn't, because there was at one time a bug that prevented you from accessing the field data of referenced entities.