Skip to content

Commit

Permalink
fixed removeItemCondition() not removing the condition if the conditi…
Browse files Browse the repository at this point in the history
…on was not added in array format
  • Loading branch information
darryldecode committed May 21, 2015
1 parent a23cf01 commit 024ede8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
44 changes: 39 additions & 5 deletions src/Darryldecode/Cart/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,15 +382,38 @@ public function removeItemCondition($itemId, $conditionName)

$tempConditionsHolder = $item['conditions'];

foreach($tempConditionsHolder as $k => $condition)
// if the item's conditions is in array format
// we will iterate through all of it and check if the name matches
// to the given name the user wants to remove, if so, remove it
if( is_array($tempConditionsHolder) )
{
if( $condition->getName() == $conditionName )
foreach($tempConditionsHolder as $k => $condition)
{
unset($tempConditionsHolder[$k]);
if( $condition->getName() == $conditionName )
{
unset($tempConditionsHolder[$k]);
}
}

$item['conditions'] = $tempConditionsHolder;
}

$item['conditions'] = $tempConditionsHolder;
// if the item condition is not an array, we will check if it is
// an instance of a Condition, if so, we will check if the name matches
// on the given condition name the user wants to remove, if so,
// lets just make $item['conditions'] an empty array as there's just 1 condition on it anyway
else
{
$conditionInstance = "Darryldecode\\Cart\\CartCondition";

if ($item['conditions'] instanceof $conditionInstance)
{
if ($tempConditionsHolder->getName() == $conditionName)
{
$item['conditions'] = array();
}
}
}
}

$this->update($itemId, array(
Expand Down Expand Up @@ -591,6 +614,17 @@ protected function saveConditions($conditions)
*/
protected function itemHasConditions($item)
{
return count($item['conditions']) > 0;
if( ! isset($item['conditions']) ) return false;

if( is_array($item['conditions']) )
{
return count($item['conditions']) > 0;
}

$conditionInstance = "Darryldecode\\Cart\\CartCondition";

if( $item['conditions'] instanceof $conditionInstance ) return true;

return false;
}
}
32 changes: 32 additions & 0 deletions tests/CartConditionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,38 @@ public function test_remove_item_condition_by_condition_name()
$this->assertCount(1,$this->cart->get(456)['conditions'], 'Item should have one condition left');
}

public function test_remove_item_condition_by_condition_name_scenario_two()
{
// NOTE: in this scenario, we will add the conditions not in array format

$itemCondition = new CartCondition(array(
'name' => 'SALE 5%',
'type' => 'sale',
'target' => 'item',
'value' => '-5%',
));

$item = array(
'id' => 456,
'name' => 'Sample Item 1',
'price' => 100,
'quantity' => 1,
'attributes' => array(),
'conditions' => $itemCondition // <--not in array format
);

$this->cart->add($item);

// let's very first the item has 2 conditions in it
$this->assertNotEmpty($this->cart->get(456)['conditions'], 'Item should have one condition in it.');

// now let's remove a condition on that item using the condition name
$this->cart->removeItemCondition(456, 'SALE 5%');

// now we should have only 1 condition left on that item
$this->assertEmpty($this->cart->get(456)['conditions'], 'Item should have no condition now');
}

public function test_clear_cart_conditions()
{
// NOTE:
Expand Down

0 comments on commit 024ede8

Please sign in to comment.