amsgames / laravel-shop

Laravel shop package
MIT License
482 stars 166 forks source link

BadMethodCallException #18

Closed devfelipereis closed 8 years ago

devfelipereis commented 8 years ago

Hello!

When I try to add something to my cart, I got a BadMethodCallException:

cart

# Cart Test
$cart = Cart::findByUser(1);
$product = Product::findBySKU('CA1524585BR');
 $cart->add($product); // this one will not work too: ['sku' => 'PROD0001', 'price' => 9.99]

dd($cart);

$cart and $product are working. When I try to add a product to the cart, I got the BadMethodCallException.

I don't know why it happens. Can you help me? Thank you!

Edit:

/**
     * Retrieves item from cart;
     *
     * @param string $sku SKU of item.
     *
     * @return mixed
     */
    private function getItem($sku)
    {
        $className  = Config::get('shop.item');
        $item       = new $className();
        return $item->where('sku', $sku)
            ->where('cart_id', $this->attributes['id'])
            ->first();
    }

I did a rewrite in the add method and it calls:

$this->getItem(is_array($item) ? $item['sku'] : $item->sku);

the getItem method is private and I cannot access it. But now it is fixed :D