How to hide or remove field options in a custom line item field?
Let’s say that I have a product type, which have location as a attribute. As location has many optional cities, I can not use it as a normal product attribute as it will generate too many product variation. So I add the location to a custom line item of this product type. The location field is a term reference field of location Taxonomy. I added over 30 cities to this location Taxonomy. I created a Customizable Products and Product display base on this custom line item.
Then I created product A, and product B, the problem is product A only available at city A and city AA, and product B only available at city B and city BB. But on the product display, the location field will display all the 30+ cities for all the products.
So that is it possible to hide or remove the other cities from the custom line item location field which not need for a special product?
I have tried rules on action when node viewed, but the customer line item not available for the product display and also tried other rules without success.
Any suggestions about this issue? I think it will be a common problem for many commerce shops.
Thanks a lot.
Comment | File | Size | Author |
---|---|---|---|
#5 | commerce_custom_product_field_values.zip | 9.6 KB | wqmeng |
#1 | commerce_custom_product_field_values.zip | 9.77 KB | malberts |
hide-field-options-from-custom-line-item-field.png | 9.63 KB | wqmeng |
Comments
Comment #1
malberts CreditAttribution: malberts commentedI was working on something like this. The idea is that you have a field on the Product that defines the allowed values for the field on the Line Item.
I attached some code that I've written for this. It is not in a sandbox or available elsewhere, so obviously this code is not claimed to be stable or anything like that. Also, you'll be responsible for maintaining your copy of it. Having said that, it should be a very good base for you to work off of.
How to use it:
- Add your "source" field to the Product. You could add something like an 'Allowed Locations' field. This field must reference the same thing as your Line Item field. When you create your Product A you then choose City AA (or any other number of allowed values).
- On the Line Item you add the "target" field.
- On the target field settings "Add to Cart form settings" do the following:
-- Mark "Include this field on Add to Cart forms for line items of this type."
-- You will then see a list "Get allowed values from" where you select the Product types that may alter the allowed values.
-- Then in the "Source fields" fieldset below, you select the field on the Product type that will alter the allowed values.
The allowed values displayed on the line item form will now be reduced to only those that are present in both the line item field allowed values AND the field values added on the product.
This code should work for any field that uses hook_options_list() for its allowed values.
One thing that won't be obvious: If the source field for a product instance is not populated, then the target field on the line item will be hidden. So if you want to show all the allowed values without selecting them all, do not use this new setting.
Test this and let me know if it works or you.
Comment #2
wqmeng CreditAttribution: wqmeng commentedHello malberts,
Thanks a lot for the new module,
I have tested, everything is working as expected. Only on the product display page, the Location list on my product not show any Allowed values.
Such as in a product allowed location filed, I have selected Los angeles, San diego, After I save the product, I linked the Source field of the Location filed in the custom line item to be the allowed location.
Then I refresh the product display, the Location can not show any value. I think there is something miss.
I will back tomorrow and test it more.
Thanks
Comment #3
malberts CreditAttribution: malberts commentedDoes the field show on the product display page, but the list is empty? Or is the field hidden completely?
I think I only tested it with List fields and with Entity Reference fields. I assume you used the Term Reference field? It could be possible that it has a different structure somewhere.
Your best bet would be to throw some dpm()'s in commerce_custom_product_field_values_form_commerce_cart_add_to_cart_form_alter() to see what is going on in the variables. Specifically, see what is in $allowed_values after line 152.
I just noticed an error in that function anyway. I set a value on line 154 which prevents the else on line 162 from ever triggering.
Comment #4
wqmeng CreditAttribution: wqmeng commentedHello malberts,
From my site, It throw this errors, yes I use term reference. I think the problem is also cause by the term different structure.
I will look into it after a while.
Thank you very much.
Comment #5
wqmeng CreditAttribution: wqmeng commentedHello malberts
I have added some code to show the Term Reference field. Please check if it is useful for you.
Thanks a lot.
Comment #6
malberts CreditAttribution: malberts commented@wqmeng: I cannot test your change right now. But does the modified code now work properly for you (showing only the correct items)?
Comment #7
malberts CreditAttribution: malberts commentedI am closing this issue as "won't fix" because I don't think the functionality required here is necessarily in scope for the module. Commerce Customizable Products is primarily concerned with allowing line item types to be modified from the back-end.
Comment #8
wqmeng CreditAttribution: wqmeng commentedYes,
It works now.
Comment #8.0
wqmeng CreditAttribution: wqmeng commentedfix
Comment #9
nvahalik CreditAttribution: nvahalik at Centarro commentedLooks like this is resolved.