Update the Stores field definition for promotions to allow users to specify "all stores" by not selecting any specific stores. When loading available promotions, promotions for "all stores" will be available in all stores.

Essentially, we are changing the definition of the stores field for promotions from a list of stores for which the promotion is valid to a limiting condition.

From:

The stores for which the promotion is valid.

To:

Limit promotion availability to selected stores.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

lisastreeter created an issue. See original summary.

lisastreeter’s picture

lisastreeter’s picture

Status: Active » Needs review
mglaman’s picture

+++ b/modules/promotion/commerce_promotion.install
@@ -115,3 +115,29 @@ function commerce_promotion_update_8203() {
+
+/**
+ * Update the 'stores' field.
+ */
+function commerce_promotion_update_8204() {
+  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
+

We don't need to invoke the field definition manager.

We could just do something like this

// An empty update will flush caches, forcing block_rebuild() to run.

We really just need the entity field definition cache to be rebuilt

lisastreeter’s picture

Updated the patch to remove the field definition update.

lisastreeter’s picture

Status: Needs review » Needs work

Current patch does not work for promotions with coupons. Didn't realize there was separate availability logic for promotions with coupons vs. promotions without. Working on a fix to the logic plus test coverage now.

lisastreeter’s picture

Updated the logic in Promotion::available() method for all stores option. Added test coverage to CouponTest and PromotionAvailabilityTest.

lisastreeter’s picture

Status: Needs work » Needs review
lisastreeter’s picture

Rerolled patch.

lisastreeter’s picture

jsacksick’s picture

Two comments:
The following // An empty update flushes caches, forcing block_rebuild() to run. is weird? I don't really understand what's the relationship between the empty update hook and block_rebuild()?

Also, I'm wondering if we shouldn't hide the "stores", behind a checkbox. It's probably unclear as is whether the promotion is available to all stores, no?

jsacksick’s picture

Shouldn't the update hook contain the following instead?

\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();


mglaman’s picture

There are no storage changes. Just definition updates to the label and required, which requires a field cache rebuild. So maybe we should just call that in post_update with EntityFieldManager

mglaman’s picture

Also, I'm wondering if we shouldn't hide the "stores", behind a checkbox. It's probably unclear as is whether the promotion is available to all stores, no?

So "Limit to specific stores", check that. Then show the entity reference. Otherwise it defaults to all stores. Kind of like specifying and end datetime?

jsacksick’s picture

So "Limit to specific stores", check that. Then show the entity reference. Otherwise it defaults to all stores. Kind of like specifying and end datetime?

Exactly, though a bit unsure about the label itself, but I like the idea of being explicit in the UI.

jsacksick’s picture

Also, posting what was discussed on Slack regarding the empty update, we probably simply need the following:

\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();

This is clearer and more explicit (haven't tested though).

jsacksick’s picture

The attached patch addresses the previous comments.

The stores checkboxes are now hidden behind a "Limit to specific stores" checkbox.
The EntitySelectWidget was modified to generically support hiding the checkboxes/autocomplete behind a checkbox.

The update hook simply clears the cached field definitions.

I haven't really reviewed the tests closely, focused on making these 2 changes.

This is how it looks now:

jsacksick’s picture

FileSize
4.73 KB

Attaching an interdiff.

jsacksick’s picture

I wonder if we shouldn't add a margin to the checkbox, because currently it looks like the checkbox belongs to the order types.

lisastreeter’s picture

FileSize
19.93 KB
13.86 KB

@jacksick Yes, I noticed the checkbox looking like an order type. I think it might be better to have the "Stores" label above the checkbox.

Add promotion

Also, when I went back to edit an existing promotion, the checkbox disappeared:

Edit promotion

I deleted those stores from the text box. When I submitted the form, I got an error:
Notice: Undefined index: target_id in Drupal\commerce\Plugin\Field\FieldWidget\EntitySelectWidget->massageFormValues() (line 166 of modules/contrib/commerce/src/Plugin/Field/FieldWidget/EntitySelectWidget.php).

It did make the update, and then the checkbox reappeared.

I get the same error if I check the checkbox but then neglect to enter any stores.

jsacksick’s picture

Also, when I went back to edit an existing promotion, the checkbox disappeared:

This is actually by design (copied logic used for the end date), the checkbox #access is set to FALSE whent the field has a value.

I added a margin to the checkbox, I can't really reperoduce the warning, but the attached patch should do the trick.

Instead of the margin, might be better to output the checkbox under the "Stores", but it's still bettr than it was with the margin.

jsacksick’s picture

FileSize
10.42 KB
1.01 KB

The change in massageFormValues() was problematic, it is fixed.

Also keep the checkbox when a promotion references stores already, it feels weird to have it disapear.

jsacksick’s picture

Rather than "Limit to specific stores", I think I'm more in favor of "Restrict to specific stores", thoughts?

mglaman’s picture

+1 to Restrict to specific stores. Limit feels weird. Restrict is more forward.

lisastreeter’s picture

I agree. "Restrict to specific stores" is better.

The increased spacing between the order types and the checkbox is good, and I like that the checkbox does not disappear now. I do not mind the "Stores" after the checkbox at all now.

Also, I've tried various combinations of the checkbox, adding, deleting stores. No error messages, and all the updates worked properly.

jsacksick’s picture

Updated the label, is it good to go now? RTBC? :p

  • jsacksick committed 05ae27b on 8.x-2.x authored by lisastreeter
    Issue #3006403 by lisastreeter, jsacksick, mglaman: Allow a promotion to...
jsacksick’s picture

Status: Needs review » Fixed

Went ahead and committed the patch! Thanks everyone!!

mglaman’s picture

🥳 beautiful, three years in the making

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.