public function test_sub_total_when_item_quantity_is_updated() { $items = array(array('id' => 456, 'name' => 'Sample Item 1', 'price' => 67.98999999999999, 'quantity' => 3, 'attributes' => array()), array('id' => 568, 'name' => 'Sample Item 2', 'price' => 69.25, 'quantity' => 1, 'attributes' => array())); $this->cart->add($items); $this->assertEquals(273.22, $this->cart->getSubTotal(), 'Cart should have sub total of 273.22'); // when cart's item quantity is updated, the subtotal should be updated as well $this->cart->update(456, array('quantity' => 2)); $this->assertEquals(205.23, $this->cart->getSubTotal(), 'Cart should have sub total of 205.23'); }
public function test_sub_total_when_item_quantity_is_updated_by_reduced() { $items = array(array('id' => 456, 'name' => 'Sample Item 1', 'price' => 67.98999999999999, 'quantity' => 3, 'attributes' => array()), array('id' => 568, 'name' => 'Sample Item 2', 'price' => 69.25, 'quantity' => 1, 'attributes' => array())); $this->cart->add($items); $this->assertEquals(273.22, $this->cart->getSubTotal(), 'Cart should have sub total of 273.22'); // when cart's item quantity is updated, the subtotal should be updated as well $this->cart->update(456, array('quantity' => -1)); // get the item to be evaluated $item = $this->cart->get(456); $this->assertEquals(2, $item['quantity'], 'Item quantity of with item ID of 456 should now be reduced to 2'); $this->assertEquals(205.23, $this->cart->getSubTotal(), 'Cart should have sub total of 205.23'); }
public function test_add_item_with_multiple_item_conditions_with_one_condition_wrong_target() { // NOTE: // $condition1 and $condition4 should not be included in calculation // as the target is not subtotal, remember that when adding // conditions in per-item bases, the condition's target should // have a value of subtotal $itemCondition1 = new CartCondition(array('name' => 'SALE 5%', 'type' => 'sale', 'target' => 'total', 'value' => '-5%')); // --> this should not be included in calculation $itemCondition2 = new CartCondition(array('name' => 'Item Gift Pack 25.00', 'type' => 'promo', 'target' => 'subtotal', 'value' => '-25')); $itemCondition3 = new CartCondition(array('name' => 'MISC', 'type' => 'misc', 'target' => 'subtotal', 'value' => '+10')); $itemCondition4 = new CartCondition(array('name' => 'MISC 2', 'type' => 'misc2', 'target' => 'total', 'value' => '+10%')); // --> this should not be included in calculation $item = array('id' => 456, 'name' => 'Sample Item 1', 'price' => 100, 'quantity' => 1, 'attributes' => array(), 'conditions' => [$itemCondition1, $itemCondition2, $itemCondition3, $itemCondition4]); $this->cart->add($item); $this->assertEquals(85.0, $this->cart->getSubTotal(), 'Cart subtotal with 1 item should be 70'); }
public function test_get_calculated_value_of_a_condition() { $cartCondition1 = new CartCondition(array('name' => 'SALE 5%', 'type' => 'sale', 'target' => 'subtotal', 'value' => '-5%')); $cartCondition2 = new CartCondition(array('name' => 'Item Gift Pack 25.00', 'type' => 'promo', 'target' => 'subtotal', 'value' => '-25')); $item = array('id' => 456, 'name' => 'Sample Item 1', 'price' => 100, 'quantity' => 1, 'attributes' => array()); $this->cart->add($item); $this->cart->condition([$cartCondition1, $cartCondition2]); $subTotal = $this->cart->getSubTotal(); $this->assertEquals(100, $subTotal, 'Subtotal should be 100'); // way 1 // now we will get the calculated value of the condition 1 $cond1 = $this->cart->getCondition('SALE 5%'); $this->assertEquals(5, $cond1->getCalculatedValue($subTotal), 'The calculated value must be 5'); // way 2 // get all cart conditions and get their calculated values $conditions = $this->cart->getConditions(); $this->assertEquals(5, $conditions['SALE 5%']->getCalculatedValue($subTotal), 'First condition calculated value must be 5'); $this->assertEquals(25, $conditions['Item Gift Pack 25.00']->getCalculatedValue($subTotal), 'First condition calculated value must be 5'); }
/** * get cart sub total * * @return float * @static */ public static function getSubTotal() { return \Darryldecode\Cart\Cart::getSubTotal(); }