08 2024
This commit is contained in:
parent
04d677d37a
commit
bfa3bb1df4
1191 changed files with 637397 additions and 10619 deletions
54
packages/digital-bird/shoppingcart/src/CanBeBought.php
Normal file
54
packages/digital-bird/shoppingcart/src/CanBeBought.php
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart;
|
||||
|
||||
trait CanBeBought
|
||||
{
|
||||
/**
|
||||
* Get the identifier of the Buyable item.
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
public function getBuyableIdentifier($options = null)
|
||||
{
|
||||
return method_exists($this, 'getKey') ?
|
||||
$this->getKey() :
|
||||
$this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the description or title of the Buyable item.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBuyableDescription($options = null)
|
||||
{
|
||||
if (property_exists($this, 'name')) {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
if (property_exists($this, 'title')) {
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
if (property_exists($this, 'description')) {
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the price of the Buyable item.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getBuyablePrice($options = null)
|
||||
{
|
||||
if (property_exists($this, 'price')) {
|
||||
return $this->price;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
842
packages/digital-bird/shoppingcart/src/Cart.php
Normal file
842
packages/digital-bird/shoppingcart/src/Cart.php
Normal file
|
|
@ -0,0 +1,842 @@
|
|||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart;
|
||||
|
||||
use Closure;
|
||||
use DateTime;
|
||||
use Gloudemans\Shoppingcart\Traits\CartHelper;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Session\SessionManager;
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Gloudemans\Shoppingcart\Contracts\Buyable;
|
||||
use Gloudemans\Shoppingcart\Exceptions\UnknownModelException;
|
||||
use Gloudemans\Shoppingcart\Exceptions\InvalidRowIDException;
|
||||
use Gloudemans\Shoppingcart\Exceptions\CartAlreadyStoredException;
|
||||
|
||||
class Cart
|
||||
{
|
||||
use CartHelper;
|
||||
|
||||
const DEFAULT_INSTANCE = 'default';
|
||||
|
||||
public $items;
|
||||
public $fees;
|
||||
|
||||
/**
|
||||
* Instance of the session manager.
|
||||
*
|
||||
* @var \Illuminate\Session\SessionManager
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
/**
|
||||
* Instance of the event dispatcher.
|
||||
*
|
||||
* @var \Illuminate\Contracts\Events\Dispatcher
|
||||
*/
|
||||
private $events;
|
||||
|
||||
/**
|
||||
* Holds the current cart instance.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $instance;
|
||||
|
||||
/**
|
||||
* Cart constructor.
|
||||
*
|
||||
* @param \Illuminate\Session\SessionManager $session
|
||||
* @param \Illuminate\Contracts\Events\Dispatcher $events
|
||||
*/
|
||||
public function __construct(SessionManager $session, Dispatcher $events)
|
||||
{
|
||||
$this->items = new Collection;
|
||||
$this->fees = new Collection;
|
||||
$this->session = $session;
|
||||
$this->events = $events;
|
||||
|
||||
$this->instance(self::DEFAULT_INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current cart instance.
|
||||
*
|
||||
* @param string|null $instance
|
||||
* @return \Gloudemans\Shoppingcart\Cart
|
||||
*/
|
||||
public function instance($instance = null)
|
||||
{
|
||||
$instance = $instance ?: self::DEFAULT_INSTANCE;
|
||||
|
||||
$this->instance = sprintf('%s.%s', 'cart', $instance);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current cart instance.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function currentInstance()
|
||||
{
|
||||
return str_replace('cart.', '', $this->instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an item to the cart.
|
||||
*
|
||||
* @param $id
|
||||
* @param $name
|
||||
* @param $qty
|
||||
* @param $price
|
||||
* @param $taxRate
|
||||
* @param $taxIncluded
|
||||
* @param array $options
|
||||
* @param array $eventOptions
|
||||
* @return array|array[]|CartItem|CartItem[]
|
||||
*/
|
||||
public function add($id, $name = null, $qty = null, $price = null, $taxRate = null, $taxIncluded = false, array $options = [], array $eventOptions = [])
|
||||
{
|
||||
if ($this->isMulti($id)) {
|
||||
return array_map(function ($item) {
|
||||
return $this->add($item);
|
||||
}, $id);
|
||||
}
|
||||
|
||||
if ($id instanceof CartItem) {
|
||||
$cartItem = $id;
|
||||
} else {
|
||||
$cartItem = $this->createCartItem($id, $name, $qty, $price, $taxRate, $taxIncluded, $options);
|
||||
}
|
||||
|
||||
$content = $this->getContent();
|
||||
|
||||
if ($content->has($cartItem->rowId)) {
|
||||
$cartItem->qty += $content->get($cartItem->rowId)->qty;
|
||||
}
|
||||
|
||||
$content->put($cartItem->rowId, $cartItem);
|
||||
|
||||
$this->items = $content;
|
||||
|
||||
$this->session->put($this->instance, $this->toArray());
|
||||
|
||||
$eventOptions = array_merge([
|
||||
'cartInstance' => $this->currentInstance(),
|
||||
'cartItem' => $cartItem,
|
||||
], $eventOptions);
|
||||
|
||||
$this->events->dispatch('cart.added', [
|
||||
$eventOptions,
|
||||
]);
|
||||
|
||||
return $cartItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the cart item with the given rowId.
|
||||
*
|
||||
* @param $rowId
|
||||
* @param $qty
|
||||
* @param array $eventOptions
|
||||
* @return CartItem|void
|
||||
*/
|
||||
public function update($rowId, $qty, array $eventOptions = [])
|
||||
{
|
||||
$cartItem = $this->get($rowId);
|
||||
|
||||
if ($qty instanceof Buyable) {
|
||||
$cartItem->updateFromBuyable($qty);
|
||||
} elseif (is_array($qty)) {
|
||||
$cartItem->updateFromArray($qty);
|
||||
} else {
|
||||
$cartItem->qty = $qty;
|
||||
}
|
||||
|
||||
$content = $this->getContent();
|
||||
|
||||
if ($rowId !== $cartItem->rowId) {
|
||||
if ($content->has($cartItem->rowId)) {
|
||||
$existingCartItem = $this->get($cartItem->rowId);
|
||||
$cartItem->setQuantity($existingCartItem->qty + $cartItem->qty);
|
||||
}
|
||||
|
||||
$content = $content->mapWithKeys(function ($val, $key) use ($rowId, $cartItem) {
|
||||
if ($key === $rowId) {
|
||||
return [ $cartItem->rowId => $cartItem ];
|
||||
}
|
||||
|
||||
return [ $key => $val ];
|
||||
});
|
||||
|
||||
$this->items = $content;
|
||||
}
|
||||
|
||||
if ($cartItem->qty <= 0) {
|
||||
$this->remove($cartItem->rowId);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->session->put($this->instance, $this->toArray());
|
||||
|
||||
$eventOptions = array_merge([
|
||||
'cartInstance' => $this->currentInstance(),
|
||||
'cartItem' => $cartItem,
|
||||
], $eventOptions);
|
||||
|
||||
$this->events->dispatch('cart.updated', [
|
||||
$eventOptions
|
||||
]);
|
||||
|
||||
return $cartItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the cart item with the given rowId from the cart.
|
||||
*
|
||||
* @param $rowId
|
||||
* @param array $eventOptions
|
||||
* @return void
|
||||
*/
|
||||
public function remove($rowId, array $eventOptions = [])
|
||||
{
|
||||
$cartItem = $this->get($rowId);
|
||||
|
||||
$content = $this->getContent();
|
||||
|
||||
$content->pull($cartItem->rowId);
|
||||
|
||||
$this->items = $content;
|
||||
|
||||
$this->session->put($this->instance, $this->toArray());
|
||||
|
||||
$eventOptions = array_merge([
|
||||
'cartInstance' => $this->currentInstance(),
|
||||
'cartItem' => $cartItem,
|
||||
], $eventOptions);
|
||||
|
||||
$this->events->dispatch('cart.removed', [
|
||||
$eventOptions
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a cart item from the cart by its rowId.
|
||||
*
|
||||
* @param string $rowId
|
||||
* @return \Gloudemans\Shoppingcart\CartItem
|
||||
*/
|
||||
public function get($rowId)
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
if ($content->has($rowId) === false) {
|
||||
throw new InvalidRowIDException("The cart does not contain rowId {$rowId}.");
|
||||
}
|
||||
|
||||
return $content->get($rowId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the current cart instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function destroy()
|
||||
{
|
||||
$this->session->remove($this->instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content of the cart.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function content()
|
||||
{
|
||||
return $this->getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of items in the cart.
|
||||
*
|
||||
* @return int|float
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
return $content->sum('qty');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total price of the items in the cart.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null, $withFees = true)
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$total = $content->reduce(function ($total, CartItem $cartItem) {
|
||||
return $total + ($cartItem->total);
|
||||
}, 0);
|
||||
|
||||
if ($withFees === true) {
|
||||
$fees = $this->feeTotal(null, null, null, true);
|
||||
|
||||
$total = $total + $fees;
|
||||
}
|
||||
|
||||
$decimals = is_null($decimals) ? config('cart.format.total_decimals') : $decimals;
|
||||
|
||||
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total tax of the items in the cart.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return float
|
||||
*/
|
||||
public function tax($decimals = null, $decimalPoint = null, $thousandSeperator = null, $withFees = true)
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$tax = $content->reduce(function ($tax, CartItem $cartItem) {
|
||||
return $tax + ($cartItem->taxTotal);
|
||||
}, 0);
|
||||
|
||||
if ($withFees === true) {
|
||||
$fees = $this->feeTax();
|
||||
|
||||
$tax = $tax + floatval($fees);
|
||||
}
|
||||
|
||||
$decimals = is_null($decimals) ? config('cart.format.tax_decimals') : $decimals;
|
||||
|
||||
return $this->numberFormat($tax, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total tax of the items in the cart.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return float
|
||||
*/
|
||||
public function feeTax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$tax = 0;
|
||||
|
||||
foreach ($this->getFees() as $fee) {
|
||||
$tax += $fee->tax;
|
||||
}
|
||||
|
||||
$decimals = is_null($decimals) ? config('cart.format.fee_total_tax_decimals') : $decimals;
|
||||
|
||||
return $this->numberFormat($tax, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the subtotal (ex tax) of the items in the cart.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return float
|
||||
*/
|
||||
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$subTotal = $content->reduce(function ($subTotal, CartItem $cartItem) {
|
||||
return $subTotal + ($cartItem->subtotal);
|
||||
}, 0);
|
||||
|
||||
$decimals = is_null($decimals) ? config('cart.format.subtotal_ex_tax_decimals') : $decimals;
|
||||
|
||||
return $this->numberFormat($subTotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the subtotal (total - tax) of the items in the cart.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return float
|
||||
*/
|
||||
public function subtotalTax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$subTotal = $content->reduce(function ($subTotal, CartItem $cartItem) {
|
||||
return $subTotal + ($cartItem->subtotalTax);
|
||||
}, 0);
|
||||
|
||||
$decimals = is_null($decimals) ? config('cart.format.subtotal_inc_tax_decimals') : $decimals;
|
||||
|
||||
return $this->numberFormat($subTotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the cart content for a cart item matching the given search closure.
|
||||
*
|
||||
* @param Closure $search
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function search(Closure $search)
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
return $content->filter($search);
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate the cart item with the given rowId with the given model.
|
||||
*
|
||||
* @param string $rowId
|
||||
* @param mixed $model
|
||||
* @return void
|
||||
*/
|
||||
public function associate($rowId, $model)
|
||||
{
|
||||
if (
|
||||
is_string($model) === true &&
|
||||
class_exists($model) === false
|
||||
) {
|
||||
throw new UnknownModelException("The supplied model {$model} does not exist.");
|
||||
}
|
||||
|
||||
$cartItem = $this->get($rowId);
|
||||
|
||||
$cartItem->associate($model);
|
||||
|
||||
$content = $this->getContent();
|
||||
|
||||
$content->put($cartItem->rowId, $cartItem);
|
||||
|
||||
$this->session->put($this->instance, $this->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the tax rate for the cart item with the given rowId.
|
||||
*
|
||||
* @param string $rowId
|
||||
* @param int|float $taxRate
|
||||
* @return void
|
||||
*/
|
||||
public function setTax($rowId, $taxRate)
|
||||
{
|
||||
$cartItem = $this->get($rowId);
|
||||
|
||||
$cartItem->setTaxRate($taxRate);
|
||||
|
||||
$content = $this->getContent();
|
||||
|
||||
$content->put($cartItem->rowId, $cartItem);
|
||||
|
||||
$this->items = $content;
|
||||
|
||||
$this->session->put($this->instance, $this->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Store an the current instance of the cart.
|
||||
*
|
||||
* @param $identifier
|
||||
* @param array $eventOptions
|
||||
* @return void
|
||||
*/
|
||||
public function store($identifier, array $eventOptions = [])
|
||||
{
|
||||
// Remove any existing identifiers
|
||||
// Although possibly first or update could work in future
|
||||
$this
|
||||
->getConnection()
|
||||
->table($this->getTableName())
|
||||
->where('identifier', $identifier)
|
||||
->delete();
|
||||
|
||||
// Insert into the database with the new cart
|
||||
$content = $this->getContent();
|
||||
//dd($content);
|
||||
$this
|
||||
->getConnection()
|
||||
->table($this->getTableName())
|
||||
->insert([
|
||||
'identifier' => $identifier,
|
||||
'instance' => $this->currentInstance(),
|
||||
'content' => serialize($content),
|
||||
'created_at'=> new DateTime(),
|
||||
]);
|
||||
|
||||
$eventOptions = array_merge([
|
||||
'cartInstance' => $this->currentInstance(),
|
||||
], $eventOptions);
|
||||
|
||||
$this->events->dispatch('cart.stored', [
|
||||
$eventOptions,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore the cart with the given identifier.
|
||||
*
|
||||
* @param $identifier
|
||||
* @param array $eventOptions
|
||||
* @return void
|
||||
*/
|
||||
public function restore($identifier, array $eventOptions = [], $delteStoredCart = true)
|
||||
{
|
||||
if ($this->storedCartWithIdentifierExists($identifier) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Find any existing carts by identifier
|
||||
$stored = $this
|
||||
->getConnection()
|
||||
->table($this->getTableName())
|
||||
->where('identifier', $identifier)
|
||||
->first();
|
||||
|
||||
// Unserialize the content (either array if new, or collection if old)
|
||||
$storedContent = unserialize($stored->content);
|
||||
|
||||
$currentInstance = $this->currentInstance();
|
||||
|
||||
$this->instance($stored->instance);
|
||||
|
||||
$content = $this->getContent();
|
||||
|
||||
// If the new approach and is array, set this class up.
|
||||
// Note that it overrides any existing items in cart
|
||||
// Does not add to existing.
|
||||
if (is_array($storedContent)) {
|
||||
$this->fromArray($storedContent);
|
||||
}
|
||||
|
||||
// If the old approach and is Collection, push into existing items
|
||||
if ($storedContent instanceof Collection) {
|
||||
foreach ($storedContent as $cartItem) {
|
||||
$content->put($cartItem->rowId, $cartItem);
|
||||
}
|
||||
}
|
||||
|
||||
$eventOptions = array_merge([
|
||||
'cartInstance' => $this->currentInstance(),
|
||||
], $eventOptions);
|
||||
|
||||
$this->events->dispatch('cart.restored', [
|
||||
$eventOptions,
|
||||
]);
|
||||
|
||||
$this->session->put($this->instance, $content);
|
||||
|
||||
$this->instance($currentInstance);
|
||||
|
||||
if($delteStoredCart === true) {
|
||||
$this->deleteStoredCart($identifier);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a specific fee from the fees array.
|
||||
*
|
||||
* @param $name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFee($name)
|
||||
{
|
||||
return $this->fees->get($name, new CartFee(null, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows to charge for additional fees that may or may not be taxable
|
||||
* ex - service fee , delivery fee, tips.
|
||||
*
|
||||
* Because it uses ->put, the name must be unique otherwise will be overwritten.
|
||||
*
|
||||
* @param $name
|
||||
* @param $amount
|
||||
* @param $taxRate
|
||||
* @param array $options
|
||||
*/
|
||||
public function addFee($name, $amount, $taxRate = null, array $options = [])
|
||||
{
|
||||
$this->fees->put($name, new CartFee($amount, $taxRate, $options));
|
||||
|
||||
$this->session->put($this->instance, $this->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a fee from the fee array.
|
||||
*
|
||||
* @todo test to see if i need to restore this
|
||||
*
|
||||
* @param $name
|
||||
*/
|
||||
public function removeFee($name)
|
||||
{
|
||||
$this->fees->forget($name);
|
||||
|
||||
$this->session->put($this->instance, $this->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all the fees set in the cart.
|
||||
*/
|
||||
public function removeFees()
|
||||
{
|
||||
$this->fees = new Collection;
|
||||
|
||||
$this->session->put($this->instance, $this->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the fee totals.
|
||||
*
|
||||
* @param bool $format
|
||||
* @param bool $withTax
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function feeTotal($decimals = null, $decimalPoint = null, $thousandSeperator = null, $withTax = true)
|
||||
{
|
||||
$feeTotal = 0;
|
||||
|
||||
foreach ($this->getFees() as $fee) {
|
||||
$feeTotal += $fee->amount;
|
||||
|
||||
if ($withTax === true && $fee->taxRate > 0) {
|
||||
$feeTotal += $fee->tax;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->numberFormat($feeTotal, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the fees on the cart object.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFees()
|
||||
{
|
||||
return $this->fees;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method to make accessing the total, tax and subtotal properties possible.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @return float|null
|
||||
*/
|
||||
public function __get($attribute)
|
||||
{
|
||||
if ($attribute === 'total') {
|
||||
return $this->total();
|
||||
}
|
||||
|
||||
if ($attribute === 'feeTotal') {
|
||||
return $this->feeTotal(null, null, null, false);
|
||||
}
|
||||
|
||||
if ($attribute === 'feeTotalTax') {
|
||||
return $this->feeTotal(null, null, null, true);
|
||||
}
|
||||
|
||||
if ($attribute === 'tax') {
|
||||
return $this->tax();
|
||||
}
|
||||
|
||||
if ($attribute === 'feeTax') {
|
||||
return $this->feeTax();
|
||||
}
|
||||
|
||||
if ($attribute === 'subtotal') {
|
||||
return $this->subtotal();
|
||||
}
|
||||
|
||||
if ($attribute === 'subtotalTax') {
|
||||
return $this->subtotalTax();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'items' => $this->items,
|
||||
'fees' => $this->fees,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $array
|
||||
* @return $this
|
||||
*/
|
||||
public function fromArray($array)
|
||||
{
|
||||
$this->items = $array['items'];
|
||||
$this->fees = $array['fees'];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the stored cart with given identifier
|
||||
*
|
||||
* @param mixed $identifier
|
||||
*/
|
||||
public function deleteStoredCart($identifier)
|
||||
{
|
||||
$this
|
||||
->getConnection()
|
||||
->table($this->getTableName())
|
||||
->where('identifier', $identifier)
|
||||
->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the carts content, if there is no cart content set yet, return a new empty Collection
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function getContent(): Collection
|
||||
{
|
||||
$instanceExists = $this->session->has($this->instance);
|
||||
|
||||
if ($instanceExists === false) {
|
||||
$this->items = new Collection;
|
||||
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
$instance = $this->session->get($this->instance);
|
||||
// dd($instance);
|
||||
// If new approach, set $this variables
|
||||
if (is_array($instance) === true) {
|
||||
$this->items = $instance['items'];
|
||||
$this->fees = $instance['fees'];
|
||||
}
|
||||
|
||||
if ($instance instanceof Collection) {
|
||||
$this->items = $instance;
|
||||
}
|
||||
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new CartItem from the supplied attributes.
|
||||
*
|
||||
* @param mixed $id
|
||||
* @param mixed $name
|
||||
* @param int|float $qty
|
||||
* @param float $price
|
||||
* @param array $options
|
||||
* @return \Gloudemans\Shoppingcart\CartItem
|
||||
*/
|
||||
private function createCartItem($id, $name, $qty, $price, $taxRate, bool $taxIncluded, array $options): CartItem
|
||||
{
|
||||
if ($id instanceof Buyable) {
|
||||
$cartItem = CartItem::fromBuyable($id, $qty ?: []);
|
||||
$cartItem->setQuantity($name ?: 1);
|
||||
$cartItem->associate($id);
|
||||
} elseif (is_array($id)) {
|
||||
$cartItem = CartItem::fromArray($id);
|
||||
$cartItem->setQuantity($id['qty']);
|
||||
} else {
|
||||
$cartItem = CartItem::fromAttributes($id, $name, $price, $options);
|
||||
$cartItem->setQuantity($qty);
|
||||
}
|
||||
|
||||
$taxRate = is_int($taxRate) ? $taxRate : config('cart.tax');
|
||||
|
||||
$cartItem->setTaxRate($taxRate);
|
||||
$cartItem->setTaxIncluded($taxIncluded);
|
||||
|
||||
return $cartItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the item is a multidimensional array or an array of Buyables.
|
||||
*
|
||||
* @param mixed $item
|
||||
* @return bool
|
||||
*/
|
||||
private function isMulti($item): bool
|
||||
{
|
||||
if (is_array($item) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return is_array(head($item)) || head($item) instanceof Buyable === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $identifier
|
||||
* @return bool
|
||||
*/
|
||||
protected function storedCartWithIdentifierExists($identifier): bool
|
||||
{
|
||||
return $this
|
||||
->getConnection()
|
||||
->table($this->getTableName())
|
||||
->where('identifier', $identifier)
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the database connection.
|
||||
*
|
||||
* @return \Illuminate\Database\Connection
|
||||
*/
|
||||
protected function getConnection()
|
||||
{
|
||||
$connectionName = $this->getConnectionName();
|
||||
|
||||
return app(DatabaseManager::class)->connection($connectionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the database table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getTableName()
|
||||
{
|
||||
return config('cart.database.table', 'shoppingcart');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the database connection name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getConnectionName()
|
||||
{
|
||||
$connection = config('cart.database.connection');
|
||||
|
||||
return is_null($connection) ? config('database.default') : $connection;
|
||||
}
|
||||
}
|
||||
118
packages/digital-bird/shoppingcart/src/CartFee.php
Normal file
118
packages/digital-bird/shoppingcart/src/CartFee.php
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart;
|
||||
|
||||
use Gloudemans\Shoppingcart\Traits\CartHelper;
|
||||
|
||||
/**
|
||||
* Class CartFee.
|
||||
*/
|
||||
class CartFee
|
||||
{
|
||||
use CartHelper;
|
||||
|
||||
public $amount;
|
||||
public $taxRate = 0;
|
||||
public $options = [];
|
||||
|
||||
/**
|
||||
* CartFee constructor.
|
||||
*
|
||||
* @param $amount
|
||||
* @param $taxRate
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct($amount, $taxRate = null, array $options = [])
|
||||
{
|
||||
$this->amount = floatval($amount);
|
||||
$this->taxRate = is_null($taxRate) ? config('cart.tax') : $taxRate;
|
||||
$this->options = new CartFeeOptions($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted fee amount without TAX.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function amountWithouTax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$decimals = is_null($decimals) ? config('cart.format.fee_ex_tax_decimals') : $decimals;
|
||||
|
||||
return $this->numberFormat($this->price, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted fee amount with TAX.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function amountTax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$priceTax = $this->amount + $this->tax;
|
||||
$decimals = is_null($decimals) ? config('cart.format.fee_inc_tax_decimals') : $decimals;
|
||||
|
||||
return $this->numberFormat($priceTax, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the formatted amount.
|
||||
*
|
||||
* @param bool $format
|
||||
* @param bool $withTax
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAmount($format = true, $withTax = false)
|
||||
{
|
||||
$total = $this->amount;
|
||||
|
||||
if ($withTax) {
|
||||
$total += $this->taxRate * $total;
|
||||
}
|
||||
|
||||
return $this->numberFormat($total);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function tax()
|
||||
{
|
||||
$tax = $this->amount * $this->taxRate / 100;
|
||||
|
||||
return $this->numberFormat($tax);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method to make accessing the total, tax and subtotal properties possible.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @return float|null
|
||||
*/
|
||||
public function __get($attribute)
|
||||
{
|
||||
if (property_exists($this, $attribute)) {
|
||||
return $this->{$attribute};
|
||||
}
|
||||
|
||||
if ($attribute === 'amountWithouTax') {
|
||||
return $this->amountWithouTax();
|
||||
}
|
||||
|
||||
if ($attribute === 'amountTax') {
|
||||
return $this->amountTax();
|
||||
}
|
||||
|
||||
if ($attribute === 'tax') {
|
||||
return $this->tax();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
19
packages/digital-bird/shoppingcart/src/CartFeeOptions.php
Normal file
19
packages/digital-bird/shoppingcart/src/CartFeeOptions.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class CartFeeOptions extends Collection
|
||||
{
|
||||
/**
|
||||
* Get the option by the given key.
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($key)
|
||||
{
|
||||
return $this->get($key);
|
||||
}
|
||||
}
|
||||
470
packages/digital-bird/shoppingcart/src/CartItem.php
Normal file
470
packages/digital-bird/shoppingcart/src/CartItem.php
Normal file
|
|
@ -0,0 +1,470 @@
|
|||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart;
|
||||
|
||||
use Gloudemans\Shoppingcart\Traits\CartHelper;
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Gloudemans\Shoppingcart\Contracts\Buyable;
|
||||
use Illuminate\Contracts\Support\Jsonable;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class CartItem implements Arrayable, Jsonable
|
||||
{
|
||||
use CartHelper;
|
||||
|
||||
/**
|
||||
* The rowID of the cart item.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $rowId;
|
||||
|
||||
/**
|
||||
* The ID of the cart item.
|
||||
*
|
||||
* @var int|string
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* The quantity for this cart item.
|
||||
*
|
||||
* @var int|float
|
||||
*/
|
||||
public $qty;
|
||||
|
||||
/**
|
||||
* The name of the cart item.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* The price without TAX of the cart item.
|
||||
*
|
||||
* @var float
|
||||
*/
|
||||
public $price;
|
||||
|
||||
/**
|
||||
* The options for this cart item.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $options;
|
||||
|
||||
/**
|
||||
* The FQN of the associated model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
private $associatedModel = null;
|
||||
|
||||
/**
|
||||
* The tax rate for the cart item.
|
||||
*
|
||||
* @var int|float
|
||||
*/
|
||||
private $taxRate = 0;
|
||||
|
||||
/**
|
||||
* Whether the tax is already included.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $taxIncluded = false;
|
||||
|
||||
/**
|
||||
* Is item saved for later.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
private $isSaved = false;
|
||||
|
||||
/**
|
||||
* CartItem constructor.
|
||||
*
|
||||
* @param int|string $id
|
||||
* @param string $name
|
||||
* @param float $price
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct($id, $name, $price, array $options = [])
|
||||
{
|
||||
if(empty($id)) {
|
||||
throw new \InvalidArgumentException('Please supply a valid identifier.');
|
||||
}
|
||||
if(empty($name)) {
|
||||
throw new \InvalidArgumentException('Please supply a valid name.');
|
||||
}
|
||||
if(strlen($price) < 0 || ! is_numeric($price)) {
|
||||
throw new \InvalidArgumentException('Please supply a valid price.');
|
||||
}
|
||||
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->price = floatval($price);
|
||||
$this->options = new CartItemOptions($options);
|
||||
$this->rowId = $this->generateRowId($id, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted price without TAX.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function price($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$decimals = is_null($decimals) ? config('cart.format.price_ex_tax_decimals') : $decimals;
|
||||
|
||||
return $this->numberFormat($this->price, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted price with TAX.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function priceTax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
if ($this->taxIncluded === true) {
|
||||
$priceTax = $this->price;
|
||||
} else {
|
||||
$priceTax = $this->price + $this->tax;
|
||||
}
|
||||
|
||||
$decimals = is_null($decimals) ? config('cart.format.price_inc_tax_decimals') : $decimals;
|
||||
|
||||
return $this->numberFormat($priceTax, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted subtotal.
|
||||
* Subtotal is price for whole CartItem without TAX
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$subtotal = $this->qty * $this->price;
|
||||
$decimals = is_null($decimals) ? config('cart.format.subtotal_ex_tax_decimals') : $decimals;
|
||||
|
||||
return $this->numberFormat($subtotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted subtotal.
|
||||
* Subtotal is price for whole CartItem with TAX
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function subtotalTax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
if ($this->taxIncluded === true) {
|
||||
$subtotal = $this->subtotal;
|
||||
} else {
|
||||
$subtotal = $this->subtotal + $this->taxTotal;
|
||||
}
|
||||
|
||||
$decimals = is_null($decimals) ? config('cart.format.subtotal_inc_tax_decimals') : $decimals;
|
||||
|
||||
return $this->numberFormat($subtotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted total.
|
||||
* Total is price for whole CartItem with tax
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$total = $this->subtotalTax;
|
||||
$decimals = is_null($decimals) ? config('cart.format.total_decimals') : $decimals;
|
||||
|
||||
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted tax.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function tax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
if ($this->taxIncluded === true) {
|
||||
$tax = $this->price / (1 + $this->taxRate);
|
||||
} else {
|
||||
$tax = $this->price * ($this->taxRate / 100);
|
||||
}
|
||||
|
||||
$decimals = is_null($decimals) ? config('cart.format.tax_decimals') : $decimals;
|
||||
|
||||
return $this->numberFormat($tax, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted tax.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function taxTotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$taxTotal = $this->tax * $this->qty;
|
||||
$decimals = is_null($decimals) ? config('cart.format.tax_total_decimals') : $decimals;
|
||||
|
||||
return $this->numberFormat($taxTotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the quantity for this cart item.
|
||||
*
|
||||
* @param int|float $qty
|
||||
*/
|
||||
public function setQuantity($qty)
|
||||
{
|
||||
if(empty($qty) || ! is_numeric($qty))
|
||||
throw new \InvalidArgumentException('Please supply a valid quantity.');
|
||||
|
||||
$this->qty = $qty;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the cart item from a Buyable.
|
||||
*
|
||||
* @param \Gloudemans\Shoppingcart\Contracts\Buyable $item
|
||||
* @return void
|
||||
*/
|
||||
public function updateFromBuyable(Buyable $item)
|
||||
{
|
||||
$this->id = $item->getBuyableIdentifier($this->options);
|
||||
$this->name = $item->getBuyableDescription($this->options);
|
||||
$this->price = $item->getBuyablePrice($this->options);
|
||||
$this->priceTax = $this->price + $this->tax;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the cart item from an array.
|
||||
*
|
||||
* @param array $attributes
|
||||
* @return void
|
||||
*/
|
||||
public function updateFromArray(array $attributes)
|
||||
{
|
||||
$this->id = array_get($attributes, 'id', $this->id);
|
||||
$this->qty = array_get($attributes, 'qty', $this->qty);
|
||||
$this->name = array_get($attributes, 'name', $this->name);
|
||||
$this->price = array_get($attributes, 'price', $this->price);
|
||||
$this->priceTax = $this->price + $this->tax;
|
||||
$this->options = new CartItemOptions(array_get($attributes, 'options', $this->options));
|
||||
|
||||
$this->rowId = $this->generateRowId($this->id, $this->options->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate the cart item with the given model.
|
||||
*
|
||||
* @param mixed $model
|
||||
* @return \Gloudemans\Shoppingcart\CartItem
|
||||
*/
|
||||
public function associate($model)
|
||||
{
|
||||
$this->associatedModel = is_string($model) ? $model : get_class($model);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the tax rate.
|
||||
*
|
||||
* @param int|float $taxRate
|
||||
* @return \Gloudemans\Shoppingcart\CartItem
|
||||
*/
|
||||
public function setTaxRate($taxRate)
|
||||
{
|
||||
$this->taxRate = $taxRate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the tax rate.
|
||||
*
|
||||
* @param bool $taxIncluded
|
||||
* @return \Gloudemans\Shoppingcart\CartItem
|
||||
*/
|
||||
public function setTaxIncluded(bool $taxIncluded)
|
||||
{
|
||||
$this->taxIncluded = $taxIncluded;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set saved state.
|
||||
*
|
||||
* @param bool $bool
|
||||
* @return \Gloudemans\Shoppingcart\CartItem
|
||||
*/
|
||||
public function setSaved($bool)
|
||||
{
|
||||
$this->isSaved = $bool;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an attribute from the cart item or get the associated model.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($attribute)
|
||||
{
|
||||
if (property_exists($this, $attribute)) {
|
||||
return $this->{$attribute};
|
||||
}
|
||||
|
||||
if ($attribute === 'priceTax') {
|
||||
return $this->priceTax();
|
||||
}
|
||||
|
||||
if ($attribute === 'subtotal') {
|
||||
return $this->subtotal();
|
||||
}
|
||||
|
||||
if ($attribute === 'subtotalTax') {
|
||||
return $this->subtotalTax();
|
||||
}
|
||||
|
||||
if ($attribute === 'total') {
|
||||
return $this->total();
|
||||
}
|
||||
|
||||
if ($attribute === 'tax') {
|
||||
return $this->tax();
|
||||
}
|
||||
|
||||
if ($attribute === 'taxTotal') {
|
||||
return $this->taxTotal();
|
||||
}
|
||||
|
||||
if ($attribute === 'model' && isset($this->associatedModel)) {
|
||||
return with(new $this->associatedModel)->find($this->id);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance from a Buyable.
|
||||
*
|
||||
* @param \Gloudemans\Shoppingcart\Contracts\Buyable $item
|
||||
* @param array $options
|
||||
* @return \Gloudemans\Shoppingcart\CartItem
|
||||
*/
|
||||
public static function fromBuyable(Buyable $item, array $options = [])
|
||||
{
|
||||
return new self($item->getBuyableIdentifier($options), $item->getBuyableDescription($options), $item->getBuyablePrice($options), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance from the given array.
|
||||
*
|
||||
* @param array $attributes
|
||||
* @return \Gloudemans\Shoppingcart\CartItem
|
||||
*/
|
||||
public static function fromArray(array $attributes)
|
||||
{
|
||||
$options = array_get($attributes, 'options', []);
|
||||
|
||||
return new self($attributes['id'], $attributes['name'], $attributes['price'], $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance from the given attributes.
|
||||
*
|
||||
* @param int|string $id
|
||||
* @param string $name
|
||||
* @param float $price
|
||||
* @param array $options
|
||||
* @return \Gloudemans\Shoppingcart\CartItem
|
||||
*/
|
||||
public static function fromAttributes($id, $name, $price, array $options = [])
|
||||
{
|
||||
return new self($id, $name, $price, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique id for the cart item.
|
||||
*
|
||||
* @param string $id
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
protected function generateRowId($id, array $options)
|
||||
{
|
||||
ksort($options);
|
||||
|
||||
$uniqueString = '';
|
||||
|
||||
if (config('cart.allow_multiple_same_id') === true) {
|
||||
$uniqueString = microtime(true);
|
||||
}
|
||||
|
||||
return md5($id . $uniqueString . serialize($options));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance as an array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'rowId' => $this->rowId,
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'qty' => $this->qty,
|
||||
'price' => $this->price,
|
||||
'options' => $this->options->toArray(),
|
||||
'tax' => $this->tax,
|
||||
'isSaved' => $this->isSaved,
|
||||
'subtotal' => $this->subtotal
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the object to its JSON representation.
|
||||
*
|
||||
* @param int $options
|
||||
* @return string
|
||||
*/
|
||||
public function toJson($options = 0)
|
||||
{
|
||||
return json_encode($this->toArray(), $options);
|
||||
}
|
||||
}
|
||||
19
packages/digital-bird/shoppingcart/src/CartItemOptions.php
Normal file
19
packages/digital-bird/shoppingcart/src/CartItemOptions.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class CartItemOptions extends Collection
|
||||
{
|
||||
/**
|
||||
* Get the option by the given key.
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($key)
|
||||
{
|
||||
return $this->get($key);
|
||||
}
|
||||
}
|
||||
27
packages/digital-bird/shoppingcart/src/Contracts/Buyable.php
Normal file
27
packages/digital-bird/shoppingcart/src/Contracts/Buyable.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart\Contracts;
|
||||
|
||||
interface Buyable
|
||||
{
|
||||
/**
|
||||
* Get the identifier of the Buyable item.
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
public function getBuyableIdentifier($options = null);
|
||||
|
||||
/**
|
||||
* Get the description or title of the Buyable item.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBuyableDescription($options = null);
|
||||
|
||||
/**
|
||||
* Get the price of the Buyable item.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getBuyablePrice($options = null);
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart\Exceptions;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class CartAlreadyStoredException extends RuntimeException {}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart\Exceptions;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class InvalidRowIDException extends RuntimeException {}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart\Exceptions;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class UnknownModelException extends RuntimeException {}
|
||||
16
packages/digital-bird/shoppingcart/src/Facades/Cart.php
Normal file
16
packages/digital-bird/shoppingcart/src/Facades/Cart.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
namespace Gloudemans\Shoppingcart\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
class Cart extends Facade {
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return 'cart';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart;
|
||||
|
||||
use Illuminate\Auth\Events\Logout;
|
||||
use Illuminate\Session\SessionManager;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class ShoppingcartServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->bind('cart', 'Gloudemans\Shoppingcart\Cart');
|
||||
|
||||
$config = __DIR__ . '/../config/cart.php';
|
||||
$this->mergeConfigFrom($config, 'cart');
|
||||
|
||||
$this->publishes([__DIR__ . '/../config/cart.php' => config_path('cart.php')], 'config');
|
||||
|
||||
$this->app['events']->listen(Logout::class, function () {
|
||||
if ($this->app['config']->get('cart.destroy_on_logout')) {
|
||||
$this->app->make(SessionManager::class)->forget('cart');
|
||||
}
|
||||
});
|
||||
|
||||
if ( ! class_exists('CreateShoppingcartTable')) {
|
||||
// Publish the migration
|
||||
$timestamp = date('Y_m_d_His', time());
|
||||
|
||||
$this->publishes([
|
||||
__DIR__.'/../database/migrations/0000_00_00_000000_create_shoppingcart_table.php' => database_path('migrations/'.$timestamp.'_create_shoppingcart_table.php'),
|
||||
], 'migrations');
|
||||
}
|
||||
}
|
||||
}
|
||||
35
packages/digital-bird/shoppingcart/src/Traits/CartHelper.php
Normal file
35
packages/digital-bird/shoppingcart/src/Traits/CartHelper.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart\Traits;
|
||||
|
||||
/**
|
||||
* trait CartHelper
|
||||
*/
|
||||
trait CartHelper
|
||||
{
|
||||
/**
|
||||
* Get the Formated number
|
||||
*
|
||||
* @param $value
|
||||
* @param $decimals
|
||||
* @param $decimalPoint
|
||||
* @param $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
private function numberFormat($value, $decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
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_seperator')) ? ',' : config('cart.format.thousand_seperator');
|
||||
}
|
||||
|
||||
return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue