Skip to content

Commit

Permalink
feat(settings): allow showing description with shipping method (#285)
Browse files Browse the repository at this point in the history
- Also fixed disabled shipping methods still being shown
  • Loading branch information
EdieLemoine authored Jul 4, 2024
1 parent 0979cb4 commit 2a80eca
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 24 deletions.
3 changes: 3 additions & 0 deletions src/App/ShippingMethod/Model/PdkShippingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/**
* @property string $id
* @property string $name
* @property string $description
* @property bool $isEnabled
* @property bool $hasDeliveryOptions
* @property int $minimumDropOffDelay
Expand All @@ -22,6 +23,7 @@ class PdkShippingMethod extends Model
protected $attributes = [
'id' => null,
'name' => null,
'description' => null,
'allowedPackageTypes' => PackageTypeCollection::class,
'hasDeliveryOptions' => true,
'isEnabled' => true,
Expand All @@ -32,6 +34,7 @@ class PdkShippingMethod extends Model
protected $casts = [
'id' => 'string',
'name' => 'string',
'description' => 'string',
'allowedPackageTypes' => PackageTypeCollection::class,
'hasDeliveryOptions' => 'bool',
'isEnabled' => 'bool',
Expand Down
27 changes: 17 additions & 10 deletions src/Frontend/View/AbstractSettingsView.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use MyParcelNL\Pdk\Settings\Model\Settings;
use MyParcelNL\Pdk\Shipment\Model\DeliveryOptions;
use MyParcelNL\Sdk\src\Support\Str;
use function array_map;

/**
* @deprecated use NewAbstractSettingsView instead
Expand Down Expand Up @@ -258,16 +259,22 @@ protected function isNotDisabled(string $name): bool
*/
protected function toSelectOptions(array $array, int $displayOptions = 0): array
{
$associativeArray = Arr::isAssoc($array) ? $array : array_combine($array, $array);

$options = array_map(static function ($value, $key) use ($displayOptions) {
$labelKey = $displayOptions & self::SELECT_USE_PLAIN_LABEL ? 'plainLabel' : 'label';

return [
'value' => $key,
$labelKey => $value,
];
}, $associativeArray, array_keys($associativeArray));
$isAssoc = Arr::isAssoc($array);

if ($isAssoc) {
$options = array_map(static function ($value, $key) use ($displayOptions) {
$labelKey = $displayOptions & self::SELECT_USE_PLAIN_LABEL ? 'plainLabel' : 'label';

return [
'value' => $key,
$labelKey => $value,
];
}, $array, array_keys($array));
} else {
$options = array_map(static function (array $selectOption) {
return array_filter($selectOption);
}, $array);
}

return $this->addDefaultOption($options, $displayOptions);
}
Expand Down
26 changes: 15 additions & 11 deletions src/Frontend/View/CheckoutSettingsView.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,22 @@ protected function getSettingsId(): string
*/
private function getShippingMethodOptions(): array
{
$shippingMethods = $this->shippingMethodRepository->all();
$allShippingMethods = $this->shippingMethodRepository->all();

return $this->toSelectOptions(
array_reduce(
$shippingMethods->all(),
static function (array $cur, PdkShippingMethod $shippingMethod) {
$cur[$shippingMethod->id] = "$shippingMethod->name ($shippingMethod->id)";
$array = $allShippingMethods
->filter(static function (PdkShippingMethod $shippingMethod) {
return $shippingMethod->isEnabled;
})
->reduce(static function (array $cur, PdkShippingMethod $shippingMethod) {
$cur[] = [
'value' => $shippingMethod->id,
'plainLabel' => $shippingMethod->name,
'description' => $shippingMethod->description,
];

return $cur;
}, []
),
AbstractSettingsView::SELECT_USE_PLAIN_LABEL
);
return $cur;
}, []);

return $this->toSelectOptions($array, AbstractSettingsView::SELECT_USE_PLAIN_LABEL);
}
}
21 changes: 20 additions & 1 deletion tests/Bootstrap/MockPdkShippingMethodRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,30 @@

use MyParcelNL\Pdk\App\ShippingMethod\Collection\PdkShippingMethodCollection;
use MyParcelNL\Pdk\App\ShippingMethod\Contract\PdkShippingMethodRepositoryInterface;
use MyParcelNL\Pdk\App\ShippingMethod\Model\PdkShippingMethod;
use function MyParcelNL\Pdk\Tests\factory;

class MockPdkShippingMethodRepository implements PdkShippingMethodRepositoryInterface
{
public function all(): PdkShippingMethodCollection
{
return new PdkShippingMethodCollection();
return new PdkShippingMethodCollection([
factory(PdkShippingMethod::class)
->withId('shipping:1')
->withName('Shipping 1')
->withIsEnabled(true)
->make(),
factory(PdkShippingMethod::class)
->withId('shipping:2')
->withName('Shipping 2')
->withIsEnabled(false)
->make(),
factory(PdkShippingMethod::class)
->withId('shipping:3')
->withName('Shipping 3')
->withDescription('My description')
->withIsEnabled(true)
->make(),
]);
}
}
3 changes: 3 additions & 0 deletions tests/Unit/App/Cart/Service/CartCalculationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
'result' => [
'id' => null,
'name' => null,
'description' => null,
'isEnabled' => true,
'allowedPackageTypes' => [
[
Expand Down Expand Up @@ -246,6 +247,7 @@
'result' => [
'id' => null,
'name' => null,
'description' => null,
'isEnabled' => true,
'hasDeliveryOptions' => true,
'minimumDropOffDelay' => 2,
Expand Down Expand Up @@ -274,6 +276,7 @@
'result' => [
'id' => null,
'name' => null,
'description' => null,
'isEnabled' => true,
'hasDeliveryOptions' => false,
'minimumDropOffDelay' => 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,17 @@
}
],
"$component": "ShippingMethodsInput",
"options": [],
"options": [
{
"value": "shipping:1",
"plainLabel": "Shipping 1"
},
{
"value": "shipping:3",
"plainLabel": "Shipping 3",
"description": "My description"
}
],
"label": "settings_checkout_allowed_shipping_methods",
"description": "settings_checkout_allowed_shipping_methods_description"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,17 @@
}
],
"$component": "ShippingMethodsInput",
"options": [],
"options": [
{
"value": "shipping:1",
"plainLabel": "Shipping 1"
},
{
"value": "shipping:3",
"plainLabel": "Shipping 3",
"description": "My description"
}
],
"label": "settings_checkout_allowed_shipping_methods",
"description": "settings_checkout_allowed_shipping_methods_description"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* @method $this withIsEnabled(bool $isEnabled)
* @method $this withMinimumDropOffDelay(int $minimumDropOffDelay)
* @method $this withName(string $name)
* @method $this withDescription(string $description)
* @method $this withShippingAddress(Address|AddressFactory $shippingAddress)
*/
final class PdkShippingMethodFactory extends AbstractModelFactory
Expand Down

0 comments on commit 2a80eca

Please sign in to comment.