|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace IntegerNet\ShippingPreselection\Plugin; |
| 5 | + |
| 6 | +use IntegerNet\ShippingPreselection\Service\AddressSetMockdata; |
| 7 | +use IntegerNet\ShippingPreselection\Service\AddressResetConditions; |
| 8 | +use Magento\Checkout\Model\Session; |
| 9 | +use Magento\Quote\Model\Quote; |
| 10 | +use Magento\Quote\Model\Quote\Address; |
| 11 | +use Magento\Quote\Model\Quote\Address\Rate; |
| 12 | +use Magento\Quote\Model\ShippingMethodManagement; |
| 13 | +use Magento\Quote\Model\ShippingAddressAssignment; |
| 14 | + |
| 15 | +class PreselectShipping |
| 16 | +{ |
| 17 | + private ShippingMethodManagement $methodManagement; |
| 18 | + private AddressSetMockdata $addressSetMockData; |
| 19 | + private AddressResetConditions $addressReset; |
| 20 | + private ShippingAddressAssignment $addressAssignment; |
| 21 | + |
| 22 | + public function __construct( |
| 23 | + ShippingMethodManagement $methodManagement, |
| 24 | + AddressResetConditions $addressReset, |
| 25 | + AddressSetMockdata $addressSetMockdata, |
| 26 | + ShippingAddressAssignment $addressAssignment |
| 27 | + ) { |
| 28 | + $this->methodManagement = $methodManagement; |
| 29 | + $this->addressSetMockData = $addressSetMockdata; |
| 30 | + $this->addressReset = $addressReset; |
| 31 | + $this->addressAssignment = $addressAssignment; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 36 | + */ |
| 37 | + public function afterGetQuote(Session $subject, Quote $result): Quote |
| 38 | + { |
| 39 | + if (!$this->isPreselectionAllowed($result)) { |
| 40 | + return $result; |
| 41 | + } |
| 42 | + $address = $result->getShippingAddress(); |
| 43 | + if ($this->shouldMockAddress($address)) { |
| 44 | + $this->addressSetMockData->setMockDataOnAddress($address); |
| 45 | + $this->addressAssignment->setAddress($result, $address); |
| 46 | + } |
| 47 | + $this->preselectShippingMethod($result); |
| 48 | + return $result; |
| 49 | + } |
| 50 | + |
| 51 | + public function shouldMockAddress(Address $address): bool |
| 52 | + { |
| 53 | + return (true !== $address->validate()); |
| 54 | + } |
| 55 | + |
| 56 | + public function preselectShippingMethod(Quote $quote): void |
| 57 | + { |
| 58 | + $quote->getShippingAddress()->requestShippingRates(); // load new rates |
| 59 | + if (!$rate = $this->getCheapestShippingRate($quote)) { |
| 60 | + return; |
| 61 | + } |
| 62 | + try { |
| 63 | + $this->methodManagement->set( |
| 64 | + $quote->getId(), |
| 65 | + $rate->getCarrier(), |
| 66 | + $rate->getMethod() |
| 67 | + ); |
| 68 | + } catch (\Exception $e) { |
| 69 | + $quote->addErrorInfo('error', null, $e->getCode(), $e->getMessage()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public function getCheapestShippingRate(Quote $quote): ?Rate |
| 74 | + { |
| 75 | + $selectedRate = null; |
| 76 | + foreach ($this->getShippingRates($quote) as $rate) { |
| 77 | + /** @var Rate $rate */ |
| 78 | + if ($selectedRate === null || $rate->getPrice() < $selectedRate->getPrice()) { |
| 79 | + $selectedRate = $rate; |
| 80 | + } |
| 81 | + } |
| 82 | + return $selectedRate; |
| 83 | + } |
| 84 | + |
| 85 | + public function getShippingRates(Quote $quote) |
| 86 | + { |
| 87 | + return $quote->getShippingAddress()->getShippingRatesCollection(); |
| 88 | + } |
| 89 | + |
| 90 | + public function isPreselectionAllowed(Quote $quote): bool |
| 91 | + { |
| 92 | + return $this->validateShippingResetConditions() && |
| 93 | + $this->validateQuoteConditions($quote) && |
| 94 | + $this->validateShippingConditions($quote); |
| 95 | + } |
| 96 | + |
| 97 | + public function validateShippingResetConditions(): bool |
| 98 | + { |
| 99 | + return !$this->addressReset->isAddressResetRequest() && |
| 100 | + !$this->addressReset->isAddressIgnoreRequest(); |
| 101 | + } |
| 102 | + |
| 103 | + public function validateQuoteConditions(Quote $quote): bool |
| 104 | + { |
| 105 | + return !$quote->getIsVirtual() && $quote->getItemsCount(); |
| 106 | + } |
| 107 | + |
| 108 | + public function validateShippingConditions(Quote $quote): bool |
| 109 | + { |
| 110 | + $address = $quote->getShippingAddress(); |
| 111 | + $shippingIsFine = $address->validate() && !empty($address->getShippingMethod()); |
| 112 | + return !$shippingIsFine; |
| 113 | + } |
| 114 | +} |
0 commit comments