Skip to content

Commit

Permalink
fix: only get available options for carriers (#703)
Browse files Browse the repository at this point in the history
Resolves: #701
  • Loading branch information
joerivanveen authored Jul 7, 2022
1 parent ae83b0a commit f6317f7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 38 deletions.
10 changes: 10 additions & 0 deletions Helper/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ public function getBasePrice()
return $this->base_price;
}

/**
* @param string $message
*
* @return void
*/
public function log(string $message): void
{
$this->_logger->critical($message);
}

/**
* @param float $base_price
*/
Expand Down
104 changes: 66 additions & 38 deletions Model/Quote/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Magento\Store\Model\StoreManagerInterface;
use MyParcelNL\Magento\Helper\Data;
use MyParcelNL\Magento\Model\Sales\Repository\PackageRepository;
use MyParcelNL\Sdk\src\Factory\ConsignmentFactory;
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;

class Checkout
{
Expand Down Expand Up @@ -38,11 +40,6 @@ class Checkout
*/
private $currency;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $carrier;

/**
* Checkout constructor.
*
Expand Down Expand Up @@ -131,7 +128,7 @@ private function getPackageType(): array
$packageType = [];
foreach ($activeCarriers as $carrier) {
$packageType = [
'packageType' => $this->checkPackageType($carrier, null),
'packageType' => $this->checkPackageType($carrier, null),
];
}

Expand All @@ -147,34 +144,55 @@ private function getDeliveryData(): array
{
$myParcelConfig = [];
$activeCarriers = $this->getActiveCarriers();
$carrierPath = Data::CARRIERS_XML_PATH_MAP;
$carrierPaths = Data::CARRIERS_XML_PATH_MAP;

foreach ($activeCarriers as $carrier) {
$carrierPath = $carrierPaths[$carrier];

try {
$consignment = ConsignmentFactory::createByCarrierName($carrier);
$consignment->setPackageType(AbstractConsignment::PACKAGE_TYPE_PACKAGE);
} catch (\Throwable $ex) {
$this->helper->log(sprintf('getDeliveryData: Could not create default consignment for %s', $carrier));
continue;
}

$canHaveDigitalStamp = $consignment->canHaveDeliveryType(AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME);
$canHaveMailbox = $consignment->canHaveDeliveryType(AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME);
$canHaveSameDay = $consignment->canHaveExtraOption(AbstractConsignment::SHIPMENT_OPTION_SAME_DAY_DELIVERY);
$canHaveMonday = $consignment->canHaveExtraOption(AbstractConsignment::EXTRA_OPTION_DELIVERY_MONDAY);
$canHaveMorning = $consignment->canHaveDeliveryType(AbstractConsignment::DELIVERY_TYPE_MORNING_NAME);
$canHaveEvening = $consignment->canHaveDeliveryType(AbstractConsignment::DELIVERY_TYPE_EVENING_NAME);
$canHaveSignature = $consignment->canHaveShipmentOption(AbstractConsignment::SHIPMENT_OPTION_SIGNATURE);
$canHaveOnlyRecipient = $consignment->canHaveShipmentOption(AbstractConsignment::SHIPMENT_OPTION_ONLY_RECIPIENT);
$canHaveAgeCheck = $consignment->canHaveShipmentOption(AbstractConsignment::SHIPMENT_OPTION_AGE_CHECK);
$canHavePickup = $consignment->canHaveDeliveryType(AbstractConsignment::DELIVERY_TYPE_PICKUP_NAME);

$basePrice = $this->helper->getBasePrice();
$morningFee = $this->helper->getMethodPrice($carrierPath[$carrier], 'morning/fee');
$eveningFee = $this->helper->getMethodPrice($carrierPath[$carrier], 'evening/fee');
$sameDayFee = (int) $this->helper->getCarrierConfig('delivery/same_day_delivery_fee', $carrierPath[$carrier]);
$signatureFee = $this->helper->getMethodPrice($carrierPath[$carrier], 'delivery/signature_fee', false);
$onlyRecipientFee = $this->helper->getMethodPrice($carrierPath[$carrier], 'delivery/only_recipient_fee', false);
$isAgeCheckActive = $this->isAgeCheckActive($carrierPath[$carrier]);
$morningFee = $canHaveMorning ? $this->helper->getMethodPrice($carrierPath, 'morning/fee') : 0;
$eveningFee = $canHaveEvening ? $this->helper->getMethodPrice($carrierPath, 'evening/fee') : 0;
$sameDayFee = $canHaveSameDay ? (int) $this->helper->getCarrierConfig('delivery/same_day_delivery_fee', $carrierPath) : 0;
$signatureFee = $canHaveSignature ? $this->helper->getMethodPrice($carrierPath, 'delivery/signature_fee', false) : 0;
$onlyRecipientFee = $canHaveOnlyRecipient ? $this->helper->getMethodPrice($carrierPath, 'delivery/only_recipient_fee', false) : 0;
$isAgeCheckActive = $canHaveAgeCheck && $this->isAgeCheckActive($carrierPath);

$myParcelConfig['carrierSettings'][$carrier] = [
'allowDeliveryOptions' => ! $this->package->deliveryOptionsDisabled && $this->helper->getBoolConfig($carrierPath[$carrier], 'delivery/active'),
'allowSignature' => $this->helper->getBoolConfig($carrierPath[$carrier], 'delivery/signature_active'),
'allowOnlyRecipient' => $this->helper->getBoolConfig($carrierPath[$carrier], 'delivery/only_recipient_active'),
'allowMorningDelivery' => ! $isAgeCheckActive && $this->helper->getBoolConfig($carrierPath[$carrier], 'morning/active'),
'allowEveningDelivery' => ! $isAgeCheckActive && $this->helper->getBoolConfig($carrierPath[$carrier], 'evening/active'),
'allowPickupLocations' => $this->isPickupAllowed($carrierPath[$carrier]),
'allowShowDeliveryDate' => $this->helper->getBoolConfig($carrierPath[$carrier], 'general/allow_show_delivery_date'),
'allowMondayDelivery' => $this->helper->getIntegerConfig($carrierPath[$carrier], 'general/monday_delivery_active'),
'allowSameDayDelivery' => $this->helper->getBoolConfig($carrierPath[$carrier], 'delivery/same_day_active'),

'cutoffTime' => $this->helper->getTimeConfig($carrierPath[$carrier], 'general/cutoff_time'),
'cutoffTimeSameDay' => $this->helper->getTimeConfig($carrierPath[$carrier], 'delivery/cutoff_time_same_day'),
'saturdayCutoffTime' => $this->helper->getTimeConfig($carrierPath[$carrier], 'general/saturday_cutoff_time'),
'deliveryDaysWindow' => $this->helper->getIntegerConfig($carrierPath[$carrier], 'general/deliverydays_window'),
'dropOffDays' => $this->helper->getArrayConfig($carrierPath[$carrier], 'general/dropoff_days'),
'dropOffDelay' => $this->getDropOffDelay($carrierPath[$carrier], 'general/dropoff_delay'),
'allowDeliveryOptions' => ! $this->package->deliveryOptionsDisabled && $this->helper->getBoolConfig($carrierPath, 'delivery/active'),
'allowSignature' => $canHaveSignature && $this->helper->getBoolConfig($carrierPath, 'delivery/signature_active'),
'allowOnlyRecipient' => $canHaveOnlyRecipient && $this->helper->getBoolConfig($carrierPath, 'delivery/only_recipient_active'),
'allowMorningDelivery' => ! $isAgeCheckActive && $canHaveMorning && $this->helper->getBoolConfig($carrierPath, 'morning/active'),
'allowEveningDelivery' => ! $isAgeCheckActive && $canHaveEvening && $this->helper->getBoolConfig($carrierPath, 'evening/active'),
'allowPickupLocations' => $canHavePickup && $this->isPickupAllowed($carrierPath),
'allowShowDeliveryDate' => $this->helper->getBoolConfig($carrierPath, 'general/allow_show_delivery_date'),
'allowMondayDelivery' => $canHaveMonday ? $this->helper->getIntegerConfig($carrierPath, 'general/monday_delivery_active') : 0,
'allowSameDayDelivery' => $canHaveSameDay && $this->helper->getBoolConfig($carrierPath, 'delivery/same_day_active'),

'cutoffTime' => $this->helper->getTimeConfig($carrierPath, 'general/cutoff_time'),
'cutoffTimeSameDay' => $canHaveSameDay ? $this->helper->getTimeConfig($carrierPath, 'delivery/cutoff_time_same_day') : 0,
'saturdayCutoffTime' => $canHaveMonday ? $this->helper->getTimeConfig($carrierPath, 'general/saturday_cutoff_time') : 0,
'deliveryDaysWindow' => $this->helper->getIntegerConfig($carrierPath, 'general/deliverydays_window'),
'dropOffDays' => $this->helper->getArrayConfig($carrierPath, 'general/dropoff_days'),
'dropOffDelay' => $this->getDropOffDelay($carrierPath, 'general/dropoff_delay'),

'priceSignature' => $signatureFee,
'priceOnlyRecipient' => $onlyRecipientFee,
Expand All @@ -188,9 +206,9 @@ private function getDeliveryData(): array
'priceEveningSignature' => ($eveningFee + $signatureFee),
'priceSignatureAndOnlyRecipient' => ($basePrice + $signatureFee + $onlyRecipientFee),

'pricePickup' => $this->helper->getMethodPrice($carrierPath[$carrier], 'pickup/fee'),
'pricePackageTypeMailbox' => $this->helper->getMethodPrice($carrierPath[$carrier], 'mailbox/fee', false),
'pricePackageTypeDigitalStamp' => $this->helper->getMethodPrice($carrierPath[$carrier], 'digital_stamp/fee', false),
'pricePickup' => $canHavePickup ? $this->helper->getMethodPrice($carrierPath, 'pickup/fee') : 0,
'pricePackageTypeMailbox' => $canHaveMailbox ? $this->helper->getMethodPrice($carrierPath, 'mailbox/fee', false) : 0,
'pricePackageTypeDigitalStamp' => $canHaveDigitalStamp ? $this->helper->getMethodPrice($carrierPath, 'digital_stamp/fee', false) : 0,
];
}

Expand Down Expand Up @@ -272,16 +290,26 @@ private function getDeliveryOptionsStrings(): array
*/
public function checkPackageType(string $carrier, ?string $country): string
{
$carrierPath = Data::CARRIERS_XML_PATH_MAP;
$products = $this->cart->getAllItems();
$country = $country ?? $this->cart->getShippingAddress()->getCountryId();
try {
$consignment = ConsignmentFactory::createByCarrierName($carrier);
} catch (\Throwable $e) {
$this->helper->log(sprintf('checkPackageType: Could not create default consignment for %s', $carrier));

return AbstractConsignment::DEFAULT_PACKAGE_TYPE_NAME;
}

$carrierPath = Data::CARRIERS_XML_PATH_MAP[$carrier];
$products = $this->cart->getAllItems();
$country = $country ?? $this->cart->getShippingAddress()->getCountryId();
$canHaveDigitalStamp = $consignment->canHavePackageType(AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME);
$canHaveMailbox = $consignment->canHavePackageType(AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME);

$this->package->setCurrentCountry($country);
$this->package->setDigitalStampActive($this->helper->getBoolConfig($carrierPath[$carrier], 'digital_stamp/active'));
$this->package->setMailboxActive($this->helper->getBoolConfig($carrierPath[$carrier], 'mailbox/active'));
$this->package->setDigitalStampActive($canHaveDigitalStamp && $this->helper->getBoolConfig($carrierPath, 'digital_stamp/active'));
$this->package->setMailboxActive($canHaveMailbox && $this->helper->getBoolConfig($carrierPath, 'mailbox/active'));
$this->package->setWeightFromQuoteProducts($products);

return $this->package->selectPackageType($products, $carrierPath[$carrier]);
return $this->package->selectPackageType($products, $carrierPath);
}

/**
Expand Down

0 comments on commit f6317f7

Please sign in to comment.