Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to search/get a cart values/item with its id not rowId #408

Closed
wasid opened this issue Oct 31, 2017 · 8 comments
Closed

How to search/get a cart values/item with its id not rowId #408

wasid opened this issue Oct 31, 2017 · 8 comments

Comments

@wasid
Copy link

wasid commented Oct 31, 2017

public function addCart($id)
{
$products = MarketProduct::find($id);

    $cartproduct = Cart::search(array('id' => $id, 'options' => array('available_quantity' => ?)));
  
     return  $cart_available_quantity = $cartproduct->options->available_quantity; 

    $available_quantity = $products->product->available_quantity;

    then compare $cart_available_quantity and $available_quantity before adding more qty

    $result = Cart::add([
        'id' => $id,
        'name' => $products->title,
        'qty' => 1,
        'price' => $products->mrp(),
        'options' => [
            'image' => $products->thumbnail(),
            'available_quantity' => $available_quantity
            ]
        ]);
        
    return redirect()->back();
    

}
@wasid wasid changed the title How to search/get a cart values item with its id not rowId How to search/get a cart values/item with its id not rowId Oct 31, 2017
@Crinsane
Copy link
Owner

Use the search method

@wasid
Copy link
Author

wasid commented Nov 1, 2017

Sir, your answer is not clear to me. I already used the search method. Have close look on the code
$cartproduct = Cart::search(array('id' => $id, 'options' => array('available_quantity' => ?)));

basically I need this:
$products = App\MarketProduct::find($id);
$cartproducts = Cart::search('id' == $id);
then compare quantity from both array...
I hope I can make you understand.

Thanks

@wasid
Copy link
Author

wasid commented Nov 2, 2017

Cart::content()->where('id', $id); this code did the tricks for me...

@A7medHub
Copy link

@wasid you saved my day man very thanks

@frikishaan
Copy link

I also can't understand how the search function works here.
But the @wasid 's solution also works for me. Thanks man.👏

@rahim3070
Copy link

rahim3070 commented Aug 30, 2020

Cart::content()->where('id', $id); this code did the tricks for me...

Output :

Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[52301c99fa63b03bd5c487d5643a4b70] => Gloudemans\Shoppingcart\CartItem Object
(
[rowId] => 52301c99fa63b03bd5c487d5643a4b70
[id] => 15
[qty] => 14
[name] => a
[price] => 17
[options] => Gloudemans\Shoppingcart\CartItemOptions Object
(
[items:protected] => Array
(
[image] => images/product_image/150.jpg
)
)
[associatedModel:Gloudemans\Shoppingcart\CartItem:private] =>
[taxRate:Gloudemans\Shoppingcart\CartItem:private] => 0
)
)
)

@wasid But how to get the qty ?

@bumbummen99
Copy link

Sorry if i misunderstand the topic but using the search method is just Collection::filter, you should be able to iterate the returning collection or use first() and then be able to access the quantity as it is a public property of the CartItem class like this: $item->qty

So out of my head something like that should work:

$cart->search(function($value, $key){
  return $value->id === $id;
})->first()->qty;

@rahim3070
Copy link

rahim3070 commented Aug 30, 2020

@bumbummen99, Its very simple ..................

$asd = Cart::content()->where('id', $request->product_id);
$cart_indqty = 0;

foreach ($asd as $row) {
$cart_indqty = $row->qty;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants