Skip to content

Commit 024ede8

Browse files
committed
fixed removeItemCondition() not removing the condition if the condition was not added in array format
1 parent a23cf01 commit 024ede8

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/Darryldecode/Cart/Cart.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,38 @@ public function removeItemCondition($itemId, $conditionName)
382382

383383
$tempConditionsHolder = $item['conditions'];
384384

385-
foreach($tempConditionsHolder as $k => $condition)
385+
// if the item's conditions is in array format
386+
// we will iterate through all of it and check if the name matches
387+
// to the given name the user wants to remove, if so, remove it
388+
if( is_array($tempConditionsHolder) )
386389
{
387-
if( $condition->getName() == $conditionName )
390+
foreach($tempConditionsHolder as $k => $condition)
388391
{
389-
unset($tempConditionsHolder[$k]);
392+
if( $condition->getName() == $conditionName )
393+
{
394+
unset($tempConditionsHolder[$k]);
395+
}
390396
}
397+
398+
$item['conditions'] = $tempConditionsHolder;
391399
}
392400

393-
$item['conditions'] = $tempConditionsHolder;
401+
// if the item condition is not an array, we will check if it is
402+
// an instance of a Condition, if so, we will check if the name matches
403+
// on the given condition name the user wants to remove, if so,
404+
// lets just make $item['conditions'] an empty array as there's just 1 condition on it anyway
405+
else
406+
{
407+
$conditionInstance = "Darryldecode\\Cart\\CartCondition";
408+
409+
if ($item['conditions'] instanceof $conditionInstance)
410+
{
411+
if ($tempConditionsHolder->getName() == $conditionName)
412+
{
413+
$item['conditions'] = array();
414+
}
415+
}
416+
}
394417
}
395418

396419
$this->update($itemId, array(
@@ -591,6 +614,17 @@ protected function saveConditions($conditions)
591614
*/
592615
protected function itemHasConditions($item)
593616
{
594-
return count($item['conditions']) > 0;
617+
if( ! isset($item['conditions']) ) return false;
618+
619+
if( is_array($item['conditions']) )
620+
{
621+
return count($item['conditions']) > 0;
622+
}
623+
624+
$conditionInstance = "Darryldecode\\Cart\\CartCondition";
625+
626+
if( $item['conditions'] instanceof $conditionInstance ) return true;
627+
628+
return false;
595629
}
596630
}

tests/CartConditionsTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,38 @@ public function test_remove_item_condition_by_condition_name()
437437
$this->assertCount(1,$this->cart->get(456)['conditions'], 'Item should have one condition left');
438438
}
439439

440+
public function test_remove_item_condition_by_condition_name_scenario_two()
441+
{
442+
// NOTE: in this scenario, we will add the conditions not in array format
443+
444+
$itemCondition = new CartCondition(array(
445+
'name' => 'SALE 5%',
446+
'type' => 'sale',
447+
'target' => 'item',
448+
'value' => '-5%',
449+
));
450+
451+
$item = array(
452+
'id' => 456,
453+
'name' => 'Sample Item 1',
454+
'price' => 100,
455+
'quantity' => 1,
456+
'attributes' => array(),
457+
'conditions' => $itemCondition // <--not in array format
458+
);
459+
460+
$this->cart->add($item);
461+
462+
// let's very first the item has 2 conditions in it
463+
$this->assertNotEmpty($this->cart->get(456)['conditions'], 'Item should have one condition in it.');
464+
465+
// now let's remove a condition on that item using the condition name
466+
$this->cart->removeItemCondition(456, 'SALE 5%');
467+
468+
// now we should have only 1 condition left on that item
469+
$this->assertEmpty($this->cart->get(456)['conditions'], 'Item should have no condition now');
470+
}
471+
440472
public function test_clear_cart_conditions()
441473
{
442474
// NOTE:

0 commit comments

Comments
 (0)