mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +00:00
init commit :rocket
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
namespace Wearepixel\Cart;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Wearepixel\Cart\Helpers\Helpers;
|
||||
|
||||
class ItemCollection extends Collection
|
||||
{
|
||||
/**
|
||||
* Sets the config parameters.
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* ItemCollection constructor.
|
||||
*
|
||||
* @param array|mixed $items
|
||||
*/
|
||||
public function __construct($items, $config = [])
|
||||
{
|
||||
parent::__construct($items);
|
||||
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the sum of price
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getPriceSum()
|
||||
{
|
||||
return Helpers::formatValue($this->price * $this->quantity, $this->config['format_numbers'], $this->config);
|
||||
}
|
||||
|
||||
public function __get($name)
|
||||
{
|
||||
if ($this->has($name) || $name == 'model') {
|
||||
return ! is_null($this->get($name)) ? $this->get($name) : $this->getAssociatedModel();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* return the associated model of an item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function getAssociatedModel()
|
||||
{
|
||||
if (! $this->has('associatedModel')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$associatedModel = $this->get('associatedModel');
|
||||
|
||||
return with(new $associatedModel)->find($this->get('id'));
|
||||
}
|
||||
|
||||
/**
|
||||
* check if item has conditions
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasConditions()
|
||||
{
|
||||
if (! isset($this['conditions'])) {
|
||||
return false;
|
||||
}
|
||||
if (is_array($this['conditions'])) {
|
||||
return count($this['conditions']) > 0;
|
||||
}
|
||||
$conditionInstance = 'Wearepixel\\Cart\\CartCondition';
|
||||
if ($this['conditions'] instanceof $conditionInstance) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if item has conditions
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getConditions()
|
||||
{
|
||||
if (! $this->hasConditions()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this['conditions'];
|
||||
}
|
||||
|
||||
/**
|
||||
* get the single price in which conditions are already applied
|
||||
*
|
||||
* @param bool $formatted
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getPriceWithConditions($formatted = true)
|
||||
{
|
||||
$originalPrice = $this->price;
|
||||
|
||||
$newPrice = 0.00;
|
||||
$processed = 0;
|
||||
|
||||
if ($this->hasConditions()) {
|
||||
if (is_array($this->conditions)) {
|
||||
foreach ($this->conditions as $condition) {
|
||||
($processed > 0) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice;
|
||||
$newPrice = $condition->applyCondition($toBeCalculated);
|
||||
$processed++;
|
||||
}
|
||||
} else {
|
||||
$newPrice = $this['conditions']->applyCondition($originalPrice);
|
||||
}
|
||||
|
||||
return Helpers::formatValue($newPrice, $formatted, $this->config);
|
||||
}
|
||||
|
||||
return Helpers::formatValue($originalPrice, $formatted, $this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the sum of price in which conditions are already applied
|
||||
*
|
||||
* @param bool $formatted
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getPriceSumWithConditions($formatted = true)
|
||||
{
|
||||
return Helpers::formatValue($this->getPriceWithConditions(false) * $this->quantity, $formatted, $this->config);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user