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

[Order] add proper events for cart-item add and remove #1676

Merged
merged 1 commit into from
Jul 14, 2021
Merged
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
10 changes: 5 additions & 5 deletions docs/03_Development/14_Events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ CoreShop comes with a lot of build-in events.
| Name | EventType | Description |
|:-----|:------------|:----------|
| `coreshop.cart.update` | GenericEvent | Fires after cart has been updated |
| `coreshop.cart.item_add_pre` | GenericEvent | Fires before a item gets added to cart |
| `coreshop.cart.item_add_post` | GenericEvent | Fires after a item gets added to cart |
| `coreshop.cart.item_remove_pre` | GenericEvent | Fires before a item gets removed from cart |
| `coreshop.cart.item_remove_post` | GenericEvent | Fires after a item gets removed from cart |
| `coreshop.cart.pre_add_item` | GenericEvent | Fires before a item gets added to cart |
| `coreshop.cart.post_add_item` | GenericEvent | Fires after a item gets added to cart |
| `coreshop.cart.pre_remove_item` | GenericEvent | Fires before a item gets removed from cart |
| `coreshop.cart.post_remove_item` | GenericEvent | Fires after a item gets removed from cart |

## Customer

Expand Down Expand Up @@ -120,4 +120,4 @@ Replace symbol (*) with one of those controller:
There are events vor every state machine transition. Read more about it [here](../17_State_Machine/README.md).

## Model Events
You can use Pimcore Events for CoreShops Pimcore Models: [Pimcore Events](https://www.pimcore.org/docs/5.0.0/Extending_Pimcore/Event_API_and_Event_Manager.html)
You can use Pimcore Events for CoreShops Pimcore Models: [Pimcore Events](https://www.pimcore.org/docs/5.0.0/Extending_Pimcore/Event_API_and_Event_Manager.html)
15 changes: 13 additions & 2 deletions src/CoreShop/Component/Order/Cart/CartModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace CoreShop\Component\Order\Cart;

use CoreShop\Component\Order\CartEvents;
use CoreShop\Component\Order\Model\OrderInterface;
use CoreShop\Component\Order\Model\OrderItemInterface;
use CoreShop\Component\StorageList\Model\StorageListInterface;
Expand Down Expand Up @@ -56,15 +57,15 @@ public function removeFromList(StorageListInterface $storageList, StorageListIte

$this->eventDispatcher->dispatch(
new GenericEvent($storageList, ['item' => $item]),
'coreshop.cart.remove_add_pre'
CartEvents::PRE_REMOVE_ITEM
);

$storageList->removeItem($item);
$item->delete();

$this->eventDispatcher->dispatch(
new GenericEvent($storageList, ['item' => $item]),
'coreshop.cart.remove_add_post'
CartEvents::POST_REMOVE_ITEM
);
}

Expand All @@ -85,6 +86,16 @@ private function resolveItem(StorageListInterface $storageList, StorageListItemI
}
}

$this->eventDispatcher->dispatch(
new GenericEvent($storageList, ['item' => $storageListItem]),
CartEvents::PRE_ADD_ITEM
);

$storageList->addItem($storageListItem);

$this->eventDispatcher->dispatch(
new GenericEvent($storageList, ['item' => $storageListItem]),
CartEvents::POST_ADD_ITEM
);
}
}
24 changes: 24 additions & 0 deletions src/CoreShop/Component/Order/CartEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2020 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

declare(strict_types=1);

namespace CoreShop\Component\Order;

class CartEvents
{
public const PRE_REMOVE_ITEM = 'coreshop.cart.pre_remove_item';
public const POST_REMOVE_ITEM = 'coreshop.cart.post_remove_item';

public const PRE_ADD_ITEM = 'coreshop.cart.pre_add_item';
public const POST_ADD_ITEM = 'coreshop.cart.post_add_item';
}