I have attached my first draft of commerce_option.rules.inc

This document adds CONDITIONS ONLY for integration with rules. Meaning you can now modify a price based on product options.

This may be a little bit hacky... If anyone has suggestions, i'll gladly take them and improve upon this.

I'm also wondering what sort of rules actions could be expected from this module.

Comments

hunziker’s picture

Status: Needs work » Postponed (maintainer needs more info)

Can you explain a bit how you use them? Can you give me a use case?

kbalderson’s picture

I am currently using these rules to change price in cart based on boolean selections.

So, if a product has a boolean set to giftwrap a product, add $5 to the price.

It seems that this might have been possible using product bundle, but I couldn't get that module to install because of #1190208: Error when enabling product bundle

hunziker’s picture

I didn't like the approach to use for all checkbox things the bundle product, because this can lead to a huge list of products.

So your approach is absolutly viable. The reason why do not really understand, why we need here separate condition is simple. Because the commerce_option is implemented by the Entity API all the functionality of it is available also for the options. So you could check if some fields are set etc.

So what do your conditions ahead to the default conditions? This is not really clear to me.

kbalderson’s picture

My conditions make the fields easily accessible through rules.

Prior to creating this file, I couldn't figure out how to access the data added through this module within Drupal Commerce's pricing rules.

I came across this issue: #1053850: Fix metadata-assertions for referenced entities. which made it seem that i couldn't easily access the fields within the Commerce Option entity.

If there is a way to do this that i have missed, without giving Commerce Options special Rules integration, then leaving this out may be the best option as not to duplicate functionality.

hunziker’s picture

You could access the options by using a second rule. There you can add some assertion to access the fields. This is the general solution.

For options you could use "fetch entity by property". As the property to filter you need to use the line item. Then you can loop over the options and depending on the found option you could change the line item.

I know we can elaborate this by adding more sophisticated events / actions / conditions, but since Drupal Commerce is not really stable, we are more stable if we are using rules. (Rules complains at least if something is wrong.)

hunziker’s picture

Status: Postponed (maintainer needs more info) » Closed (fixed)
chrisnovak’s picture

Thanks for the rules integration piece orbface9. I created a checkbox attribute and when checked it will add an amount to the product price thanks to your code put in a custom module. I could not figure out any other way to get this to work.

imp7’s picture

@hunziker thanks for explaining this, do you think still think Commerce "is not really stable"?

I have been using your method of option based pricing with rules. This may work however it is a level of complexity that makes adding a lot of option specific prices unmanageable. Maybe creating a crud wrapper of some kind for these rules would be an option, (there will still be the issue of updating the display node price and cart) do you plan on integrating this feature into in some form for this module? There seems to be a demand for it.

kristofferwiklund’s picture

Status: Closed (fixed) » Active

How comes this is marked as fixed. I can not find any of this code in dev-package or git.

7thkey’s picture

It appears this is not clear at all. I've tried to do what it says Hunziker and finally I got some light with this solution and I will try to describe below.
The way I've found to access the options from rules with a simple example (Starting with a Commerce Kickstart setup):

Add and configure an Option Set

  1. Setup a new set of options: Go to admin/commerce/products/option-sets and "+Add commerce option set"
  2. Add a name to the Option Set, for example "Registration"
  3. Now that we have the new option set saved, click on "manage fields".
  4. Add a new field, I've added "Subscribe?" and field type "Boolean" with "Single on/off checkbox" widget, click on save.
  5. Next page on "Field Settings", use "Yes" and "No" for the values, click on save. Remember the field name "field_subscribe"

Attach Option Set to a product type

  1. Go to /admin/commerce/products/types and "manage fields" the default product type called "Product"
  2. Add a new field called for example "Option Ref" and the type of field "Option Set Reference" and "Select List" as widget. And now click on save the rest of the settings leaving the default values.

Add a product with our selected option set

  1. Edit one of the kickstart products or create one. Select the Option Ref "Registration"
  2. No our product shows the "Subscribe" with the checkbox "N/A, no, Yes". (Remember to add the product to a product view node)

Track the selected option in Rules

  1. Go to /admin/commerce/config/product-pricing and click on "+Add a pricing rule"
  2. Add a name for the rule and click save.
  3. Add a Condition "Data value is empty", data selector: "commerce-line-item:line-item-id" and click on "Negate" and save.
  4. Add another condition "Entity exist by property", entity type select "Commerce Option", property select "line_item_id" and value "commerce-line-item:line-item-id", save it
  5. In the last part of the rules, we gonna add some actions. Add "Fetch entity by property", entity type: "Commerce Option", property: "Line_item_id", value: "commerce-line-item:line-item-id" and we will give a name to variable name as "commerce_option"
  6. Now we add a loop, in the data selector we gonna use "commerce-option" and define list item variable as "list_options
  7. Inside the loop we add an action (***this is for testing***) "Show a message on the site" and add in the value textfield "Subscribe? [list-options:field-subscribe]"

And that's it, when you select an option in subscribe you will get a message with the selected option.
But, what I don't get it to work is to use a conditional with the selected option values.
Any help will be appreciated.

Cheers

rosso69’s picture

Is the custom module working for anyone?

I tried to calculate a price based on options. But i cannot find anything usefull.
firtst i tried : based on a custom module
#10 not working, just because i cannot find my field.

then i tried this solution : http://drupal.org/node/1081800#comment-4240420

junetellain626’s picture

Thanks a lot kyle. The commerce_option.rules.inc worked for me.

Tried to solely use rules at first but couldn't manage to drill into the fields I want to fetch in the data selectors. (Tried entity is of type/has a field for a couple of times to no avail)

Cheers. :)

rfulcher’s picture

I am sorry if this is a stupid question but what do you do with that file to make this work. I have the exact scenario that Kyle did but not sure where to place the file. Thanks for your help.

georgemastro’s picture

Check this new module http://drupal.org/project/commerce_pricing_attributes
It handles a lot of the missing features.

powersurge’s picture

@rfulcher #13

Just copy this file http://drupal.org/files/issues/commerce_option.rules_.inc_.txt to the commerce_option folder, which is most likely located in the sites/all/modules folder, and then rename the file from "commerce_option.rules_.inc_.txt" to "commerce_option.rules.inc".

Then go to http://drupal.org/node/1394624#comment-5742254 and complete the remaining steps starting with step 3 (ignore steps 1-2).

If the new condition doesn't appear in Rules, then try clearing your cache before proceeding with step 3.

enorniel’s picture

I use http://drupal.org/project/commerce_pricing_attributes

but it doesn't work for numeric fields. So I would like to create a rule for that.

I would like to know how to use the values of fields called by a pricing attribute field in rules.

I tried https://drupal.org/node/1205628#comment-6102112

but when I look at the values given in the loop, I don't see anything about the fields I put in the option set.

How can I use the values of these fields to do operations on them in rules ?

Thanks.

Anonymous’s picture

Issue summary: View changes
Status: Active » Postponed

Rules integration is on the way.

Anonymous’s picture

Basic rules integration added through proper use of Entity API.
If this is not sufficient, please open up a new issue and describe what other Rules options you lack.

Anonymous’s picture

Status: Postponed » Closed (fixed)
NatiNog’s picture

StatusFileSize
new121.28 KB

@powersurge #15

I added the commerce_options_rules.inc to the module but I hace one problem, on the

function commerce_option_rules_compare_options($lineitem, $field, $operator, $value)

I get NULL for

$lineitem->line_item_id

So the condition always returns FALSE.

I tried to fix it but couldn't. Any suggestions?