hassansin / dbcart

Shopping Cart library for Laravel 5 that uses Database instead of Sessions
MIT License
25 stars 10 forks source link

MoveToCart creates double entries for a product #15

Closed nfunwigabga closed 5 years ago

nfunwigabga commented 5 years ago

Nice package. May be nice to change the moveToCart(Cart $cart) method to check if any of the products in the source cart already exists in the receiving cart. At the moment, it copies the products from cart A to cart B, even if the products are already in cartB. Maybe something like this:

public function moveToCart(Cart $cart)
{
    $current_items = $cart->items()->pluck('product_id');
    $items_to_update = $this->items()->whereNotIn('product_id', $current_items);
    $items_to_update->update([
        'cart_id' => $cart->id 
    ]);
    $cart->item_count = $cart->items()->sum('quantity');
     ....
}
hassansin commented 5 years ago

Thanks for using the package. Please try again from master branch.