Skip to content

Commit 2ab3a5a

Browse files
authored
Fix cart condition session (#109)
* feat(cart): enhance session key handling and item conversion logic * feat: update method signatures to include type hints for better clarity
1 parent 8ccdb23 commit 2ab3a5a

File tree

7 files changed

+16
-12
lines changed

7 files changed

+16
-12
lines changed

src/CartCondition.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ public function __construct(
6262
*/
6363
protected $config = [])
6464
{
65-
$this->setSessionKey(sprintf('%s.conditions.%s', array_get($this->config, 'cartInstance', 'default'), $this->name));
65+
$this->setSessionKey(sprintf('cart-conditions.%s.%s',
66+
array_get($this->config, 'cartInstance', 'default'),
67+
array_get($this->config, 'name', $this->name),
68+
));
6669
$this->fillFromConfig($this->config);
6770
}
6871

@@ -72,6 +75,7 @@ public function fillFromConfig($config): void
7275
$this->name = array_get($config, 'name', $this->name);
7376
$this->priority = array_get($config, 'priority', $this->priority);
7477
$this->removeable = array_get($config, 'removeable', $this->removeable);
78+
$this->sessionKey = array_get($config, 'sessionKey', $this->sessionKey);
7579

7680
if ($metaData = array_get($config, 'metaData')) {
7781
Session::put($this->getSessionKey(), $metaData);
@@ -287,6 +291,8 @@ public function toArray()
287291
'label' => $this->label,
288292
'priority' => $this->priority,
289293
'removeable' => $this->removeable,
294+
'sessionKey' => $this->sessionKey,
295+
'config' => $this->config,
290296
'metaData' => Session::get($this->getSessionKey(), []),
291297
];
292298
}

src/CartItem.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,8 @@ public function associate(mixed $model): static
227227

228228
/**
229229
* Get an attribute from the cart item or get the associated model.
230-
*
231-
* @param string $attribute
232-
*
233-
* @return mixed
234230
*/
235-
public function __get($attribute)
231+
public function __get(string $attribute): mixed
236232
{
237233
if ($attribute === 'subtotal') {
238234
return $this->subtotal();

src/Classes/CartConditionManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CartConditionManager
2424
*/
2525
protected array $registeredCallbacks = [];
2626

27-
public function makeCondition($className, array $config = [])
27+
public function makeCondition(string $className, array $config = [])
2828
{
2929
if (!array_key_exists($className, $this->registeredConditions ?? [])) {
3030
throw new LogicException(sprintf("The Cart Condition class '%s' has not been registered", $className));
@@ -74,7 +74,7 @@ public function registerConditions(array $conditions): void
7474
}
7575
}
7676

77-
public function registerCondition($className, $conditionInfo = null): void
77+
public function registerCondition(string $className, $conditionInfo = null): void
7878
{
7979
if ($this->registeredConditions === null) {
8080
$this->registeredConditions = [];

src/Classes/CartManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ protected function loadCartConditions()
215215

216216
$definition['cartInstance'] = $this->cart->currentInstance();
217217

218-
$className = array_get($definition, 'className');
218+
$className = array_get($definition, 'className', '');
219219
$condition = $conditionManager->makeCondition($className, $definition);
220220

221221
$this->cart->loadCondition($condition);

src/Concerns/ActsAsItemable.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ trait ActsAsItemable
1515
*/
1616
public function toItem(): static
1717
{
18-
return new static($this->toArray());
18+
return new static(array_merge($this->toArray(), [
19+
'cartInstance' => array_get($this->config, 'cartInstance', 'default'),
20+
]));
1921
}
2022

2123
public static function isApplicableTo($cartItem) {}

src/Models/CartSettings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CartSettings extends Model
2323
// Reference to field configuration
2424
public string $settingsFieldsConfig = 'cartsettings';
2525

26-
public function getConditionsAttribute($value)
26+
public function getConditionsAttribute(?array $value)
2727
{
2828
$result = [];
2929
$registeredConditions = resolve(CartConditionManager::class)->listRegisteredConditions();

src/Models/Category.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Category extends Model
7676

7777
public $relation = [
7878
'belongsTo' => [
79-
'parent_cat' => [\Igniter\Cart\Models\Category::class, 'foreignKey' => 'parent_id', 'otherKey' => 'category_id'],
79+
'parent_cat' => [Category::class, 'foreignKey' => 'parent_id', 'otherKey' => 'category_id'],
8080
],
8181
'belongsToMany' => [
8282
'menus' => [Menu::class, 'table' => 'menu_categories'],

0 commit comments

Comments
 (0)