Closed fadedshadowx closed 4 years ago
Instead of $items[1]->getUniqueId()
use $items[0]->getUniqueId()
. You have only one item in the cart.
just tested, but still no effects:
Undefined offset: 0
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());
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);
It is for php 7.4. For lower versions of php use
$item = Cart::content()->first(function (CartItem $item) {
return $item->name === $name;
});
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
Try
$item = Cart::content()->first(function (CartItem $item) use ($id) {
return $item->id === $id;
});
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());
To what your $id
equals?
Melihovv\ShoppingCart\CartItem {#423 ▼
-uniqueId: "027c91341fd5cf4d2579b49c4b6a90da"
+id: 1
+name: "Product 1"
+price: 120.0
+quantity: 1
+options: []
}
to this: +id: 1
$item = Cart::content()->first(function (CartItem $item) {
return $item->id === 1;
});
dd($item);
Show me output.
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"
Try to use $item->id == $id;
instead of $item->id === $id;
Cannot, because inside function is not defined (Undefined variable: id, even with " use ($id)". Strange.
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
How to remove from cart any object?
you are using
in your tests, but this code not working:
(ErrorException Undefined offset: 1)
dd($items) shows:
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?