diff --git a/README.md b/README.md index 8bb852c..22825fc 100644 --- a/README.md +++ b/README.md @@ -329,7 +329,20 @@ $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: +For the convenience of faster adding items to cart and their automatic association, your model has to implement the `Buyable` interface. You can use the `CanBeBought` trait to implement the required methods but keep in mind that these will use predefined fields on your model for the required values. +```php +price; } + public function getBuyableWeight(){ + return $this->weight; + } ``` Example: diff --git a/tests/CartTest.php b/tests/CartTest.php index 53f2812..b6b5014 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -16,6 +16,7 @@ use Gloudemans\Shoppingcart\ShoppingcartServiceProvider; use Gloudemans\Tests\Shoppingcart\Fixtures\ProductModel; use Gloudemans\Tests\Shoppingcart\Fixtures\BuyableProduct; use Gloudemans\Tests\Shoppingcart\Fixtures\Identifiable; +use Gloudemans\Tests\Shoppingcart\Fixtures\BuyableProductTrait; class CartTest extends TestCase { @@ -1096,6 +1097,28 @@ class CartTest extends TestCase $this->assertItemsInCart(2, $cart); } + /** @test */ + public function cart_can_create_items_from_models_using_the_canbebought_trait() + { + $cart = $this->getCartDiscount( 50 ); + + $cart->add(new BuyableProductTrait(1, 'First item', 10.00), 2); + + $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); + + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + + $this->assertEquals(10.00, $cartItem->price(2)); + $this->assertEquals(5.00, $cartItem->discount(2)); + $this->assertEquals(10.00, $cartItem->discountTotal(2)); + $this->assertEquals(5.00, $cartItem->priceTarget(2)); + $this->assertEquals(10.00, $cartItem->subtotal(2)); + $this->assertEquals(0.95, $cartItem->tax(2)); + $this->assertEquals(1.90, $cartItem->taxTotal(2)); + $this->assertEquals(5.95, $cartItem->priceTax(2)); + $this->assertEquals(11.90, $cartItem->total(2)); + } + /** * Get an instance of the cart. * diff --git a/tests/Fixtures/BuyableProductTrait.php b/tests/Fixtures/BuyableProductTrait.php new file mode 100644 index 0000000..9aadb6d --- /dev/null +++ b/tests/Fixtures/BuyableProductTrait.php @@ -0,0 +1,45 @@ +id = $id; + $this->name = $name; + $this->price = $price; + $this->weight = $weight; + } +} \ No newline at end of file