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 all commits
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 of <em>{{ purchased_entity_label }}</em> changed in the cart from {{ original_quantity }} to {{ quantity }}.</p>'

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();
}

}
19 changes: 18 additions & 1 deletion modules/log/tests/src/Kernel/CartIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class CartIntegrationTest extends CommerceKernelTestBase {
'commerce_log',
'commerce_product',
'commerce_order',
'commerce_test',
];

/**
Expand Down Expand Up @@ -141,6 +140,24 @@ public function testRemovedFromCart() {
$this->assertText("{$this->variation->label()} removed from the cart.");
}

/**
* Tests that a log is generated when a quantity changed for an order item.
*/
public function testQuantityChangedFromCart() {
$this->enableCommerceCart();
$cart = $this->cartProvider->createCart('default', $this->store, $this->createUser());
$order_item = $this->cartManager->addEntity($cart, $this->variation);
$order_item->setQuantity(2);
$this->cartManager->updateOrderItem($cart, $order_item);

$logs = $this->logStorage->loadByEntity($cart);
$log = end($logs);
$build = $this->logViewBuilder->view($log);
$this->render($build);

$this->assertText("Quantity of {$this->variation->label()} changed in the cart from 1.0 to 2.");
}

/**
* Enables commerce_cart for tests.
*
Expand Down