Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2846890: Create a log entry when item is removed from cart or qty is changed programatically #625

Open
wants to merge 3 commits into
base: 8.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions modules/log/commerce_log.commerce_log_templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ cart_item_removed:
category: commerce_cart
label: 'Removed from cart'
template: '<p><em>{{ purchased_entity_label }}</em> removed from the cart.</p>'
cart_item_quantity_changed:
category: commerce_cart
label: 'Quantity changed from cart'
template: '<p>Quantity for <em>{{ purchased_entity_label }}</em> changed in the cart form <em>{{ original_quantity }} to {{ quantity }}</em>.</p>'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change "Quantity for" to "Quantity of".
No emphasis around the quantities.
And "form" should be "from".


order_placed:
category: commerce_order
Expand Down
19 changes: 19 additions & 0 deletions modules/log/src/EventSubscriber/CartEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\commerce_cart\Event\CartEntityAddEvent;
use Drupal\commerce_cart\Event\CartEvents;
use Drupal\commerce_cart\Event\CartOrderItemRemoveEvent;
use Drupal\commerce_cart\Event\CartOrderItemUpdateEvent;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -34,6 +35,7 @@ public static function getSubscribedEvents() {
$events = [
CartEvents::CART_ENTITY_ADD => ['onCartEntityAdd', -100],
CartEvents::CART_ORDER_ITEM_REMOVE => ['onCartOrderItemRemove', -100],
CartEvents::CART_ORDER_ITEM_UPDATE => ['onCartOrderItemUpdate', -100],
];
return $events;
}
Expand Down Expand Up @@ -66,4 +68,21 @@ public function onCartOrderItemRemove(CartOrderItemRemoveEvent $event) {
])->save();
}

/**
* Creates a log when a quantity is updated.
*
* @param \Drupal\commerce_cart\Event\CartOrderItemUpdateEvent $event
* The transition event.
*/
public function onCartOrderItemUpdate(CartOrderItemUpdateEvent $event) {
$cart = $event->getCart();
$order_item = $event->getOrderItem();
$original_order_item = $event->getOriginalOrderItem();
$this->logStorage->generate($cart, 'cart_item_quantity_changed', [
'purchased_entity_label' => $order_item->getPurchasedEntity()->label(),
'original_quantity' => $original_order_item->getQuantity(),
'quantity' => $order_item->getQuantity(),
])->save();
}

}