Hi, i have a product and i have successfully attached a custom text field as an option.
I have updated the shopping and checkout forms appropriately.
Everything works great. However it seems there is no! way to access the field in a PHP execution code from the completed order ($commerce_order) (Rule action)
In this rule i use for example $line_item_wrapper->commerce_product->title->value() to successfully get the title (using entity_metadata_wrapper($commerce_order).
But it seems there is no way!! to get $line_item_wrapper->commerce_product->field_myrequest.
Although dpm() shows this property on rutime it generates a "data property" doesn't exist error!
How can i access my product custom option?
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | 0001-lineitem-options-property-first-shot.patch | 7.11 KB | geek-merlin |
Comments
Comment #1
geek-merlindaring to raise this, it really is a very custom use case for options.
Comment #2
geek-merlinto summarize:
option set definitions (bundles) are referenced from a field in the product.
(each option set bundle can have fields.)
when a product add-to-cart is rendered, widgets for all fields for all referenced option set bundles are added.
on add-to-cart option sets are created with a reference to the generated line item.
what we need is a backref from the line item to the option sets and their fields.
note: fields can be attached to multiple option sets.
options:
1) create an lineitem property that returns an array of corresponding option sets.
but this may be cumbersome as searching an option set means iterating over an array.
edit: also it turned out that rules does not get a wrapper for array items so we can't access their properties
so how do we make the list of option sets be searchable by the name of the option set?
* can we make this array keyed by the name of the option set?
* or do we need something else like an intermediate onthefly entity or struct?
think we should add a namespaced propertiy for every option set bundle and return the wrapped option set.
we have to care to cache-clear the corresponding cache on option set changes.
Comment #3
olafkarsten commentedI'm not so familiar with this project's code. But I think option sets are simple entities attached per reference field to another entity (the product). I guess that is a fairly common pattern in the drupal7 world?? So we shouldn't have to invent something new.
axel? Or do I miss something?
axel, are you working on the issue?
Comment #4
geek-merlini'm working on it and i'm sure.
re-read my notes (will edit them) and maybe the source!
Comment #5
phobosk commentedHi i think my problem is related to this bug, but i am not sure.
Great module btw.
I have this configuration:
1. The customer makes a reservation for a date and leaves a phone number for contact.
2. I had created an option set with the two fields -> date(using Date module) and a text field (for the phone number).
3. The option set is referenced in a product type.
4. A product is created using the product type from 3.
5. I have filled in some completed orders for the product. And it shows and works ok in cart, orders etc.
Now i need to have an Order View (listing all orders) that uses as fields the date field of the option set(formatted as date), so this field could be used in a Calendar or FullCalendar (both are other modules that need as input at least one field formatted as date) Format of the View.
Problem is i cannot access the date field of the option set alone. I can only access the Product Attributes that list all the fields used in the set...(Relationship is set to the Line item reference etc..)
How can i do this, or is it something that you work on to be implemented?
Thanks.
Comment #6
geek-merlin#5: sounds more like views integration
updating title
Comment #7
phobosk commentedSo is it:
And are you working on it if 1 or 2?
Comment #8
geek-merlini am working on this issue which i consider a bug.
what you tell in #5 sounds like 3) or 1) which should be filed as a separate issue instead of hijacking this one (bad bad karma).
Comment #9
phobosk commentedWell i do not think i hijacked it... lol
I have tried all possible variations of context and relationships in the views... but the only way to see the separate fields of the option set is if i use it as a base when creating a new view :D
So there is some problem with exposing the option set fields to the views i assume ;)
Comment #10
dornea.nu commentedsubscribing
Comment #11
olafkarsten commentedThese "subcribing" posts are not longer necessary. Please read Stop subscribing, start following.
Comment #12
dallas.masters commentedHas there been any recent work on this? I'm very interested in this too. I find options to be much easier than customizable products, but I need access to the option fields in some rules. Thanks!
Comment #13
geek-merlingood you ask - here is my work in progress.
first idea was to implement a property that returns an array of all option-sets. this has some flaws, one is probably a rules issue, the other is that it is difficult to get a specific option.
i think i'll take that out again.
then i implemented a property per option set name which i happily use.
it won't work if you attach more than one option set with the same name.
(but that should be excluded in the module anyay).
feedback appreciated.
Comment #14
tuccio commentedI tried patch #13 but then when I try loading an Option Set entity by property, the property value select list is empty and I can't go on.
Comment #15
krlucas commented@olafkarsten in #3 is correct. This module should not be trying to maintain references to products or line items itself. Instead it should require or recommend entityreference. Doing so would eliminate most if not all needs for Rules or Views integration.
Comment #16
tuccio commentedI debugged the fetched entity with Krum and the selected value is like: fetched_entity->CommerceOption->field_your_option_set_name['und'][0]['value'] where field_your_option_set_name is whatever name you gave to your specific option set. Not what you'd call a user-friendly approach to retrieve a checkout-time user-selected option.
Comment #17
tuccio commentedBasically, commerce options are not attached to products but to line items. When you fetch a commerce option, you need to match it to the line item ID for the current order.
So eg I have a rule component that takes line items as input, and where I fetch a commerce option, then I match the line item ID of the current line item to the commerce option line item id (all in php). This is damn complicated for anyone, not to speak of non-programmers. I hope there will be some true Rules integration progress on this side.
Comment #18
summit commentedHi @tuccio, can you display your rule component?
Thanks a lot in advance.
greetings, Martijn
Comment #19
tuccio commentedI pass line items as input / parameter to the component in a loop.
Then for each line item input passed to the component, I get the line item ID as a variable.
Then inside the component, I fetch the entity: Commerce Option, by property: Line Item ID and value = the line item ID input / parameter variable from above.
At this point, I have nailed the Commerce Option record corresponding to the line item being passed to the component.
The job is half done here. To finish, you need to dpm() the fetched Commerce Option to learn the machine name of your option set.
You set your component output / provided variable to the option value corresponding to the line item id with a php snippet and you are done.
It should not be so damn involved, of course. In any case, dpm() is the savior.
The main point is to stick to the line item id when you fetch the commerce option entity..
Otherwise, if you fetch the commerce options by product id like I was doing previously (as opposite to fetching them by line item id), product commerce options by product id grow indefinitely for each user choice, in that a record is stored for each user choice of the option in relation to that product, and you cannot fetch all of them in the long run.
In the "fetch entity by property" screen, you can set a value for "LIMIT RESULT COUNT Limit the maximum number of fetched entities", but no matter how big, the result count will keep growing unless you nail the specific line item id you need. Not to speak of the performance inpact with large results. So don't fetch the Commerce Option by product ID, but by line item ID.
Hope this helps.
Comment #20
Anonymous (not verified) commentedThere is better Entity API integration in the new dev release. This should normally give you more configuration options with rules as well.
Comment #21
lionguard commentedEven in the newest 2.x build I had issue retrieving my option sets, so I wrote the following simple rule - just create a file called commerce_option.rules.inc and paste the following code into it:
Comment #22
bjcooper commentedI found the reason this doesn't work! The Product Option module has a repeated typo in
commerce_option_entity_property_info(), where it sets "schema_field" instead of "schema field" (check the hook doc here). The element is set correctly for the first few properties, but not "line_item_id", "created", and "changed":Therefore, Rules can't fetch Commerce Options by their "line_item_id" property, even though that option is available in the Rules UI. Commerce does that fetch using
entity_property_query(), which then determines that the entity property isn't queryable since it doesn't have a "schema field" element and returns NULL. Checkout the second line of the function in entity.property.inc:I think if we just fix the
commerce_option_entity_property_info()hook implementation to use "schema field", everything will be just fine.Comment #23
bjcooper commentedOpened a new issue for this here: https://www.drupal.org/node/2868437
Comment #24
4kant commentedI still couldn´t get a working condition of a certain value of a selected option in any kind of rule...
Any example from scratch (just to be sure everything is in place) would be very much appreciated.
Thanks in advance!