Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 2 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
4 EventListener/CartListener.php
@@ -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
40 Model/Cart.php
@@ -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();
View
21 Model/CartInterface.php
@@ -43,6 +43,27 @@ public function setTotalItems($totalItems);
public function changeTotalItems($amount);
/**
+ * Returns total quantity of items in cart.
+ *
+ * @return integer
+ */
+ public function getTotalQuantity();
+
+ /**
+ * Sets total quantity of items in cart.
+ *
+ * @param integer $totalItems;
+ */
+ public function setTotalQuantity($totalQuantity);
+
+ /**
+ * Change total quantity number by given amount.
+ *
+ * @param integer $amount
+ */
+ public function changeTotalQuantity($amount);
+
+ /**
* Get total cart value.
*/
public function getTotal();
View
1 Resources/config/doctrine/Cart.orm.xml
@@ -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>
View
13 spec/Sylius/Bundle/CartBundle/Model/Cart.php
@@ -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

No commit comments for this range

Something went wrong with that request. Please try again.