vanilophp / cart

Cart Module for Vanilo (or any Laravel app)
https://vanilo.io
MIT License
50 stars 17 forks source link

Is it possible to add the same product multiple times to cart? #12

Closed victorighalo closed 4 years ago

victorighalo commented 4 years ago

Is it possible to add an item more than once to the cart and it won't increase the quantity but add a separate item to cart.

victorighalo commented 4 years ago
public function addItem(Buyable $product, $qty = 1, $params = []): \Vanilo\Cart\Contracts\CartItem
{
    $item = $this->items()->ofCart($this)->byProduct($product)->first();

    if ($item) {
        $item->quantity += $qty;
        $item->save();
    } else {
        $item = $this->items()->create(
            array_merge(
                $this->getDefaultCartItemAttributes($product, $qty),
                $this->getExtraProductMergeAttributes($product),
                $params['attributes'] ?? []
            )
        );
    }

    $this->load('items');

    return $item;
}

This seems to be the part that increments the cart item quantity if it exists. Can we have an alternative to add a new item to cart instead of increasing quantity?

fulopattila122 commented 4 years ago

I think that's possible. So that I get your context, can you please describe the use case in a little bit more details?

victorighalo commented 4 years ago

I think that's possible. So that I get your context, can you please describe the use case in a little bit more details?

A shopper wants to add a product with variant Color:Red and Size:XL, the same shopper also wants to add another of the same product with variant Color:Blue and Size:XXL. The shopper wants to see these as two distinct items in cart and as distinct items as an order

fulopattila122 commented 4 years ago

Ah cool, I get it. It's basically the product variants problem, see also vanilophp/framework#62 for more details.

How do you distinguish between Color and Size variants? Are you using the properties package or something else?

victorighalo commented 4 years ago

I made my own variants module. Variants have foreign key relationships with the product model.

However, even without variants, is this possible currently or do can I extend the Cart model like I have done with the Product model.

fulopattila122 commented 4 years ago

Absolutely possible. You only need to replace the Cart model (not the facade!) and overwrite the addItem method with your version.

Steps:

  1. Create your model, eg. App\Cart
  2. Make App\Cart to extend Vanilo\Cart\Models\Cart
  3. Implement your addItem method in App\Cart
  4. Register your custom model so that it will be used instead of the original one:
    
    // app/Providers/AppServiceProvider.php
    namespace App\Providers;

use Illuminate\Support\ServiceProvider; use Vanilo\Cart\Contracts\Cart as CartContract;

class AppServiceProvider extends ServiceProvider { public function boot() { $this->app->concord->registerModel( CartContract::class, \App\Cart::class ); } }



This way, your custom method will be used, even if you're invoking the cart facade's `Cart::addItem` method.

More details: https://vanilo.io/how-to/use-custom-models-in-your-application

Let me know if it worked :bowing_man: 
fulopattila122 commented 4 years ago

Could you manage to achieve the functionality?

chellmann commented 4 years ago

Hey Attila, i had the same Problem and could solve the problem. Excellent help. Thank you!

As of Laravel 7.x it needs to be

class Cart extends \Vanilo\Cart\Models\Cart { public function addItem(Buyable $product, $qty = 1, $params = []): \Vanilo\Cart\Contracts\CartItem

(with \ before Vanilo)