This commit is contained in:
Patrick Henninger
2018-12-28 03:37:34 +01:00
parent 4cfd3e99b0
commit 4af50346eb

View File

@@ -71,7 +71,7 @@ Maybe you prefer to add the item using an array? As long as the array contains t
Cart::add(['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => ['size' => 'large']]);
```
New in version 2 of the package is the possibility to work with the `Buyable` interface. The way this works is that you have a model implement the `Buyable` interface, which will make you implement a few methods so the package knows how to get the id, name and price from your model.
New in version 2 of the package is the possibility to work with the [Buyable](#buyable) interface. The way this works is that you have a model implement the [Buyable](#buyable) interface, which will make you implement a few methods so the package knows how to get the id, name and price from your model.
This way you can just pass the `add()` method a model and the quantity and it will automatically add it to the cart.
**As an added bonus it will automatically associate the model with the CartItem**
@@ -320,6 +320,44 @@ Cart::setDiscount($rowId, 21);
$cart->setDiscount($rowId, 21);
```
### Buyable
For the convenience of faster adding items to cart and their automatic association, your model can implement `Buyable` interface. To do so, it must implement such functions:
```php
public function getBuyableIdentifier(){
return $this->id;
}
public function getBuyableDescription(){
return $this->name;
}
public function getBuyablePrice(){
return $this->price;
}
```
Example:
```php
<?php
namespace App\Models;
use Gloudemans\Shoppingcart\Contracts\Buyable;
use Illuminate\Database\Eloquent\Model;
class Product exends Model implements Buyable {
public function getBuyableIdentifier($options = null) {
return $this->id;
}
public function getBuyableDescription($options = null) {
return $this->name;
}
public function getBuyablePrice($options = null) {
return $this->price;
}
}
```
## Collections
On multiple instances the Cart will return to you a Collection. This is just a simple Laravel Collection, so all methods you can call on a Laravel Collection are also available on the result.