LaravelShoppingcart icon indicating copy to clipboard operation
LaravelShoppingcart copied to clipboard

Problem to update item qty in instance

Open blenderdeluxe opened this issue 7 years ago • 4 comments

Hi everyone I have a problem with update cart instance I have two instance

Cart::instance('courses')

Cart::instance('reservations')

All it is fine but when I try to update a item in instance reservations this return exception The cart does not contain rowId 9e6f03ab1c6bb1227b92db5a945c817d.

But I go to Cart.php in vendor folder nand in line 181 in the function get() I modify for know what happend here so I add a dd() for show actual rowId that I send in Ajax and know if this use the correct instance (courses and NOT reservations)

public function get($rowId)
    {
        $content = $this->getContent();
        dd($rowId.$content);

        if ( ! $content->has($rowId))
            throw new InvalidRowIDException("The cart does not contain rowId {$rowId}.");

        return $content->get($rowId);
    }

And in the inspector this return (I used ajax for update item qty) the instance is correct (reservations) and the rowId the I get from Ajax exist in the content of the instance

"bd8524f8195d365217ffd17b14ec7415{"6b9b3641ee84da514e000d305e606843":{"rowId":"6b9b3641ee84da514e000d305e606843","id":1,"name":"Open Water Diver","qty":0,"price":260000,"options":{"description":"Aprende a bucear con equipo scuba y obt\u00e9n una licencia, para bucear sin problemas por el mundo entero.","icon":"icon-open-water-diver"},"tax":0,"subtotal":0},"5e2690d03b39c8122d6febbd4b293ab7":{"rowId":"5e2690d03b39c8122d6febbd4b293ab7","id":2,"name":"Advanced Open Water Diver","qty":0,"price":240000,"options":{"description":"Este curso te ayuda a incrementar la con\ufb01anza, tambi\u00e9n podr\u00e1s desarrollar tus destrezas y t\u00e9cnicas de buceo para que puedas sentirte m\u00e1s c\u00f3modo y h\u00e1bil en el agua.","icon":"icon-advanced-open-water-diver"},"tax":0,"subtotal":0},"bd8524f8195d365217ffd17b14ec7415":{"rowId":"bd8524f8195d365217ffd17b14ec7415","id":4,"name":"Rescue Diver","qty":0,"price":250000,"options":{"description":"Aprender\u00e1s t\u00e9cnicas de rescate en el agua. Partiendo de lo que ya haz aprendido al bucear, este curso ampl\u00eda los conocimientos que ya tienes acerca de como prevenir problemas y como gestionarlos si suceden.","icon":"icon-rescue-diver"},"tax":0,"subtotal":0}}"

If you see

bd8524f8195d365217ffd17b14ec7415

exist here

{"rowId":"bd8524f8195d365217ffd17b14ec7415","id":4,"name":"Rescue Diver","qty":0,"price":250000,"options":{"description":"Aprender\u00e1s t\u00e9cnicas de rescate en el agua. Partiendo de lo que ya haz aprendido al bucear, este curso ampl\u00eda los conocimientos que ya tienes acerca de como prevenir problemas y como gestionarlos si suceden.","icon":"icon-rescue-diver"},"tax":0,"subtotal":0}}"

and if I use in my controller

$item = Cart::instance('courses')->get($rowId);
dd($item);

if($item)
{
      //Update qty of course
      Cart::instance('courses')->update($item->rowId, ['qty' => $quantity]);
 }

of course without dd($rowId.$content); in get function I get perfectly the item

CartItem {#214
  +rowId: "bd8524f8195d365217ffd17b14ec7415"
  +id: 4
  +qty: 0
  +name: "Rescue Diver"
  +price: 250000.0
  +options: CartItemOptions {#215
    #items: array:2 [
      "description" => "Aprenderás técnicas de rescate en el agua. Partiendo de lo que ya haz aprendido al bucear, este curso amplía los conocimientos que ya tienes acerca de como prevenir problemas y como gestionarlos si suceden."
      "icon" => "icon-rescue-diver"
    ]
  }
  -associatedModel: null
  -taxRate: 0
}

If I delete dd($item); here this return Exception InvalidRowIDException in Cart.php line 187: The cart does not contain rowId bd8524f8195d365217ffd17b14ec7415.

Aps and if you see that the quantity of items is 0, this is correct because I modified before because I have defaults items and I dont need add items dynamic

vendor/gloudemans/shoppingcart/src/Cart.php

I commented the line 145

$this->remove($cartItem->rowId);

and after

vendor/gloudemans/shoppingcart/src/CartItem.php

I modified the line 181

if(empty($qty) || ! is_numeric($qty))

by if(! is_numeric($qty))

This modifications dont have problems and works fine :D, is only for that you understand Why I use qty 0 in the items.

Hope can help me with this update item

blenderdeluxe avatar Jul 28 '16 00:07 blenderdeluxe

Could you please give me a little smaller example of how I can reproduce this problem? Given a new Laravel installation.

Crinsane avatar Aug 01 '16 06:08 Crinsane

Hi,

If you want to update the cart for the instance 'reservations'

you have to use the below code

$item = Cart::instance('reservations')->get($rowId);

Instead of

$item = Cart::instance('courses')->get($rowId);

Thanks.

sonikunal avatar Mar 11 '17 14:03 sonikunal

good afternoon, I ask for your help, perform the implementation of laravelshoppingcart, the products are added but they are not shown throws the following error, I do not see the fault, thanks Trying to get property 'slug' of non-object (View: C: \ xampp \ htdocs \ ecommerce \ ecommerce \ resources \ views \ cart.blade.php), I go to cart.blade.php view

                @foreach (Cart :: content () as $ item)                 
                    
                         item                         
                            
> {{$ item-> model-> name }}
                            
{{$ item-> model-> details}}
                        
                    
                    
                        
                            
                                {{csrf_field ()}}                                 {{method_field ('DELETE')}}

                                                             

                            

                                {{csrf_field ()}}

                                                             

                        
                        
                            <select class = "quantity" data-id = "{{$ item-> rowId}}" data-productQuantity = "{{$ item-> model-> quantity}}">                                 @for ($ i = 1; $ i <5 + 1; $ i ++)                                     <option {{$ item-> qty == $ i? 'selected': ''}}> {{$ i}}                                 @endfor                                                      
                        
{{presentPrice ($ item-> subtotal)}}
                    
                
<! - end cart-table-row ->                 @endforeach

            

<! - end cart-table ->

leydijo avatar Sep 02 '19 16:09 leydijo

@leydijo If you encounter a 'Trying to get property XY of non-object in ...' It is usually due that the object that should hold the propery has not been instantiated (i.e. is null or any other type than object). Do you have a reference to ->slug in your cart.blade.php? But remember, without knowing more about your integration i can only make guesses.

Also you could add three ` before and after your code in order to properly format it.

bumbummen99 avatar Sep 02 '19 17:09 bumbummen99