Skip to content

Commit

Permalink
Merge pull request #27 from offline-agency/fix-options-cart
Browse files Browse the repository at this point in the history
Fix Cart getOptions
  • Loading branch information
Giacomo92 authored Apr 15, 2022
2 parents 9501deb + c7186e4 commit 6541084
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"illuminate/events": "^5.0|^5.8|^6.0|^7.0|^8.0|^9.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0|^7.0|^8.0",
"orchestra/testbench": "^3.1|^4.0",
"phpunit/phpunit": "^6.0|^7.0|^8.0|^9.0",
"orchestra/testbench": "^3.1|^4.0|^5.0|^6.0|^7.0",
"mockery/mockery": "^1.0",
"doctrine/dbal": "^2.5",
"php-coveralls/php-coveralls": "^2.4"
Expand Down
50 changes: 45 additions & 5 deletions src/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OfflineAgency\LaravelCart;

use ArrayAccess;
use Carbon\Carbon;
use Closure;
use Illuminate\Contracts\Events\Dispatcher;
Expand All @@ -18,11 +19,12 @@
class Cart
{
const DEFAULT_INSTANCE = 'default';
const CART_OPTIONS_KEY = 'options';

/**
* @var array
* @var
*/
private $options;
private $options = [];

/**
* Instance of the session manager.
Expand Down Expand Up @@ -477,6 +479,18 @@ protected function getContent(): Collection
: new Collection();
}

/**
* Get the cart generic info, if there is no cart set yet, return a new empty Collection.
*
* @return Collection
*/
protected function getCartInfo(): Collection
{
return $this->session->has($this->getCartInstance())
? $this->session->get($this->getCartInstance())
: new Collection();
}

/**
* * Create a new CartItem from the supplied attributes.
*
Expand Down Expand Up @@ -765,7 +779,7 @@ public function hasGlobalCoupon()

/**
* @param string $couponCode
* @return array|\ArrayAccess|mixed|null
* @return array|ArrayAccess|mixed|null
*/
public function getCoupon(
string $couponCode
Expand Down Expand Up @@ -844,14 +858,40 @@ private function applyGlobalCoupon(
*/
public function getOptions(): array
{
return $this->options;
$cart_info = $this->getCartInfo();

return Arr::has($cart_info, self::CART_OPTIONS_KEY) ? Arr::get($cart_info, self::CART_OPTIONS_KEY) : [];
}

/**
* @param array $options
*/
public function setOptions(array $options): void
{
$this->options = $options;
$content = $this->getCartInfo();

$content->put(self::CART_OPTIONS_KEY, $options);

$this->session->put($this->getCartInstance(), $content);
}

/**
* @return string
*/
private function getCartInstance(): string
{
return $this->instance.'_cart_info';
}

/**
* @param $key
* @param $default_value
* @return array|ArrayAccess|mixed|null
*/
public function getOptionsByKey($key, $default_value = null)
{
$options = $this->getOptions();

return Arr::has($options, $key) ? Arr::get($options, $key) : $default_value;
}
}
10 changes: 10 additions & 0 deletions tests/CartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1823,7 +1823,17 @@ public function it_can_set_and_get_options_on_cart()
{
$cart = $this->getCart();
$cart->setOptions(['test' => 'test']);

$this->assertEquals(['test' => 'test'], $cart->getOptions());

$cart->getOptionsByKey('test');
$this->assertEquals('test', $cart->getOptionsByKey('test'));

$cart->getOptionsByKey('option_not_existing_with_default_value', false);
$this->assertEquals(false, $cart->getOptionsByKey('option_not_existing_with_default_value'));

$cart->getOptionsByKey('option_not_existing_without_default_value');
$this->assertNull($cart->getOptionsByKey('option_not_existing_without_default_value'));
}

/**
Expand Down

0 comments on commit 6541084

Please sign in to comment.