Permalink
Browse files

Add total_quantity field for tracking the total sum of items.quantity…

… in your cart
  • Loading branch information...
1 parent 50b4536 commit 43fcd1a2c3c30884ee7b46ec3b5218a301bd3e22 @mykehsd committed Mar 27, 2013
@@ -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);
}
}
View
@@ -45,6 +45,13 @@ class Cart implements CartInterface
protected $totalItems;
/**
+ * Total quantity of items.
+ *
+ * @var integer
+ */
+ protected $totalQuantity;
+
+ /**
* Total value.
*
* @var float
@@ -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();
@@ -138,6 +146,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}
+ */
public function isEmpty()
{
return 0 === $this->countItems();
@@ -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>
@@ -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);
@@ -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

0 comments on commit 43fcd1a

Please sign in to comment.