Problem/Motivation

I think a simple alter hook for add to cart message should be there so that other developers can alter the message as per their requirement.

Proposed resolution

Create an alter_HOOK_type.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

sorabh.v6 created an issue. See original summary.

sorabh.v6’s picture

Assigned: sorabh.v6 » Unassigned
Status: Active » Needs review
StatusFileSize
new1006 bytes

Patch uploaded.

joshmiller’s picture

Status: Needs review » Needs work
+++ b/modules/cart/src/Form/AddToCartForm.php
@@ -210,10 +210,16 @@ class AddToCartForm extends ContentEntityForm implements AddToCartFormInterface
+    \Drupal::moduleHandler()->alter("commerce_add_to_cart_message", $message, $cart, $purchased_entity);

Needs dependency injection.

sorabh.v6’s picture

Assigned: Unassigned » sorabh.v6
heddn’s picture

Assigned: sorabh.v6 » Unassigned

I'd like to see the position of the drupal_set_message move before, $this->cartManager->addOrderItem($cart, $order_item, $form_state->get(['settings', 'combine']));. This way I can alter the messages that are set in an event subscriber. And then we don't even need the message alter at all. Just retrieve the messages and alter them as you see fit in an event subscriber.

As an example of what you can do with that simple movement: here's https://gist.github.com/heddn/74386a203797894a84c67901cf4951d0

sorabh.v6’s picture

Status: Needs work » Needs review
StatusFileSize
new996 bytes

Replaced $purchased_entity with $order_item, because developer can extract purchased_entity from $order_item but what if he needs order_item too.

Also, moduleHandler was already injected into some parent class. So, just used that here in the updated patch. Please review.

drugan’s picture

Status: Needs review » Reviewed & tested by the community

Works as expected.

heddn’s picture

Status: Reviewed & tested by the community » Needs work

At the very least, can we move the dsm before the addtocart call so the option exists to do this either way?

sorabh.v6’s picture

@heddn Moved dsm before addToCart and created event for add to cart message. Please review the attached patch and interdiff file.

sorabh.v6’s picture

Status: Needs work » Needs review
heddn’s picture

Status: Needs review » Closed (duplicate)

@drugan thanks for the link to #2834416: Move the add to cart message to an event subscriber to allow it to be replaced/removed. I think should be closed duplicate.