Skip to content

Commit 05be786

Browse files
authored
Merge pull request #4 from integer-net/20211001-update
Update module
2 parents 732c0df + 68c2826 commit 05be786

File tree

12 files changed

+301
-98
lines changed

12 files changed

+301
-98
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7-
## [Unreleased]
7+
##[0.0.1] - 2021-10-01
88
### Added
9+
- Services to set and unset mock data
910
### Changed
10-
### Removed
11-
### Fixed
11+
- improved module structure

README.md

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
[![Maintainability][ico-maintainability]][link-maintainability]
1111
</div>
1212

13-
# IntegerNet ShippingPreselection (AutoShipping)
13+
# IntegerNet ShippingPreselection (AutoShipping)
1414

15-
This module provides methods to preselect a shipping method upon quote creation of the customer. It fetches all available countries from Magento Config and will preselect the default country/region/postcode of the current storeview.
15+
This module preselects the cheapest shipping method by filling shipping address with mock data and default country/region/postcode of the current storeview.
16+
17+
Upon entering checkout, PayPal Express or PayOne pages, the mock data is removed from shipping address.
1618

17-
**Important:** This is WIP as of yet - shipping address mock data still needs to be cleared when entering checkout.
1819

1920

2021
## Installation
@@ -32,76 +33,99 @@ This module provides methods to preselect a shipping method upon quote creation
3233
3334
## Configuration
3435
35-
In general, make sure your configuration settings meet requirements:
36+
In general, make sure your configuration settings meet requirements:
3637
3738
- postcode, region and country set in General > Store Information
3839
- all available countries need to have at least one shipping method available
3940
- available countries cannot have mandatory region setting
4041
4142
1) Add selectedShippingCountry select to `Mage_Checkout::cart.phtml`
42-
43-
43+
44+
4445
```
4546
<?= $block->getChildHtml('shipping_country') ?>
4647

4748
```
4849
2. Add shipping country script to `Mage_Checkout::cart/js/cart.phtml`
49-
50+
5051
```
5152
updateCartDataDependencies() {
5253
[...]
5354
this.selectedShippingCountry = this.cartData && this.cartData.shipping_addresses && this.cartData.shipping_addresses[0] && this.cartData.shipping_addresses[0].country && this.cartData.shipping_addresses[0].country.code || null
5455
},
5556
<?= $block->getChildHtml('shipping_country_js') ?>
5657
```
57-
5858
59-
3) Set config value for the mock data `integernet/shipping_preselection/mock_data` to custom value if desired
60-
59+
60+
3) Set config value for the mock data `integernet/shipping_preselection/mock_data` to custom value if desired
61+
6162
4) If you have an altered cart query for GraphQl, you need to override `IntegerNet\ShippingPreselection\ViewModel\ShippingAddressMutation` accordingly.
6263
63-
## Usage
6464
65-
## Changelog
65+
.
6666
67-
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
67+
Please mind: *The template provided is supposed to work with table rates / one shipping method.*
6868
69-
## Contributing
69+
If you have several shipping methods and through changes made in cart - like quantity changes - the best available method changes, this will not be recognized by the module (because a shipping method is already in place). You can solve this by checking client-side in the cart, or adding a check backend-wise.
7070
71-
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
71+
## Extending configuration
7272
73-
## Testing
73+
Override config values for integernet/shipping_preselection/mock_clearance_urls or hook into the service to modify behaviour for pages where the shipping address should be (un-)mocked.
7474
75-
### Unit Tests
75+
## Known issues
7676
77-
```
78-
./vendor/bin/phpunit tests/unit
79-
```
77+
**1. When aborting PayPal Express and return to cart, the shipping method isn't displayed anymore**
8078
81-
### Magento Integration Tests
79+
Paypal doesn't properly set the telephone address attribute, as it is called differently in its response. You can make the telephone field nullable in a `etc/schema.graphql` of your own
8280
83-
0. Configure test database in `dev/tests/integration/etc/install-config-mysql.php`. [Read more in the Magento docs.](https://devdocs.magento.com/guides/v2.4/test/integration/integration_test_execution.html)
8481
85-
1. Copy `tests/integration/phpunit.xml.dist` from the package to `dev/tests/integration/phpunit.xml` in your Magento installation.
82+
interface CartAddressInterface {
83+
telephone: String
84+
}
85+
86+
input CartAddressInput {
87+
telephone: String
88+
}
8689
87-
2. In that directory, run
88-
``` bash
89-
../../../vendor/bin/phpunit
90-
```
90+
91+
92+
93+
## Changelog
94+
95+
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
96+
97+
## Contributing
98+
99+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
91100
92101
## Security
93102
94103
If you discover any security related issues, please email security@integer-net.de instead of using the issue tracker.
95104
96105
## Credits
97106
98-
- [Lisa Buchholz][link-author]
107+
- [integer_net GmbH][link-author]
99108
- [All Contributors][link-contributors]
100109
101110
## License
102111
103112
The MIT License (MIT). Please see [License File](LICENSE) for more information.
104113
114+
[ico-version]: https://img.shields.io/packagist/v/integer-net/magento2-shippingpreselection.svg?style=flat-square
115+
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
116+
[ico-code-quality]: https://img.shields.io/scrutinizer/g/integer-net/magento2-shippingpreselection.svg?style=flat-square
117+
[ico-maintainability]: https://img.shields.io/codeclimate/maintainability/integer-net/magento2-shippingpreselection?style=flat-square
118+
[ico-compatibility]: https://img.shields.io/badge/magento-2.4-brightgreen.svg?logo=magento&longCache=true&style=flat-square
119+
120+
[link-packagist]: https://packagist.org/packages/integer-net/magento2-shippingpreselection
121+
[link-scrutinizer]: https://scrutinizer-ci.com/g/integer-net/magento2-shippingpreselection/code-structure
122+
[link-code-quality]: https://scrutinizer-ci.com/g/integer-net/magento2-shippingpreselection
123+
[link-maintainability]: https://codeclimate.com/github/integer-net/magento2-shippingpreselection
124+
[link-author]: https://github.com/integer_net
125+
[link-contributors]: ../../contributors
126+
127+
128+
105129
[ico-version]: https://img.shields.io/packagist/v/integer-net/magento2-shippingpreselection.svg?style=flat-square
106130
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
107131
[ico-travis]: https://img.shields.io/travis/integer-net/magento2-shippingpreselection/master.svg?style=flat-square

src/ExampleClass.php

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,53 @@
11
<?php
22
declare(strict_types=1);
33

4-
// phpcs:disable PSR2.Methods.FunctionCallSignature.Indent
5-
64
namespace IntegerNet\ShippingPreselection\Plugin;
75

6+
use IntegerNet\ShippingPreselection\Service\AddressSetMockdata;
7+
use IntegerNet\ShippingPreselection\Service\AddressResetConditions;
88
use Magento\Checkout\Model\Session;
99
use Magento\Quote\Model\Quote;
10+
use Magento\Quote\Model\Quote\Address;
1011
use Magento\Quote\Model\Quote\Address\Rate;
1112
use Magento\Quote\Model\Quote\AddressFactory;
1213
use Magento\Quote\Model\ShippingMethodManagement;
1314
use Magento\Framework\App\Config\ScopeConfigInterface;
1415

1516
class PreselectShippingMethod
1617
{
17-
private ScopeConfigInterface $storeConfig;
1818
private ShippingMethodManagement $shippingMethodManagement;
1919
private AddressFactory $addressFactory;
20-
21-
private const CONFIG_PATH_DEFAULT_COUNTRY_ID = 'general/country/default';
22-
private const CONFIG_PATH_DEFAULT_REGION_ID = 'general/store_information/region_id';
23-
private const CONFIG_PATH_DEFAULT_POSTCODE = 'general/store_information/postcode';
24-
private const CONFIG_PATH_MOCK_DATASET = 'integernet/shipping_preselection/mock_data';
20+
private AddressSetMockdata $addressSetMockdata;
21+
private AddressResetConditions $addressResetConditions;
2522

2623
public function __construct(
2724
ScopeConfigInterface $storeConfig,
2825
ShippingMethodManagement $shippingMethodManagement,
29-
AddressFactory $addressFactory)
26+
AddressFactory $addressFactory,
27+
AddressResetConditions $addressResetConditions,
28+
AddressSetMockdata $addressSetMockdata)
3029
{
3130
$this->storeConfig = $storeConfig;
3231
$this->shippingMethodManagement = $shippingMethodManagement;
3332
$this->addressFactory = $addressFactory;
33+
$this->addressSetMockdata = $addressSetMockdata;
34+
$this->addressResetConditions = $addressResetConditions;
3435
}
3536

3637
/**
3738
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3839
*/
3940
public function afterGetQuote(Session $subject, Quote $result): Quote
4041
{
41-
if (!$result->getIsVirtual() && $result->getItemsCount()) {
42-
if (!$result->getShippingAddress()
43-
|| $result->getShippingAddress() && !$result->getShippingAddress()->getShippingMethod()) {
44-
$this->prepareShippingAddress($result);
42+
if (!$this->addressResetConditions->isAddressResetRequest() && !$this->addressResetConditions->isAddressIgnoreRequest()) {
43+
if (!$result->getIsVirtual() && $result->getItemsCount()) {
44+
$shippingAddress = $result->getShippingAddress();
45+
if (!$shippingAddress || !$shippingAddress->validate() || !$shippingAddress->getShippingMethod()) {
46+
$this->prepareShippingAddress($shippingAddress, $result);
47+
}
48+
49+
$this->prepareShippingRates($result);
4550
}
46-
47-
$this->prepareShippingRates($result);
4851
}
4952

5053
return $result;
@@ -54,57 +57,28 @@ public function afterGetQuote(Session $subject, Quote $result): Quote
5457
* GraphQl requires fully valid address data to work with in cart, so we need to make up data if
5558
* it has not been set yet
5659
*
57-
* @param Quote $quote
58-
* @return Quote
60+
* @param Address $quote
5961
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
6062
* @SuppressWarnings(PHPMD.NPathComplexity)
6163
*/
62-
private function prepareShippingAddress(Quote $quote): Quote
64+
private function prepareShippingAddress(Address $shippingAddress, Quote $quote): void
6365
{
64-
$address = $quote->getShippingAddress();
65-
66-
if (!$address) {
67-
$address = $this->addressFactory->create();
68-
$address->setQuote($quote);
66+
if (!$shippingAddress) {
67+
$shippingAddress = $this->addressFactory->create();
68+
$shippingAddress->setQuote($quote);
6969
}
7070

71-
if ($address->validate() !== true) {
72-
$prefill = $this->storeConfig->getValue(self::CONFIG_PATH_MOCK_DATASET, 'store');
73-
74-
$address->setFirstname($address->getFirstname() ?: $prefill);
75-
$address->setLastname($address->getLastname() ?: $prefill);
76-
$address->setPostcode(
77-
$address->getPostcode() ?: $this->storeConfig->getValue(self::CONFIG_PATH_DEFAULT_POSTCODE, 'store')
78-
);
79-
$address->setCity($address->getCity() ?: $prefill);
80-
$address->setTelephone($address->getTelephone() ?: $prefill);
81-
$address->setRegion(
82-
$address->getRegion() ?: $this->storeConfig->getValue(self::CONFIG_PATH_DEFAULT_REGION_ID, 'store')
83-
);
84-
$address->setCountryId(
85-
$address->getData('country_id')
86-
?: $this->storeConfig->getValue(
87-
self::CONFIG_PATH_DEFAULT_COUNTRY_ID,
88-
'store'
89-
)
90-
);
91-
$address->setStreet(
92-
(is_array($address->getStreet()) && count($address->getStreet()) && $address->getStreet()[0] !== '')
93-
|| is_string($address->getStreet()) && strlen($address->getStreet()) ? $address->getStreet()
94-
: [$prefill]
95-
);
71+
if ($shippingAddress->validate() !== true) {
72+
$this->addressSetMockdata->setMockDataOnAddress($shippingAddress);
9673
}
97-
98-
return $quote;
9974
}
10075

10176
/**
10277
* try setting cheapest shipping rate available for customer
10378
*
10479
* @param Quote $quote
105-
* @return Quote
10680
*/
107-
private function prepareShippingRates(Quote $quote): Quote
81+
private function prepareShippingRates(Quote $quote): void
10882
{
10983
$quote->getShippingAddress()->requestShippingRates();
11084
$rates = $quote->getShippingAddress()->getShippingRatesCollection();
@@ -129,7 +103,5 @@ private function prepareShippingRates(Quote $quote): Quote
129103
$quote->addErrorInfo('error', null, $e->getCode(), $e->getMessage());
130104
}
131105
}
132-
133-
return $quote;
134106
}
135107
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\ShippingPreselection\Plugin;
5+
6+
use Magento\Checkout\Model\Session;
7+
use Magento\Quote\Model\Quote;
8+
use IntegerNet\ShippingPreselection\Service\AddressUnsetMockdata;
9+
use IntegerNet\ShippingPreselection\Service\AddressResetConditions;
10+
11+
class ResetShippingAddress
12+
{
13+
14+
private AddressUnsetMockdata $addressUnsetMockdata;
15+
private AddressResetConditions $addressResetConditions;
16+
private $resetUrls = null;
17+
18+
public function __construct(
19+
AddressUnsetMockdata $addressUnsetMockdata,
20+
AddressResetConditions $addressResetConditions)
21+
{
22+
$this->addressUnsetMockdata = $addressUnsetMockdata;
23+
$this->addressResetConditions = $addressResetConditions;
24+
}
25+
26+
/**
27+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
28+
*/
29+
public function afterGetQuote(Session $subject, Quote $result): Quote
30+
{
31+
if ($this->addressResetConditions->isAddressResetRequest()) {
32+
if (!$result->getIsVirtual() && $result->getItemsCount()) {
33+
$shippingAddress = $result->getShippingAddress();
34+
35+
if ($this->addressUnsetMockdata->isMockedAddress($shippingAddress)) {
36+
$this->addressUnsetMockdata->resetShippingAddress($result);
37+
} else {
38+
$this->addressUnsetMockdata->checkForEmptyAddressFields($shippingAddress);
39+
}
40+
}
41+
}
42+
43+
return $result;
44+
}
45+
}

0 commit comments

Comments
 (0)