#2842924: Add a product condition added a condition that allows limiting by product.
Ideally we'd also be able to limit by variation after selecting a product ("Applies to all variations" / selected specific variations).
Space was left in $this->configuration['product'] to allow for that.
The main problem is figuring out the UI.
Matt wanted to be able to select individual attributes if the variation type supports them, but that can result in a potentially huge form.
I'm more inclined to just go with an autocomplete shown when the "Applies to all variations" is unchecked.
Alternatively it might make sense to think about a UI that opens a modal for selecting a product & variations, that updates the settings form when closed.
Comment | File | Size | Author |
---|---|---|---|
#44 | commerce-condition_plugin_purchased_entities-2887061-44-do-not-test.patch | 22.89 KB | ELC |
#44 | commerce-add_default_configuration_2887061-44.patch | 1.48 KB | ELC |
| |||
#38 | interdiff-2887061-36-38.txt | 1.3 KB | mglaman |
#38 | introduce_a_conditio-2887061-38.patch | 21.1 KB | mglaman |
| |||
#32 | Screen Shot 2020-07-27 at 2.50.34 PM.png | 59.97 KB | mglaman |
Comments
Comment #2
Sinan Erdem CreditAttribution: Sinan Erdem commentedHi Bojan,
Just an idea; the condition creation part of Drupal 7 Rules was very easy to use. You would:
1. Add a new condition
2. Select the field as tokens like: node:author:email
3. Select the values either as fixed values or as tokens.
Do you think it is applicable here? Like:
1. Add condition
2. Select product attribute
3. Select desired values
Comment #3
chrisrockwell CreditAttribution: chrisrockwell commentedI don't think this is the direction that this issue is going to do but I'm attaching the plugin I'm using for now, I need something quick for allowing selecting variations. It's a copy of
OrderItemProduct
adapted for variations. A clear shortcoming is that someone could select variations, and then restrict to a product that doesn't have those variations and the coupon would never apply. Maybe this can help someone in the meantime.Comment #4
chrisrockwell CreditAttribution: chrisrockwell commentedShould be
target_id
if you use this :) Argh, there are some other issues in the evaluate method - just ping me if you want the updated stuff :DComment #5
GoZ CreditAttribution: GoZ at Barbe-Rousse for Capgemini commentedHere is another way to limit by variations. Once products are selected in autocomplete field, variations are loaded as pre-checked checboxes field.
Now product limitation only check variations, no more products.
Comment #6
GoZ CreditAttribution: GoZ at Barbe-Rousse for Capgemini commentedJust figure out this patch should add a hook_update or deal with old product way to evaluate conditions
Comment #8
GoZ CreditAttribution: GoZ at Barbe-Rousse for Capgemini commentedOf course, some tests are also needed, so i think we should update this to make old behavior works without hook_update (previous tests should pass) + add new tests for variations
Comment #9
lawxen CreditAttribution: lawxen at Sparkpad commentedOrderItemProduct.php has been changed on 21 June 2018 in issue Rename conditions to remove the "Limit by" prefix,
#5 can't be applied to latest commerce module, so I can't test the effect of it.
Comment #10
lawxen CreditAttribution: lawxen at Sparkpad commentedComment #11
lawxen CreditAttribution: lawxen at Sparkpad commentedAll of this is too complicated.
Consider that every condition is "or", so we can just add another condition OrderItemVariaiton/OrderVariation, more simple more better.
Just like #3 did.
Comment #12
lawxen CreditAttribution: lawxen at Sparkpad commentedAdd "OrderItemVariatoin,OrderVariation,OrderItemVariationTest,OrderVariationTest"
Just some copy of "OrderItemProduct,OrderProduct,OrderItemProductTest,OrderProductTest"
Comment #13
bojanz CreditAttribution: bojanz at Centarro commentedThis issue now has several competing approaches.
The problem with #5 is that products have a potentially high number of variations, and this patch code outputs a checkbox for each one.
5 products with 50 variations each result in 250 checkboxes added to the page. Even 50 checkboxes would break the UI. Checkboxes also make it impossible to differentiate between "All variations are allowed" and "All currently created variations are allowed" (when a new variation is created, is it then allowed automatically or not?)
I tried to convert the checkboxes into a multiple select box, but multiple select boxes suck (select-variations-multiple.png), they are a very non-obvious UI element (having to hold shift to select, etc). One more example of how not having Select2 hurts us in Drupal.
#12 adds a separate condition (select-variations-12.png). This highlights the problem from #2985571: Emphasize that conditions configured on the "Offer" level add up instead of restrict, it becomes less obvious that the conditions are additive, my colleague's hunch was that he should specify both the product (in "Specific product") and the product variations (in "Specific product variations"). This would have still worked (due to the ORing), but it shows that the UI is not fully logical. Might still be our best bet, though.
Comment #14
Cheviot CreditAttribution: Cheviot commentedHope I'm not suggestion something impossible given the existing structure in the promotion menu.
I have great difficulties to figure out how to use the existing taxonomies used within the existing variations. It seems to me impossible in the current structure. I haven't found any use for 'categories'. In the current concept the taxonomies seems to brought into focus for use on every level irregardless if they are associated to product or variations. But why do I see no result in 'categories'?
To have a more elegant process, should it not be a nested query process to limit the query size?
And how about selecting stores?
Would this concept still be possible? I show some ideas.
First select store X (click and query follows and finds nested items - if null 'no product type found')
select product type X (same - if null 'no product variations found')
select the available (same - if null 'no taxonomies found')
select the available taxonomy
select product type Y
select the available variation
select the available taxonomy
First select store Y
select product type Z (click and query follows, multiple nested items found - if null 'no product type found', taxonomy not mentioned)
select a product type related taxonomy
select the available variation
select the available taxonomy
And separately select AND / OR discounted quantity.
And separately select AND / OR value totals
That way even a massive list can be reduce to show only what is selected / exists.
But it deviates a lot from the current structure. It's just an idea which may improve it a bit.
Comment #15
jsacksick CreditAttribution: jsacksick at Centarro commentedI think the approach in #12 is the right one, I closed #2971381: Add a product SKU condition in favor of this.
The patch needs minor tweaks though. We store the UUIDS for the "order_item_product" condition, and use the entity uuid mapper. We should do the same. Otherwise the patch looks good, especially now that #3083123: Product variation autocomplete should allow matching by SKU is in (since that allows searching by title + SKU).
The only potential problem I see is that I'm wondering if customers wouldn't expect the search to be limited to products selected (If "applies to selected products is also checked and products are selected).
So wrapping it up.
Comment #16
jsacksick CreditAttribution: jsacksick at Centarro commentedComment #17
jsacksick CreditAttribution: jsacksick at Centarro commentedComment fix :p.
Comment #18
jsacksick CreditAttribution: jsacksick at Centarro commentedComment #19
mglamanCouldn't we just get the UUID from the product variation and check from the configuration instead of querying for the mapped identifier?
Again, couldn't we just use the
uuid
method to check, without having to map the UUIDs?+1 on trait, helps folks build custom conditions.
Comment #20
jsacksick CreditAttribution: jsacksick at Centarro commentedComment #21
jsacksick CreditAttribution: jsacksick at Centarro commentedComment #22
mglamanAt this point, do we get
getVariationUuids
or just anisValid
that gets the UUIDs and does the in_array comparison?Comment #24
jsacksick CreditAttribution: jsacksick at Centarro commentedI renamed
getVariationUuids()
toisUuidValid()
(could not come up with a better name.The problem with naming the method this way is that it could potentially conflict with someone else's code (since it's a Trait).
@mglaman: better naming in mind?
Comment #25
mglamanLooks good. 🤣another thing: should we just have "isValid" and pass the purchased entity?
Comment #26
jsacksick CreditAttribution: jsacksick at Centarro commentedWe actually still need
getVariationIds()
that is used to set the default value of the field.Comment #27
jsacksick CreditAttribution: jsacksick at Centarro commented@mglaman: I just realized, what about other purchasable entity types? Should we provide support for that? Using deriver perhaps? technically, the only main difference is the entity reference field that needs to target the right entity type. The rest should just work?
Comment #28
mglamanSo we'd basically call these "purchased_entity"
We label them as "Specific @label" where @lavel is the plural label from the entity type definition.
It's a lot of tedious work (not hard, but lot of changes.) But it technically is the "right" way since we have purchasable entity from the order item not just variations.
Comment #29
jsacksick CreditAttribution: jsacksick at Centarro commentedI agree, I think we need to do this the right way and be consistent with the rest of Commerce which technically supports different purchasable entity types.
Comment #30
jsacksick CreditAttribution: jsacksick at Centarro commentedSo setting this back to needs work.
Comment #31
mglamanWrapping this up today
Comment #32
mglamanOkay, here is it converted to a purchasable entity plugin with a derived, in the Order namespace. The Product module performs an alter to "fix" the category.
I don't know why we opted for a mapping here instead of just a sequence? We use a sequence for the
order_store
plugin and removes the need forarray_column
.The trait feels kind of weird. I'd rather a shared base class. One that just sets the configuration form up, still.
Moving the config from a mapping to sequence simplifies this. Probably removes the need for the method, even.
Missing doc block
PHPCS violations
Comment #33
mglamanComment #34
jsacksick CreditAttribution: jsacksick at Centarro commentedI wondered the same while working on the
order_item_variation
condition plugin, and I did this because it matched what was done for products, but I see no reason to not switch to a simple sequence instead.Regarding the trait, feel free to try with a different approach (but once again, that is the pattern that was used for products, and product types.
Comment #35
mglamanHere is a reroll dropping the trait for a base class, it feels a bit cleaner.
Comment #36
mglamanHere is another go, which refactors the schema storage and evaluation method.
Comment #37
jsacksick CreditAttribution: jsacksick at Centarro commentedThis looks good, few nitpicks.
The entity type manager :).
This should be PurchasedEntityConditionDeriver.
The parameter should be $entity_type_maanger.
Comment #38
mglamanCaught in the act of a bold face copy paste.
Comment #40
mglaman🥳 Committed!
Comment #41
21piyush CreditAttribution: 21piyush commentedHi,
After update the Drupal core to Drupal 8.9.3 after that re-apply the patch and got the error as
An HTTP AJAX error has occurred. HTTP status code: 200 Debugging information below. Path: / promotion / 1 / edit? Destination = / admin / commerce / promotions & ajax_form = 1 StatusText: parsererror ResponseText: "
can you please help to solve me this asap as the site is already live and we update it and got this error.
Thank you
Comment #43
pieterdt CreditAttribution: pieterdt commentedHi,
Not sure if this is related to the fact that the patch works against 2.x, but even though it applies to 2.20, it doesn't work.
I had to fix L83 in PurchasedEntityConditionBase.php:
Without that fix, I get an error in the logs that the second argument for mapToIds is null instead of an array.
Also, while reviewing the patch, I noticed a typo in line 90 for PurchasedEntityConditionBase.php should be
instead of
see https://git.drupalcode.org/project/commerce/-/blob/8.x-2.x/modules/order...
Comment #44
ELC CreditAttribution: ELC as a volunteer commentedThis code needs a bit more polish re #43 to avoid errors for both new and existing promotions after this code is added.
The configuration issue needs to have the
entities
key returned as part ofdefaultConfiguration()
to get rid of the errors.The typo is obvious.
Attached is an additional patch for current dev state (essentially an interdiff) plus the full patch.
Comment #46
jsacksick CreditAttribution: jsacksick at Centarro commentedGood catch, not sure how this has been unnoticed for that long :). Thanks!
Comment #47
mglaman🤦🏼♂️sorry, everyone.
Comment #49
ryanfc78 CreditAttribution: ryanfc78 commentedI am wondering if there can be a bit of an expansion on this. In my store with Drupal 8/9 I have a Product Attribute of Department Name. Each product variation has a drop down where that product is assigned a Department Name (because some products are only available in certain departments). I want to apply a promotion with a coupon code where I can limit that code to only work for products that have that department name assigned to them. A similar example would be I also have a Product Attribute for size. If I wanted to just limit that Promotion Code by the size XXL I don't see a way to do that in the current restrictions. Other than selecting every single SKU but that would take ages with the amount of products I have. Unless I am missing something.