mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-11 18:54:33 +00:00
107
src/Cart.php
107
src/Cart.php
@@ -110,8 +110,7 @@ class Cart
|
||||
*/
|
||||
public function add($id, $name = null, $qty = null, $price = null, array $options = [])
|
||||
{
|
||||
if ($this->isMulti($id))
|
||||
{
|
||||
if ($this->isMulti($id)) {
|
||||
return array_map(function ($item) {
|
||||
return $this->add($item);
|
||||
}, $id);
|
||||
@@ -140,8 +139,9 @@ class Cart
|
||||
|
||||
$content = $this->getContent();
|
||||
|
||||
if ($content->has($item->rowId))
|
||||
if ($content->has($item->rowId)) {
|
||||
$item->qty += $content->get($item->rowId)->qty;
|
||||
}
|
||||
|
||||
$content->put($item->rowId, $item);
|
||||
|
||||
@@ -163,33 +163,31 @@ class Cart
|
||||
{
|
||||
$cartItem = $this->get($rowId);
|
||||
|
||||
if ($qty instanceof Buyable)
|
||||
if ($qty instanceof Buyable) {
|
||||
$cartItem->updateFromBuyable($qty);
|
||||
elseif (is_array($qty))
|
||||
} elseif (is_array($qty)) {
|
||||
$cartItem->updateFromArray($qty);
|
||||
else
|
||||
} else {
|
||||
$cartItem->qty = $qty;
|
||||
}
|
||||
|
||||
$content = $this->getContent();
|
||||
|
||||
if ($rowId !== $cartItem->rowId)
|
||||
{
|
||||
if ($rowId !== $cartItem->rowId) {
|
||||
$content->pull($rowId);
|
||||
|
||||
if ($content->has($cartItem->rowId))
|
||||
{
|
||||
if ($content->has($cartItem->rowId)) {
|
||||
$existingCartItem = $this->get($cartItem->rowId);
|
||||
$cartItem->setQuantity($existingCartItem->qty + $cartItem->qty);
|
||||
}
|
||||
}
|
||||
|
||||
if ($cartItem->qty <= 0)
|
||||
{
|
||||
if ($cartItem->qty <= 0) {
|
||||
$this->remove($cartItem->rowId);
|
||||
return;
|
||||
}
|
||||
else
|
||||
} else {
|
||||
$content->put($cartItem->rowId, $cartItem);
|
||||
}
|
||||
|
||||
$this->events->fire('cart.updated', $cartItem);
|
||||
|
||||
@@ -250,8 +248,10 @@ class Cart
|
||||
*/
|
||||
public function content()
|
||||
{
|
||||
if (is_null($this->session->get($this->instance)))
|
||||
if (is_null($this->session->get($this->instance))) {
|
||||
return new Collection([]);
|
||||
}
|
||||
|
||||
return $this->session->get($this->instance);
|
||||
}
|
||||
|
||||
@@ -445,8 +445,9 @@ class Cart
|
||||
*/
|
||||
public function associate($rowId, $model)
|
||||
{
|
||||
if(is_string($model) && ! class_exists($model))
|
||||
if(is_string($model) && ! class_exists($model)) {
|
||||
throw new UnknownModelException("The supplied model {$model} does not exist.");
|
||||
}
|
||||
|
||||
$cartItem = $this->get($rowId);
|
||||
|
||||
@@ -490,8 +491,7 @@ class Cart
|
||||
$this->taxRate = $taxRate;
|
||||
|
||||
$content = $this->getContent();
|
||||
if ($content && $content->count())
|
||||
{
|
||||
if ($content && $content->count()) {
|
||||
$content->each(function ($item, $key) {
|
||||
$item->setTaxRate($this->taxRate);
|
||||
});
|
||||
@@ -529,8 +529,7 @@ class Cart
|
||||
$this->discount = $discount;
|
||||
|
||||
$content = $this->getContent();
|
||||
if ($content && $content->count())
|
||||
{
|
||||
if ($content && $content->count()) {
|
||||
$content->each(function ($item, $key) {
|
||||
$item->setDiscountRate($this->discount);
|
||||
});
|
||||
@@ -547,8 +546,9 @@ class Cart
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
if ($this->storedCartWithIdentifierExists($identifier))
|
||||
if ($this->storedCartWithIdentifierExists($identifier)) {
|
||||
throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored.");
|
||||
}
|
||||
|
||||
$this->getConnection()->table($this->getTableName())->insert([
|
||||
'identifier' => $identifier,
|
||||
@@ -567,8 +567,9 @@ class Cart
|
||||
*/
|
||||
public function restore($identifier)
|
||||
{
|
||||
if( ! $this->storedCartWithIdentifierExists($identifier))
|
||||
if( ! $this->storedCartWithIdentifierExists($identifier)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$stored = $this->getConnection()->table($this->getTableName())
|
||||
->where('identifier', $identifier)->first();
|
||||
@@ -605,16 +606,18 @@ class Cart
|
||||
*/
|
||||
public function merge( $identifier, $keepDiscount = false, $keepTax = false )
|
||||
{
|
||||
if( ! $this->storedCartWithIdentifierExists($identifier))
|
||||
if( ! $this->storedCartWithIdentifierExists($identifier)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$stored = $this->getConnection()->table($this->getTableName())
|
||||
->where('identifier', $identifier)->first();
|
||||
|
||||
$storedContent = unserialize($stored->content);
|
||||
|
||||
foreach ($storedContent as $cartItem)
|
||||
foreach ($storedContent as $cartItem) {
|
||||
$this->addCartItem($cartItem, $keepDiscount, $keepTax);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -627,17 +630,19 @@ class Cart
|
||||
*/
|
||||
public function __get($attribute)
|
||||
{
|
||||
switch($attribute)
|
||||
{
|
||||
case 'total':
|
||||
return $this->total();
|
||||
case 'tax':
|
||||
return $this->tax();
|
||||
case 'subtotal':
|
||||
return $this->subtotal();
|
||||
default:
|
||||
return null;
|
||||
if($attribute === 'total') {
|
||||
return $this->total();
|
||||
}
|
||||
|
||||
if($attribute === 'tax') {
|
||||
return $this->tax();
|
||||
}
|
||||
|
||||
if($attribute === 'subtotal') {
|
||||
return $this->subtotal();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -647,9 +652,11 @@ class Cart
|
||||
*/
|
||||
protected function getContent()
|
||||
{
|
||||
if ($this->session->has($this->instance))
|
||||
return $this->session->get($this->instance);
|
||||
return new Collection;
|
||||
$content = $this->session->has($this->instance)
|
||||
? $this->session->get($this->instance)
|
||||
: new Collection;
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -687,8 +694,8 @@ class Cart
|
||||
*/
|
||||
private function isMulti($item)
|
||||
{
|
||||
if ( ! is_array($item))
|
||||
return false;
|
||||
if ( ! is_array($item)) return false;
|
||||
|
||||
return is_array(head($item)) || head($item) instanceof Buyable;
|
||||
}
|
||||
|
||||
@@ -708,7 +715,9 @@ class Cart
|
||||
*/
|
||||
private function getConnection()
|
||||
{
|
||||
return app(DatabaseManager::class)->connection( $this->getConnectionName() );
|
||||
$connectionName = $this->getConnectionName();
|
||||
|
||||
return app(DatabaseManager::class)->connection($connectionName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -729,6 +738,7 @@ class Cart
|
||||
private function getConnectionName()
|
||||
{
|
||||
$connection = config('cart.database.connection');
|
||||
|
||||
return is_null($connection) ? config('database.default') : $connection;
|
||||
}
|
||||
|
||||
@@ -743,14 +753,15 @@ class Cart
|
||||
*/
|
||||
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
|
||||
{
|
||||
if(is_null($decimals))
|
||||
$decimals = config('cart.format.decimals', 2);
|
||||
|
||||
if(is_null($decimalPoint))
|
||||
$decimalPoint = config('cart.format.decimal_point', '.');
|
||||
|
||||
if(is_null($thousandSeperator))
|
||||
$thousandSeperator = config('cart.format.thousand_separator', ',');
|
||||
if(is_null($decimals)){
|
||||
$decimals = is_null(config('cart.format.decimals')) ? 2 : config('cart.format.decimals');
|
||||
}
|
||||
if(is_null($decimalPoint)){
|
||||
$decimalPoint = is_null(config('cart.format.decimal_point')) ? '.' : config('cart.format.decimal_point');
|
||||
}
|
||||
if(is_null($thousandSeperator)){
|
||||
$thousandSeperator = is_null(config('cart.format.thousand_separator')) ? ',' : config('cart.format.thousand_separator');
|
||||
}
|
||||
|
||||
return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
@@ -335,43 +335,47 @@ class CartItem implements Arrayable, Jsonable
|
||||
return $this->{$attribute};
|
||||
}
|
||||
|
||||
switch($attribute)
|
||||
{
|
||||
case 'discount':
|
||||
return $this->price * ($this->discountRate / 100);
|
||||
|
||||
case 'priceTarget':
|
||||
return $this->price - $this->discount;
|
||||
|
||||
case 'subtotal':
|
||||
return $this->priceTarget * $this->qty;
|
||||
|
||||
case 'tax':
|
||||
return $this->priceTarget * ($this->taxRate / 100);
|
||||
|
||||
case 'priceTax':
|
||||
return $this->priceTarget + $this->tax;
|
||||
|
||||
case 'total':
|
||||
return $this->priceTax * $this->qty;
|
||||
|
||||
case 'taxTotal':
|
||||
return $this->tax * $this->qty;
|
||||
|
||||
case 'discountTotal':
|
||||
return $this->discount * $this->qty;
|
||||
|
||||
case 'weightTotal':
|
||||
return $this->weight * $this->qty;
|
||||
|
||||
case 'model':
|
||||
if (isset($this->associatedModel))
|
||||
return with(new $this->associatedModel)->find($this->id);
|
||||
return null;
|
||||
|
||||
default:
|
||||
return null;
|
||||
if($attribute === 'discount') {
|
||||
return $this->price * ($this->discountRate / 100);
|
||||
}
|
||||
|
||||
if($attribute === 'priceTarget') {
|
||||
return $this->price - $this->discount;
|
||||
}
|
||||
|
||||
if($attribute === 'subtotal') {
|
||||
return $this->qty * $this->priceTarget;
|
||||
}
|
||||
|
||||
if($attribute === 'tax') {
|
||||
return $this->priceTarget * ($this->taxRate / 100);
|
||||
}
|
||||
|
||||
if($attribute === 'priceTax') {
|
||||
return $this->priceTarget + $this->tax;
|
||||
}
|
||||
|
||||
if($attribute === 'total') {
|
||||
return $this->qty * $this->priceTax;
|
||||
}
|
||||
|
||||
if($attribute === 'taxTotal') {
|
||||
return $this->tax * $this->qty;
|
||||
}
|
||||
|
||||
if($attribute === 'discountTotal') {
|
||||
return $this->discount * $this->qty;
|
||||
}
|
||||
|
||||
if($attribute === 'weightTotal') {
|
||||
return $this->qty * $this->weight;
|
||||
}
|
||||
|
||||
if($attribute === 'model' && isset($this->associatedModel)) {
|
||||
return with(new $this->associatedModel)->find($this->id);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -470,14 +474,17 @@ class CartItem implements Arrayable, Jsonable
|
||||
*/
|
||||
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
|
||||
{
|
||||
if (is_null($decimals))
|
||||
$decimals = config('cart.format.decimals', 2);
|
||||
if (is_null($decimals)){
|
||||
$decimals = is_null(config('cart.format.decimals')) ? 2 : config('cart.format.decimals');
|
||||
}
|
||||
|
||||
if (is_null($decimalPoint))
|
||||
$decimalPoint = config('cart.format.decimal_point', '.');
|
||||
if (is_null($decimalPoint)){
|
||||
$decimalPoint = is_null(config('cart.format.decimal_point')) ? '.' : config('cart.format.decimal_point');
|
||||
}
|
||||
|
||||
if (is_null($thousandSeperator))
|
||||
$thousandSeperator = config('cart.format.thousand_separator', ',');
|
||||
if (is_null($thousandSeperator)){
|
||||
$thousandSeperator = is_null(config('cart.format.thousand_separator')) ? ',' : config('cart.format.thousand_separator');
|
||||
}
|
||||
|
||||
return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user