melihovv / laravel-shopping-cart

Laravel shopping cart package
MIT License
73 stars 29 forks source link

Remove from cart #15

Closed fadedshadowx closed 4 years ago

fadedshadowx commented 4 years ago

How to remove from cart any object?

you are using

$items = $this->addItemsToCart();
\Cart::remove($items[2]->getUniqueId());

in your tests, but this code not working:

Cart::restore(session()->getId());
Cart::restore('default');
$items = Cart::content();
dd(Cart::remove($items[1]->getUniqueId()));

(ErrorException Undefined offset: 1)

dd($items) shows:

Illuminate\Support\Collection {#422 ▼
  #items: array:1 [▼
    "027c91341fd5cf4d2579b49c4b6a90da" => Melihovv\ShoppingCart\CartItem {#423 ▼
      -uniqueId: "027c91341fd5cf4d2579b49c4b6a90da"
      +id: 1
      +name: "Product 1"
      +price: 120.0
      +quantity: 1
      +options: []
    }
  ]
}

when try use 027c91341fd5cf4d2579b49c4b6a90da statically dd(Cart::remove('027c91341fd5cf4d2579b49c4b6a90da'));

result is: true but cart has still product..

edit: nvm, just forget about store after remove.. but how get this unique id on existing cart?

melihovv commented 4 years ago

Instead of $items[1]->getUniqueId() use $items[0]->getUniqueId(). You have only one item in the cart.

fadedshadowx commented 4 years ago

just tested, but still no effects: Undefined offset: 0

melihovv commented 4 years ago

Ok, got it. Items in the card are indexed by their unique ids. To get unique id of particular item you need somehow filter items by item name or other attribute.

$item = Cart::content()->first(fn (CartItem $item) => $item->name === $name);
Cart::remove($item->getUniqueId());
fadedshadowx commented 4 years ago

Is this code ok? I am not very experienced user, but something wrong:

ParseError
syntax error, unexpected '$item' (T_VARIABLE), expecting ')' 

on this line $item = Cart::content()->first(fn (CartItem $item) => $item->name === $name);

melihovv commented 4 years ago

It is for php 7.4. For lower versions of php use

$item = Cart::content()->first(function (CartItem $item) {
  return $item->name === $name;
});
fadedshadowx commented 4 years ago

wrrr i am confused. still no effects. using php 7.3 my full code now:

Cart::restore(session()->getId());
Cart::restore('default');
$item = Cart::content();

$item = Cart::content()->first(function (CartItem $item, $id) { //passed id instead of name to search
   //dd($item); 
return $item->id === $id;
});
Cart::remove($item->getUniqueId());
Cart::store(session()->getId());

when run dd which you see above, I gave only one, first element of cart, but still no effects because of error: Call to a member function getUniqueId() on null on line with Cart::remove

thank you for your help so far

melihovv commented 4 years ago

Try

$item = Cart::content()->first(function (CartItem $item) use ($id) {
  return $item->id === $id;
});
fadedshadowx commented 4 years ago

Still the same problem - Call to a member function getUniqueId() on null , dd inside this function display first element of cart, not equal with $id Full code:

Cart::restore(session()->getId());
Cart::restore('default');
$item = Cart::content();
$item = Cart::content()->first(function (CartItem $item) use ($id) {
//dd($item);
return $item->id === $id;
});
Cart::remove($item->getUniqueId());
Cart::store(session()->getId());
melihovv commented 4 years ago

To what your $id equals?

fadedshadowx commented 4 years ago
Melihovv\ShoppingCart\CartItem {#423 ▼
  -uniqueId: "027c91341fd5cf4d2579b49c4b6a90da"
  +id: 1
  +name: "Product 1"
  +price: 120.0
  +quantity: 1
  +options: []
}

to this: +id: 1

melihovv commented 4 years ago
$item = Cart::content()->first(function (CartItem $item) {
  return $item->id === 1;
});
dd($item);

Show me output.

fadedshadowx commented 4 years ago
Melihovv\ShoppingCart\CartItem {#423 ▼
  -uniqueId: "027c91341fd5cf4d2579b49c4b6a90da"
  +id: 1
  +name: "Pakiet 1"
  +price: 120.0
  +quantity: 1
  +options: []
}

When use this "1" statically, remove working too. Cannot see $id on this function. dd($id) before this code prints "1"

melihovv commented 4 years ago

Try to use $item->id == $id; instead of $item->id === $id;

fadedshadowx commented 4 years ago

Cannot, because inside function is not defined (Undefined variable: id, even with " use ($id)". Strange.

fadedshadowx commented 4 years ago

This works:

Cart::restore(session()->getId());
Cart::restore('default');
$item = Cart::content()->toArray();
$filterred = Arr::where($item, function ($value, $key) use ($itemToDelete) {
if($value['id']==$itemToDelete){
    Cart::remove($key);
}
});
Cart::store(session()->getId());

thank you for your help so far