Skip to content

Commit

Permalink
fix(checkout): consistently use correct package type for delivery opt…
Browse files Browse the repository at this point in the history
…ions (#782)
  • Loading branch information
joerivanveen authored Nov 10, 2023
1 parent 6bef66b commit 85aa4c3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
27 changes: 16 additions & 11 deletions Model/Quote/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ public function getDeliveryOptions(array $forAddress = []): array
$this->hideDeliveryOptionsForProduct();

$data = [
'methods' => [$this->helper->getParentMethodNameFromQuote($this->quoteId, $forAddress)],
'methods' => explode(',', $this->helper->getGeneralConfig('shipping_methods/methods') ?? ''),
'config' => array_merge(
$this->getGeneralData(),
$this->getPackageType(),
$this->getDeliveryData()
$this->getDeliveryData(),
['packageType' => $this->getPackageType()]
),
'strings' => $this->getDeliveryOptionsStrings(),
'forAddress' => $forAddress,
Expand Down Expand Up @@ -119,17 +119,22 @@ private function getGeneralData()
/**
* Get general data
*
* @return array
* @return string
*/
private function getPackageType(): array
private function getPackageType(): string
{
$packageType = AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME;
$activeCarriers = $this->getActiveCarriers();

$packageType = [];
foreach ($activeCarriers as $carrier) {
$packageType = [
'packageType' => $this->checkPackageType($carrier, null),
];
$tentativePackageType = $this->checkPackageType($carrier);

switch ($tentativePackageType) {
case AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME:
return AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME;
case AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME:
$packageType = AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME;
}
}

return $packageType;
Expand Down Expand Up @@ -288,7 +293,7 @@ private function getDeliveryOptionsStrings(): array
*
* @return string
*/
public function checkPackageType(string $carrier, ?string $country): string
public function checkPackageType(string $carrier, ?string $country = null): string
{
try {
$consignment = ConsignmentFactory::createByCarrierName($carrier);
Expand Down Expand Up @@ -359,7 +364,7 @@ public function hideDeliveryOptionsForProduct()
*/
private function isPickupAllowed(string $carrier): bool
{
$isMailboxPackage = self::PACKAGE_TYPE_MAILBOX === $this->getPackageType()['packageType'];
$isMailboxPackage = self::PACKAGE_TYPE_MAILBOX === $this->getPackageType();
$pickupEnabled = $this->helper->getBoolConfig($carrier, 'pickup/active');
$showPickupForMailbox = $this->helper->getBoolConfig($carrier, 'mailbox/pickup_mailbox');
$showPickup = ! $isMailboxPackage || $showPickupForMailbox;
Expand Down
16 changes: 14 additions & 2 deletions view/frontend/web/js/model/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,17 @@ function(
Model.countryId(quote.shippingAddress().countryId);
doRequest(Model.getDeliveryOptionsConfig, {onSuccess: Model.onInitializeSuccess});

quote.billingAddress.subscribe(function() {
function reloadConfig() {
var shippingAddress = quote.shippingAddress();

if (shippingAddress.countryId !== Model.countryId()) {
doRequest(Model.getDeliveryOptionsConfig, {onSuccess: Model.onReFetchDeliveryOptionsConfig});
}

Model.countryId(shippingAddress.countryId);
});
}
quote.billingAddress.subscribe(reloadConfig);
quote.shippingAddress.subscribe(reloadConfig);
},

onReFetchDeliveryOptionsConfig: function(response) {
Expand Down Expand Up @@ -181,6 +183,16 @@ function(
});
}

if ('undefined' !== typeof MyParcelConfig && MyParcelConfig.hasOwnProperty('methods')) {
MyParcelConfig.methods.forEach(function(code) {
try {
document.getElementById('label_method_' + code + '_' + code).remove();
} catch (e) {
// when the element is not there as such, it is already ok
}
});
}

rowsToHide.forEach(function(row) {
row.style.display = 'none';
});
Expand Down
Loading

0 comments on commit 85aa4c3

Please sign in to comment.