laravelshoppingcart icon indicating copy to clipboard operation
laravelshoppingcart copied to clipboard

The order of the items after updating always changes

Open nurmanhabib opened this issue 5 years ago • 3 comments

The cause of the always changing order is the pull method

https://github.com/darryldecode/laravelshoppingcart/blob/7f1581d64184575384d1a09924e45142e3bc58ed/src/Darryldecode/Cart/Cart.php#L242

If updating the first item will move to the lowest order, this causes the Frontend to always move.

nurmanhabib avatar Sep 20 '20 07:09 nurmanhabib

Same for me, how to fix?

larswoltersdev avatar Sep 27 '20 11:09 larswoltersdev

This is the normal behavior when updating. If you would like to maintain the order somehow check this for solution -> https://github.com/darryldecode/laravelshoppingcart/issues/47

silios avatar Sep 28 '20 21:09 silios

This is the normal behavior when updating. If you would like to maintain the order somehow check this for solution -> #47

Because I want the order based on the time of adding to the shopping cart.

===

Same for me, how to fix?

temporary and possibly permanent solutions: D

Cart::add([
            'id' => $product->id,
            'name' => $product->name,
            'price' => $product->getActualPrice()->getValue(),
            'quantity' => $request->input('quantity', 1),
            'attributes' => [
                'added_at' => Carbon::now(),
            ],
            'associatedModel' => $product,
        ]);

and then

$contents = Cart::getContent()->sortBy(function ($cart) {
            return $cart->attributes->get('added_at');
        });

nurmanhabib avatar Sep 29 '20 00:09 nurmanhabib