create a new event fire when cart item is duplicated

This commit is contained in:
Manuel Raynaud
2014-04-09 17:05:18 +02:00
parent ad58b27b00
commit 48b7eb9353
4 changed files with 78 additions and 2 deletions

View File

@@ -120,7 +120,7 @@ trait CartTrait
*/
protected function duplicateCart(EventDispatcherInterface $dispatcher, CartModel $cart, Session $session, Customer $customer = null)
{
$newCart = $cart->duplicate($this->generateCookie(), $customer);
$newCart = $cart->duplicate($this->generateCookie(), $customer, $dispatcher);
$session->setCart($newCart->getId());
$cartEvent = new CartEvent($newCart);

View File

@@ -0,0 +1,70 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Event\Cart;
use Thelia\Core\Event\ActionEvent;
use Thelia\Model\CartItem;
/**
* Class CartItemDuplicationItem
* @package Thelia\Core\Event\Cart
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class CartItemDuplicationItem extends ActionEvent
{
/**
* @var \Thelia\Model\CartItem
*/
protected $oldItem;
/**
* @var \Thelia\Model\CartItem
*/
protected $newItem;
function __construct(CartItem $newItem, CartItem $oldItem)
{
$this->newItem = $newItem;
$this->oldItem = $oldItem;
}
/**
* @return \Thelia\Model\CartItem
*/
public function getNewItem()
{
return $this->newItem;
}
/**
* @return \Thelia\Model\CartItem
*/
public function getOldItem()
{
return $this->oldItem;
}
}

View File

@@ -342,6 +342,8 @@ final class TheliaEvents
*/
const CART_DUPLICATE = "cart.duplicate";
const CART_ITEM_DUPLICATE = "cart.item.duplicate";
/**
* sent when a new item is added to current cart
*/

View File

@@ -4,6 +4,9 @@ namespace Thelia\Model;
use Propel\Runtime\ActiveQuery\Criteria;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Thelia\Core\Event\Cart\CartItemDuplicationItem;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Base\Cart as BaseCart;
class Cart extends BaseCart
@@ -15,7 +18,7 @@ class Cart extends BaseCart
* @param Customer $customer
* @return Cart
*/
public function duplicate($token, Customer $customer = null)
public function duplicate($token, Customer $customer = null, EventDispatcherInterface $dispatcher)
{
$cartItems = $this->getCartItems();
@@ -62,6 +65,7 @@ class Cart extends BaseCart
->setPriceEndOfLife(time() + ConfigQuery::read("cart.priceEOF", 60*60*24*30));
}
$item->save();
$dispatcher->dispatch(TheliaEvents::CART_ITEM_DUPLICATE, new CartItemDuplicationItem($item, $cartItem));
}
}