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

Aggiunta Funzionalità #9

Closed
wants to merge 5 commits into from
Closed
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
8 changes: 7 additions & 1 deletion config/zendcart.global.php.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?php
return array (
'zendcart' => array (
'vat' => '21'
'vat' => '21',
/**
* If true, when insert is called and an item is already present in the cart (item with the same id attribute), an update is performed and the cart item quantity is updated.
* false otherwise.
* default: false, to mantain original ZendCart->insert() behaviour
*/
'on_insert_update_existing_item' => false
)
);
114 changes: 99 additions & 15 deletions src/Zendcart/Controller/Plugin/ZendCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@

use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\Session\Container;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\EventManager;
use Zendcart\Event\CartEvent;

class ZendCart extends AbstractPlugin
class ZendCart extends AbstractPlugin implements EventManagerAwareInterface
{

/**
Expand All @@ -29,6 +33,11 @@ class ZendCart extends AbstractPlugin
*/
private $_config;

/**
* @var $eventManager
*/
protected $eventManager;

/**
* __construct
*
Expand All @@ -38,6 +47,7 @@ public function __construct($config = array())
{
$this->_config = $config;
$this->_session = new Container('zfProducts');
$this->setEventManager(new EventManager());
}

/**
Expand All @@ -55,7 +65,8 @@ private function _cart($items = array())
'price' => $this->_formatNumber($items['price']),
'name' => $items['name'],
'options' => isset($items['options']) ? $items['options'] : 0,
'date' => date('Y-m-d H:i:s', time())
'date' => date('Y-m-d H:i:s', time()),
'vat' => isset($items['vat']) ? $items['vat'] : $this->_config['vat']
);
}

Expand Down Expand Up @@ -215,15 +226,44 @@ public function insert($items = array())
{
if ($this->_checkCartInsert($items) === TRUE)
{
$token = sha1($items['id'].$items['qty'].time());

if (is_array($this->_session['products']))
{
$this->_session['products'][$token] = $this->_cart($items);
} else {
$this->_session['products'] = array();
$this->_session['products'][$token] = $this->_cart($items);
$isNew = true;
$shouldUpdate = $this->_config['on_insert_update_existing_item'];

//check if should update existing product
if($shouldUpdate){
$products = is_array($this->_session['products']) ? $this->_session['products'] : array();
foreach ($products as $token => $existing_item) {
if($existing_item['id'] === $items['id']){
//fount same product already on cart
$isNew = false;
$items = array('token'=>$token, 'qty'=> $existing_item['qty']+$items['qty']);
break;
}
}
}

if($isNew){
$token = sha1($items['id'].$items['qty'].time());

if (is_array($this->_session['products']))
{
$this->_session['products'][$token] = $this->_cart($items);
} else {
//creo il carrello in sessione
$this->_session['products'] = array();
$this->_session->cartId = $this->_session->getManager()->getId();
$this->getEventManager()->trigger(CartEvent::EVENT_CREATE_CART_POST, $this, array('cart_id'=>$this->_session->cartId));
//aggiungo elemento
$this->_session['products'][$token] = $this->_cart($items);
}
//evento per elemento aggiunto
$this->trigger(CartEvent::EVENT_ADD_ITEM_POST, $token, $this->_session['products'][$token], $this);
}else{
//update existing product
$this->update($items);
}

}
}

Expand All @@ -240,6 +280,7 @@ public function update($items = array())
if ($this->_checkCartUpdate($items) === TRUE)
{
$this->_session['products'][$items['token']]['qty'] = $items['qty'];
$this->trigger(CartEvent::EVENT_UPDATE_QUANTITY_POST, $items['token'], $this->_session['products'][$items['token']], $this);
}
}

Expand All @@ -253,9 +294,11 @@ public function update($items = array())
*/
public function remove($items = array())
{
if ($this->_checkCartRemove($items) === TRUE)
if (($this->_checkCartRemove($items) === TRUE) && isset($this->_session['products'][$items['token']]) )
{
$cart = $this->_session['products'][$items['token']];
unset($this->_session['products'][$items['token']]);
$this->trigger(CartEvent::EVENT_REMOVE_ITEM_POST, $items['token'], $cart, $this);
}
}

Expand All @@ -268,6 +311,7 @@ public function remove($items = array())
public function destroy()
{
$this->_session->offsetUnset('products');
$this->getEventManager()->trigger(CartEvent::EVENT_DELETE_CART_POST, $this, ['cart_id'=>$this->_session->cartId]);
}

/**
Expand All @@ -291,7 +335,8 @@ public function cart()
'name' => $value['name'],
'sub_total' => $this->_formatNumber($value['price'] * $value['qty']),
'options' => $value['options'],
'date' => $value['date']
'date' => $value['date'],
'vat' => $value['vat']
);
}
return $items;
Expand Down Expand Up @@ -331,17 +376,22 @@ public function total()
if ($this->_isCartArray($this->cart()) === TRUE)
{
$price = 0;
$vat = 0;
foreach ($this->cart() as $key)
{
$price =+ ($price + ($key['price'] * $key['qty']));
$item_price = ($key['price'] * $key['qty']);
$item_vat = (($item_price/100)*$key['vat']);
// $price =+ ($price + ($key['price'] * $key['qty']));
$price += $item_price;
$vat += $item_vat;
}

$params = $this->_config['vat'];
$vat = $this->_formatNumber((($price / 100) * $params));
// $params = $this->_config['vat'];
// $vat = $this->_formatNumber((($price / 100) * $params));

return array(
'sub-total' => $this->_formatNumber($price),
'vat' => $vat,
'vat' => $this->_formatNumber($vat),
'total' => $this->_formatNumber($price + $vat)
);
}
Expand All @@ -362,4 +412,38 @@ public function item_options($token)
return $this->_session['products'][$token]['options'];
}
}

public function getEventManager()
{
return $this->eventManager;
}

public function setEventManager(EventManagerInterface $eventManager)
{
$eventManager->setIdentifiers(
'ZendCart\Service\Cart',
__CLASS__,
get_called_class(),
'zendcart'
);
// $eventManager->setEventClass('ZendCart\Service\Cart');

$this->eventManager = $eventManager;
return $this;
}


private function trigger($name, $token, $cartItem, $target=null)
{
$cartId = $this->_session->getManager()->getId();
$event = new CartEvent();
$event->setCartId($cartId)
->setItemToken($token)
->setCartItem($cartItem);

if ($target)
$event->setTarget($target);

$this->getEventManager()->trigger($name, $event);
}
}
112 changes: 112 additions & 0 deletions src/Zendcart/Event/CartEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* ZendCart
* Simple Shopping Cart
*
* @package ZF2 Modules
* @category Plugin
* @copyright 2013
* @version 1.0 Beta
*
* @author Stefano Corallo <stefanorg@gmail.com>
*/
namespace ZendCart\Event;

use Zend\EventManager\Event;

class CartEvent extends Event {

const EVENT_ADD_ITEM = 'addItem';
const EVENT_ADD_ITEM_POST = 'addItem.post';
const EVENT_REMOVE_ITEM = 'removeItem';
const EVENT_REMOVE_ITEM_POST = 'removeItem.post';
const EVENT_CREATE_CART = 'createCart';
const EVENT_CREATE_CART_POST = 'createCart.post';
const EVENT_DELETE_CART = 'removeCart';
const EVENT_DELETE_CART_POST = 'removeCart.post';
const EVENT_EMPTY_CART = 'emptyCart';
const EVENT_EMPTY_CART_POST = 'emptyCart.post';
const EVENT_UPDATE_QUATITY = 'updateQuantities';
const EVENT_UPDATE_QUANTITY_POST = 'updateQuantities.post';


protected $cartId;
protected $itemToken;
protected $cartItem;


/**
* Gets the value of cartId.
*
* @return mixed
*/
public function getCartId()
{
return $this->cartId;
}

/**
* Sets the value of cartId.
*
* @param mixed $cartId the cart id
*
* @return self
*/
public function setCartId($cartId)
{
$this->setParam('cartId', $cartId);
$this->cartId = $cartId;

return $this;
}

/**
* Gets the value of cartId.
*
* @return mixed
*/
public function getCartItem()
{
return $this->cartItem;
}

/**
* Sets the value of cartId.
*
* @param mixed $cartId the cart id
*
* @return self
*/
public function setCartItem($cartItem)
{
$this->setParam('cartItem', $cartItem);
$this->cartItem = $cartItem;

return $this;
}

/**
* Gets the value of itemToken.
*
* @return mixed
*/
public function getItemToken()
{
return $this->itemToken;
}

/**
* Sets the value of itemToken.
*
* @param mixed $itemToken the item token
*
* @return self
*/
public function setItemToken($itemToken)
{
$this->setParam('itemToken', $itemToken);
$this->itemToken = $itemToken;

return $this;
}
}
10 changes: 7 additions & 3 deletions src/Zendcart/Factory/ZendCartFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ public function createService(ServiceLocatorInterface $servicelocator)
throw new \Exception('No vat index defined.');
}

return new ZendCart(array(
'vat' => $config['zendcart']['vat']
));
$default = array(
'on_insert_update_existing_item' => false
);

$options = array_merge($default, $config['zendcart']);

return new ZendCart($options);
}
}