Skip to content

Allow multiple taxrates #9

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

Merged
merged 2 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ As an optional fifth parameter you can pass it options, so you can add multiple
Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']);
```

You can optional pass different taxrates to the items.
```php
Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large'], 20);
```

**The `add()` method will return an CartItem instance of the item you just added to the cart.**

Maybe you prefer to add the item using an array? As long as the array contains the required keys, you can pass it to the method. The options key is optional.
Expand Down
14 changes: 10 additions & 4 deletions src/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ public function currentInstance()
* @param int|float $qty
* @param float $price
* @param array $options
* @param float $taxrate
* @return \Gloudemans\Shoppingcart\CartItem
*/
public function add($id, $name = null, $qty = null, $price = null, array $options = [])
public function add($id, $name = null, $qty = null, $price = null, array $options = [], $taxrate = null)
{
if ($this->isMulti($id)) {
return array_map(function ($item) {
Expand All @@ -97,7 +98,7 @@ public function add($id, $name = null, $qty = null, $price = null, array $option
if ($id instanceof CartItem) {
$cartItem = $id;
} else {
$cartItem = $this->createCartItem($id, $name, $qty, $price, $options);
$cartItem = $this->createCartItem($id, $name, $qty, $price, $options, $taxrate);
}

$content = $this->getContent();
Expand Down Expand Up @@ -469,9 +470,10 @@ protected function getContent()
* @param int|float $qty
* @param float $price
* @param array $options
* @param float $taxrate
* @return \Gloudemans\Shoppingcart\CartItem
*/
private function createCartItem($id, $name, $qty, $price, array $options)
private function createCartItem($id, $name, $qty, $price, array $options, $taxrate)
{
if ($id instanceof Buyable) {
$cartItem = CartItem::fromBuyable($id, $qty ?: []);
Expand All @@ -485,7 +487,11 @@ private function createCartItem($id, $name, $qty, $price, array $options)
$cartItem->setQuantity($qty);
}

$cartItem->setTaxRate(config('cart.tax'));
if($taxrate) {
$cartItem->setTaxRate($taxrate);
} else {
$cartItem->setTaxRate(config('cart.tax'));
}

return $cartItem;
}
Expand Down