vanilophp / cart

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

Private methods in Vanilo\Cart\Models\Cart #9

Closed j-dohnalek closed 5 years ago

j-dohnalek commented 5 years ago

Vanilo Cart 0.5.1

I have a special type of product in database for which I need to insert the product into the cart from the product.show page as new item not to update the quantity every time I insert it like it is standard for inserting the same product. Therefore, I have extended the Cart model to modify the addItem(Buyable $product, $qty = 1, $params = []). The only issue that I had implementing it that there are two methods that are private in the Vanilo\Cart\Models\Cart

getDefaultCartItemAttributes(Buyable $product, $qty)
getExtraProductMergeAttributes(Buyable $product)

I just had to copy and paste them around from the parent class. Do you have a particular reason these methods are private please? Is it possible that the methods can be protected instead? Thank you for help.

yusufkandemir commented 5 years ago

I don't know if I got you correct but as I understand, you have a special item that needs to have exactly 1 as quantity when it is in cart. As addItem() returns you the CartItem that has been created or modified, you can just do $item->quantity = 1; to set it's quantity as 1 after calling addItem(), and if you want to remove it you can do that by removeItem($item). If I got you wrong, please provide some code samples so we can understand better what you are trying to achieve.

j-dohnalek commented 5 years ago

Thank you for the question. I do not know if I have explained it well. So I have a SKU in inventory. The SKU represents a item that is customisable (let say volume in this example). I do not know which volume the customer will pick. Let's say I have product SKU1 a customer comes into the shop selects he wants SKU1 with volume 1, subsequently he selected SKU1 with volume 2. The target of the logic is to add SKU1 with volume 1 to the cartItems as record 1 and add SKU1 with volume 2 as record 2. Keeping the logic of the code as it is now would add SKU1 volume 1 and SKU1 volume 2 as same record and just increment the quantity to 2 because they are the same product. I need to be able to adjust quantity of SKU1 volume 1 separately of the SKU1 volume 2 quantity in the cart. It is very strange requirement but it is what we need. Lastly, please consider also width of the volume range can be too great to just put it as a property of the item. I am open to suggestions, as I am not sure that this is the best solution, but it is the best solution I have come up with now.

fulopattila122 commented 5 years ago

I would say in general it's the best to use different product entries for different products. SKU is "Stock Keeping Unit" so in case you have different volumes of the same product use different SKUs for them. A common practice is to use SKU suffxing eg. "skuxyz-1kg" skuxyz-5kg" or something like that.

fulopattila122 commented 5 years ago

The methods are now protected on the master/1.0-dev branch. Use composer version 1.0@dev to get it. Sorry, it should've been fixed much, much faster. My bad... :(

j-dohnalek commented 5 years ago

No worries, thank you