Merge pull request #36 from cwprogger/fix-products-sequence

Fix products sequence after changing cart item options
This commit is contained in:
Patrick
2019-11-02 15:50:44 +01:00
committed by GitHub
2 changed files with 24 additions and 1 deletions

View File

@@ -180,6 +180,8 @@ class Cart
$content = $this->getContent();
if ($rowId !== $cartItem->rowId) {
$itemOldIndex = $content->keys()->search($rowId);
$content->pull($rowId);
if ($content->has($cartItem->rowId)) {
@@ -193,7 +195,13 @@ class Cart
return;
} else {
$content->put($cartItem->rowId, $cartItem);
if (isset($itemOldIndex)) {
$content = $content->slice(0, $itemOldIndex)
->merge([$cartItem->rowId => $cartItem])
->merge($content->slice($itemOldIndex));
} else {
$content->put($cartItem->rowId, $cartItem);
}
}
$this->events->dispatch('cart.updated', $cartItem);

View File

@@ -399,6 +399,21 @@ class CartTest extends TestCase
$this->assertRowsInCart(1, $cart);
}
/** @test */
public function it_will_keep_items_sequence_if_the_options_changed()
{
$cart = $this->getCart();
$cart->add(new BuyableProduct(), 1, ['color' => 'red']);
$cart->add(new BuyableProduct(), 1, ['color' => 'green']);
$cart->add(new BuyableProduct(), 1, ['color' => 'blue']);
$cart->update($cart->content()->values()[1]->rowId, ['options' => ['color' => 'yellow']]);
$this->assertRowsInCart(3, $cart);
$this->assertEquals('yellow', $cart->content()->values()[1]->options->color);
}
/** @test */
public function it_can_remove_an_item_from_the_cart()
{