Skip to content

Commit 7ef3576

Browse files
committed
[*] refactoring preselect plugin
+ quote address assignment + method separation
1 parent a1e89f9 commit 7ef3576

File tree

4 files changed

+116
-107
lines changed

4 files changed

+116
-107
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
}
3535
],
3636
"require": {
37-
"php": "~7.3||~7.4",
37+
"php": "~7.4",
3838
"magento/framework": "^103.0.0",
3939
"magento/module-quote": "^101.2",
4040
"magento/module-customer": "^103.0"

src/Plugin/PreselectShipping.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}

src/Plugin/PreselectShippingMethod.php

Lines changed: 0 additions & 105 deletions
This file was deleted.

src/etc/frontend/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
44
<type name="Magento\Checkout\Model\Session">
55
<plugin name="integernet_shippingpreselection_preselectshippingmethod"
6-
type="IntegerNet\ShippingPreselection\Plugin\PreselectShippingMethod" sortOrder="20"/>
6+
type="IntegerNet\ShippingPreselection\Plugin\PreselectShipping" sortOrder="20"/>
77
<plugin name="integernet_shippingpreselection_resetshippingaddress"
88
type="IntegerNet\ShippingPreselection\Plugin\ResetShippingAddress" sortOrder="30"/>
99
</type>

0 commit comments

Comments
 (0)