Skip to content

Commit 027ed53

Browse files
committed
refactor(cart): set cart condition session key based on cart instance name to store cart conditions per location
Signed-off-by: Sam Poyigi <[email protected]>
1 parent 62c5cea commit 027ed53

File tree

7 files changed

+41
-21
lines changed

7 files changed

+41
-21
lines changed

src/CartCondition.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ abstract class CartCondition implements Arrayable, Jsonable
4343
// Object properties
4444
//
4545

46-
protected $sessionKey = 'cart.conditions.%s';
46+
protected ?string $sessionKey = null;
4747

4848
protected null|CartContent|CartItem $target = null;
4949

@@ -56,11 +56,13 @@ abstract class CartCondition implements Arrayable, Jsonable
5656
*
5757
* @param array $config
5858
*/
59-
public function __construct(/**
60-
* The config for this cart condition.
61-
*/
59+
public function __construct(
60+
/**
61+
* The config for this cart condition.
62+
*/
6263
protected $config = [])
6364
{
65+
$this->setSessionKey(sprintf('%s.conditions.%s', array_get($this->config, 'cartInstance', 'default'), $this->name));
6466
$this->fillFromConfig($this->config);
6567
}
6668

src/Classes/CheckoutForm.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function validationRules(): array
2929
{
3030
$rules = $this->getConfig('rules', []);
3131

32-
$prefixedKeys = array_map(fn($key) => is_string($key) ? 'fields.'.$key : $key, array_keys($rules));
32+
$prefixedKeys = array_map(fn(int|string $key) => is_string($key) ? 'fields.'.$key : $key, array_keys($rules));
3333

3434
return array_combine($prefixedKeys, $rules);
3535
}
@@ -38,16 +38,16 @@ public function validationMessages(): array
3838
{
3939
$messages = $this->getConfig('messages', []);
4040

41-
$prefixedKeys = array_map(fn($key) => is_string($key) ? 'fields.'.$key : $key, array_keys($messages));
41+
$prefixedKeys = array_map(fn(int|string $key) => is_string($key) ? 'fields.'.$key : $key, array_keys($messages));
4242

4343
return array_combine($prefixedKeys, $messages);
4444
}
4545

4646
public function validationAttributes(): array
4747
{
48-
$attributes = array_map(fn($field): string => lang($field['label'] ?? $field['name']), $this->fields);
48+
$attributes = array_map(fn(array $field): string => lang($field['label'] ?? $field['name']), $this->fields);
4949

50-
$prefixedKeys = array_map(fn($key) => is_string($key) ? 'fields.'.$key : $key, array_keys($attributes));
50+
$prefixedKeys = array_map(fn(int|string $key) => is_string($key) ? 'fields.'.$key : $key, array_keys($attributes));
5151

5252
return array_combine($prefixedKeys, $attributes);
5353
}

src/Classes/OrderManager.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Igniter\Cart\Models\Order;
1010
use Igniter\Flame\Exception\ApplicationException;
1111
use Igniter\Flame\Geolite\Facades\Geocoder;
12+
use Igniter\Flame\Geolite\Model\Location as UserLocation;
1213
use Igniter\Local\Classes\CoveredArea;
1314
use Igniter\Local\Classes\Location;
1415
use Igniter\Local\Models\LocationArea;
@@ -151,14 +152,17 @@ public function validateDeliveryAddress(array $address): void
151152
$address['country'] = app('country')->getCountryNameById($address['country_id']);
152153
}
153154

154-
$collection = Geocoder::geocode(implode(' ', array_only($address, [
155-
'address_1', 'address_2', 'city', 'state', 'postcode', 'country',
156-
])));
155+
$addressInfo = array_only($address, ['address_1', 'address_2', 'city', 'state', 'postcode', 'country']);
156+
if (empty($addressInfo)) {
157+
throw new ApplicationException(lang('igniter.local::default.alert_invalid_search_query'));
158+
}
157159

160+
$collection = Geocoder::geocode(implode(' ', $addressInfo));
158161
if ($collection->isEmpty()) {
159162
throw new ApplicationException(lang('igniter.local::default.alert_invalid_search_query'));
160163
}
161164

165+
/** @var UserLocation $userLocation */
162166
$userLocation = $collection->first();
163167
if (!$userLocation->getStreetNumber() || !$userLocation->getStreetName()) {
164168
throw new ApplicationException(lang('igniter.local::default.alert_missing_street_address'));
@@ -428,9 +432,10 @@ public function applyCurrentPaymentFee($code): ?CartCondition
428432
$this->setCurrentPaymentCode($code);
429433

430434
$condition = $this->cart->getCondition('paymentFee');
431-
$condition->setMetaData(['code' => $code]);
432-
433-
$this->cart->loadCondition($condition);
435+
if ($condition instanceof CartCondition) {
436+
$condition->setMetaData(['code' => $code]);
437+
$this->cart->loadCondition($condition);
438+
}
434439

435440
return $condition;
436441
}

src/Concerns/CartConditionHelper.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,15 @@ protected function valueIsToBeDivided(string $value): bool
210210
// Session
211211
//
212212

213-
protected function getSessionKey(): string
213+
public function getSessionKey(): string
214214
{
215-
return sprintf($this->sessionKey, $this->name);
215+
return $this->sessionKey;
216+
}
217+
218+
public function setSessionKey(string $sessionKey): static
219+
{
220+
$this->sessionKey = $sessionKey;
221+
222+
return $this;
216223
}
217224
}

src/Extension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ public function registerSettings(): array
227227
{
228228
return [
229229
'settings' => [
230+
'priority' => 1,
230231
'label' => 'Cart Settings',
231232
'description' => 'Manage cart conditions and tipping settings.',
232233
'icon' => 'fa fa-cart-shopping',

src/Http/Middleware/InjectStatusWorkflow.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,14 @@ public function handle($request, Closure $next)
2222
}
2323

2424
if ($response->isSuccessful()) {
25-
$orderWorkflowModalView = $this->renderOrderWorkflowView();
26-
2725
$content = $response->getContent();
2826
$insertPos = strripos((string)$content, '</body>');
2927
if ($insertPos !== false) {
3028
$content = substr((string)$content, 0, $insertPos);
31-
$content .= $orderWorkflowModalView;
29+
$content .= $this->renderOrderWorkflowView();
3230
$content .= substr($content, $insertPos);
31+
$response->setContent($content);
3332
}
34-
35-
$response->setContent($content);
3633
}
3734

3835
return $response;

tests/Classes/OrderManagerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@
120120
->toThrow(ApplicationException::class, lang('igniter.cart::default.checkout.alert_customer_not_logged'));
121121
});
122122

123+
it('validateDeliveryAddress throws exception when delivery address is empty', function(): void {
124+
$address = [];
125+
Geocoder::shouldReceive('geocode')->andReturn(collect());
126+
127+
expect(fn() => $this->manager->validateDeliveryAddress($address))
128+
->toThrow(ApplicationException::class, lang('igniter.local::default.alert_invalid_search_query'));
129+
});
130+
123131
it('validateDeliveryAddress throws exception when delivery address is invalid', function(): void {
124132
$address = [
125133
'address_1' => '123 Main St',

0 commit comments

Comments
 (0)