Skip to content

Commit

Permalink
Add total_quantity field for tracking the total sum of items.quantity…
Browse files Browse the repository at this point in the history
… in your cart
  • Loading branch information
mykehsd committed Mar 27, 2013
1 parent 50b4536 commit 43fcd1a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions EventListener/CartListener.php
Expand Up @@ -113,5 +113,9 @@ private function refreshCart(CartInterface $cart)
{
$cart->calculateTotal();
$cart->setTotalItems($cart->countItems());
$totalQuantity = 0;
foreach ($cart->getItems() as $item)
$totalQuantity += $item->getQuantity();
$cart->setTotalQuantity($totalQuantity);
}
}
40 changes: 40 additions & 0 deletions Model/Cart.php
Expand Up @@ -44,6 +44,13 @@ class Cart implements CartInterface
*/
protected $totalItems;

/**
* Total quantity of items.
*
* @var integer
*/
protected $totalQuantity;

/**
* Total value.
*
Expand Down Expand Up @@ -74,6 +81,7 @@ public function __construct()
{
$this->items = new ArrayCollection();
$this->totalItems = 0;
$this->totalQuantity = 0;
$this->total = 0;
$this->locked = false;
$this->incrementExpiresAt();
Expand Down Expand Up @@ -135,6 +143,38 @@ public function changeTotalItems($amount)
}
}

/**
* {@inheritdoc}
*/
public function getTotalQuantity()
{
return $this->totalQuantity;
}

/**
* {@inheritdoc}
*/
public function setTotalQuantity($totalQuantity)
{
if (0 > $totalQuantity) {
throw new \OutOfRangeException('Total quantity must not be less than 0');
}

$this->totalQuantity = $totalQuantity;
}

/**
* {@inheritdoc}
*/
public function changeTotalQuantity($amount)
{
$this->totalQuantity += $amount;

if (0 > $this->totalQuantity) {
$this->totalQuantity = 0;
}
}

/**
* {@inheritdoc}
*/
Expand Down
1 change: 1 addition & 0 deletions Resources/config/doctrine/Cart.orm.xml
Expand Up @@ -19,6 +19,7 @@
<mapped-superclass name="Sylius\Bundle\CartBundle\Entity\Cart" table="sylius_cart">
<field name="locked" column="locked" type="boolean" />
<field name="totalItems" column="total_items" type="integer" />
<field name="totalQuantity" column="total_quantity" type="integer" />
<field name="total" column="total" type="decimal" precision="10" scale="2" />
<field name="expiresAt" column="expires_at" type="datetime" />
</mapped-superclass>
Expand Down
13 changes: 13 additions & 0 deletions spec/Sylius/Bundle/CartBundle/Model/Cart.php
Expand Up @@ -26,6 +26,11 @@ function it_should_intitialize_items_collection_by_default()
$this->getItems()->shouldHaveType('Doctrine\\Common\\Collections\\Collection');
}

function it_should_have_0_total_quantity_by_default()
{
$this->getTotalQuantity()->shouldReturn(0);
}

function it_should_have_0_total_items_by_default()
{
$this->getTotalItems()->shouldReturn(0);
Expand Down Expand Up @@ -63,6 +68,14 @@ function it_should_have_fluid_interface_for_items_management($item)
$this->clearItems()->shouldReturn($this);
}

function it_should_complain_when_total_quantity_is_less_than_0()
{
$this
->shouldThrow(new \OutOfRangeException('Total quantity must not be less than 0'))
->duringSetTotalQuantity(-1)
;
}

function it_should_complain_when_total_items_is_less_than_0()
{
$this
Expand Down

0 comments on commit 43fcd1a

Please sign in to comment.