mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 22:01:48 +00:00
quantities now support decimals (so adding 1.1, or setting it to 1.1)
This commit is contained in:
+5
-13
@@ -226,18 +226,10 @@ public function update($id, $data)
|
||||
$item = $cart->get($id);
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
// if the key is currently "quantity" we will need to check if an arithmetic
|
||||
// symbol is present so we can decide if the update of quantity is being added
|
||||
// or being reduced.
|
||||
if ($key == 'quantity') {
|
||||
// we will check if quantity value provided is array,
|
||||
// if it is, we will need to check if a key "relative" is set
|
||||
// and we will evaluate its value if true or false,
|
||||
// this tells us how to treat the quantity value if it should be updated
|
||||
// relatively to its current quantity value or just totally replace the value
|
||||
if (is_array($value)) {
|
||||
if (isset($value['relative'])) {
|
||||
if ((bool) $value['relative']) {
|
||||
if (isset($value['relative']) && $value['relative'] === true) {
|
||||
$item = $this->updateQuantityRelative($item, $key, $value['value']);
|
||||
} else {
|
||||
$item = $this->updateQuantityNotRelative($item, $key, $value['value']);
|
||||
@@ -791,7 +783,7 @@ protected function itemHasConditions($item)
|
||||
protected function updateQuantityRelative($item, $key, $value)
|
||||
{
|
||||
if (preg_match('/\-/', $value) == 1) {
|
||||
$value = (int) str_replace('-', '', $value);
|
||||
$value = (float) str_replace('-', '', $value);
|
||||
|
||||
// we will not allowed to reduced quantity to 0, so if the given value
|
||||
// would result to item quantity of 0, we will not do it.
|
||||
@@ -799,9 +791,9 @@ protected function updateQuantityRelative($item, $key, $value)
|
||||
$item[$key] -= $value;
|
||||
}
|
||||
} elseif (preg_match('/\+/', $value) == 1) {
|
||||
$item[$key] += (int) str_replace('+', '', $value);
|
||||
$item[$key] += (float) str_replace('+', '', $value);
|
||||
} else {
|
||||
$item[$key] += (int) $value;
|
||||
$item[$key] += (float) $value;
|
||||
}
|
||||
|
||||
return $item;
|
||||
@@ -814,7 +806,7 @@ protected function updateQuantityRelative($item, $key, $value)
|
||||
*/
|
||||
protected function updateQuantityNotRelative($item, $key, $value)
|
||||
{
|
||||
$item[$key] = (int) $value;
|
||||
$item[$key] = (float) $value;
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user