Add an option to disable the add to cart message on a certain order type

Created on 27 October 2022, about 2 years ago
Updated 23 May 2023, over 1 year ago

Problem/Motivation

As a site builder, I'd like to disable the add-to-cart message for certain order types.

Steps to reproduce

In order to disable the add-to-cart message, it is currently necessary to remove the commerce_cart.cart_subscriber service using a ServiceProvider class. As a result features like โœจ Allow order types to have no carts Needs work are needless complex to implement.

Proposed resolution

One option to make this simpler is to make the cart message configurable, analogous to how order receipt mails are handled on the order type and in the OrderReceiptSubscriber.

Remaining tasks

  • Review
  • Commit

User interface changes

New fieldset and checkbox on the order type form.

API changes

API additions:

  • OrderTypeInterface::shouldDisplayCartMessage(): bool;
  • OrderTypeInterface::setDisplayCartMessage(bool $display_message);

Data model changes

Additional setting on the commerce_order_type.

Release notes snippet

โœจ Feature request
Status

Fixed

Version

2.0

Component

Cart

Created by

๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany rgpublic Dรผsseldorf ๐Ÿ‡ฉ๐Ÿ‡ช ๐Ÿ‡ช๐Ÿ‡บ

    Coming from https://www.drupal.org/project/commerce_cart_redirection/issues/3218501 โœจ Suppress the " added to your cart" message on redirect? Closed: won't fix I have 2 questions:

    * Is it really needed to have an option to set this per order-type? What is the use-case if I may ask? I have nothing against it, though. Just wondering...

    * Could anyone of the core maintainers comment if this is feasible? Otherwise we'd have to find an out-of-core solution. I definitely think solving this in core makes more sense. At least as long as it's as difficult to switch the message off as it currently is.

    BTW: Should we perhaps set this on "Needs review" to move this forward?

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany rgpublic Dรผsseldorf ๐Ÿ‡ฉ๐Ÿ‡ช ๐Ÿ‡ช๐Ÿ‡บ

    PS: Finding now #2810723 I wonder... Do we really need an additional option for this? If #2810723 ever gets implemented (switching off the cart completely per order-item), I'd assume the corresponding message disappears as well without any further configuration...?

  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol

    Re #4 and #5:

    • A global setting whether the message is displayed or not wouldn't be sufficient in our case. This is because we sell books and subscriptions. In the former case the status message is desirable, in the latter case it is not (direct checkout).
    • This issue will make โœจ Allow order types to have no carts Needs work much simpler and cleaner to implement. I am currently using the patch from the other issue. If I was a maintainer, I wouldn't want to merge that in its current state though.
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol

    Added tests and a post update hook which is turning the setting on for all existing order types.

  • @znerol opened merge request.
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol
  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡ฎ๐Ÿ‡ฑIsrael jsacksick

    @znerol: Several issues with your patch, the main issue being that the setting must be added form the cart module.

    See commerce_cart_form_commerce_order_type_form_alter commerce_cart_order_type_form_submit.

    The setting must then be accessed like the following:

          $display_add_to_cart_message = $order_type->getThirdPartySetting('commerce_cart', '<name of the setting>');
    

    But also, I'm just unsure this is a good idea to just expose settings for everything... Disabling this programatically isn't really complicated but I know writing code isn't an option for everyone.

  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol

    But also, I'm just unsure this is a good idea to just expose settings for everything... Disabling this programatically isn't really complicated but I know writing code isn't an option for everyone.

    True. Ultimately my goal is to unblock issues like โœจ Allow order types to have no carts Needs work and โœจ Make it possible to not display the add to cart message when using ajax Needs work . If another approach with the potential to advance those issues is more viable, then I'm willing to implement that. Suggestions welcome.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany rgpublic Dรผsseldorf ๐Ÿ‡ฉ๐Ÿ‡ช ๐Ÿ‡ช๐Ÿ‡บ

    But also, I'm just unsure this is a good idea to just expose settings for everything... Disabling this programatically isn't really complicated but I know writing code isn't an option for everyone.

    Well, IMHO, Drupal from A to Z is only about making everything configurable. If you look at Views, many configuration pages in Commerce etc. etc. It's a flood of checkboxes, dropdowns etc. The problem I guess is that Drupal is missing sth. viable between writing a whole new module in PHP and just setting obscure checkboxes. Sth. like "about:config" in Firefox browser, for example. But, for the time being, I still think it would be nice if this message could easily be changed, translated etc. It would perhaps be sufficient if there was a Twig template suggestion to override this message. This would also make it possible to make the message dynamic (perhaps depending on the item just added etc.). And if this template is empty (to be defined what that means - perhaps a test with trim() equals empty string) the message is suppressed altogether. If there are individual template suggestions per order type, this issue here could be solved elegantly without any new UI but also without the need for writing modules. Just an idea.

  • @znerol opened merge request.
  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol

    @jsacksick thanks for the third-party-setting suggestion. Implemented that in MR !157. This is obviously much cleaner and it doesn't require an update function.

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ฆUkraine vlad.dancer Kyiv

    Thank you @znerol. One thing to note we need to save cart_message value in submit function:

    function commerce_cart_order_type_form_submit($form, FormStateInterface $form_state) {
      //...
      $order_type->setThirdPartySetting('commerce_cart', 'cart_message', $settings['cart_message']);
    }
    
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update over 1 year ago
    783 pass
  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol

    Right, thanks for spotting that @vlad.dancer.

  • Status changed to RTBC over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ฆUkraine vlad.dancer Kyiv

    Tested manually, works for me.

  • ๐Ÿ‡ฎ๐Ÿ‡ฑIsrael jsacksick

    Patch looks good, could we maybe just move the tests to the existing AddToCartFormTest?
    We might just need the following method since we are already testing there that the ATC message is displayed:

    public function testNoAddToCartMessageWhenDisabled() {
        // Disable the add-to-cart message.
        $orderType = OrderType::load('default');
        $orderType->setThirdPartySetting('commerce_cart', 'cart_message', FALSE);
        $orderType->save();
    
        // Anonymous active cart.
        $this->drupalGet('product/' . $this->variation->getProductId());
        $this->submitForm([], 'Add to cart');
        $this->assertSession()->statusCodeEquals(200);
    
        $product = $this->variation->getProduct();
        $this->assertSession()->pageTextNotContains('added to your cart.');
      }

    I'm also just unsure about the setting name? "cart_message" sounds like a setting that holds the actual message itself, and not whether or not it is enabled.

    Maybe we should go with "enable_cart_message"?

  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update over 1 year ago
    CI aborted
  • ๐Ÿ‡ฎ๐Ÿ‡ฑIsrael jsacksick

    Moved the tests to the AddToCartFormTest and renamed the setting to "enable_cart_message".

  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update over 1 year ago
    782 pass
  • ๐Ÿ‡ฎ๐Ÿ‡ฑIsrael jsacksick

    Same patch with an updated comment for the testAddToCartMessageBehavior() method.

  • ๐Ÿ‡ฎ๐Ÿ‡ฑIsrael jsacksick

    Went ahead and committed the updated version. @znerol: thanks for your contribution!

  • Status changed to Fixed over 1 year ago
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol

    Great, thanks!

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

Production build 0.71.5 2024