@@ -7,15 +7,18 @@ class CheckoutDataToQuoteHandler implements \Magento\Payment\Gateway\Response\Ha
77 private $ shippingInformationManagement ;
88 private $ shippingInformationFactory ;
99 private $ addressInterfaceFactory ;
10+ private $ region ;
1011
1112 public function __construct (
1213 \Magento \Checkout \Api \ShippingInformationManagementInterface $ shippingInformationManagement ,
1314 \Magento \Checkout \Api \Data \ShippingInformationInterfaceFactory $ shippingInformationFactory ,
14- \Magento \Quote \Api \Data \AddressInterfaceFactory $ addressInterfaceFactory
15+ \Magento \Quote \Api \Data \AddressInterfaceFactory $ addressInterfaceFactory ,
16+ \Magento \Directory \Model \Region $ region
1517 ) {
1618 $ this ->shippingInformationManagement = $ shippingInformationManagement ;
1719 $ this ->shippingInformationFactory = $ shippingInformationFactory ;
1820 $ this ->addressInterfaceFactory = $ addressInterfaceFactory ;
21+ $ this ->region = $ region ;
1922 }
2023
2124 public function handle (array $ handlingSubject , array $ response ): void
@@ -60,15 +63,21 @@ public function handle(array $handlingSubject, array $response): void
6063 ->setCountryId ($ response ['shipping ' ]['countryCode ' ])
6164 ->setStreet ([$ response ['shipping ' ]['line1 ' ]])
6265 ->setPostcode ($ response ['shipping ' ]['postcode ' ])
63- ->setRegion ($ response ['shipping ' ]['region ' ] ?? '' );
66+ ->setRegion ($ response ['shipping ' ]['region ' ] ?? '' )
67+ ->setRegionId (
68+ $ this ->region ->loadByCode ($ address ->getRegion (), $ address ->getCountryId ())->getId ()
69+ ?? $ this ->region ->loadByName ($ address ->getRegion (), $ address ->getCountryId ())->getId ()
70+ );
6471 if (isset ($ response ['shipping ' ]['line2 ' ]) && $ streetLine2 = $ response ['shipping ' ]['line2 ' ]) {
6572 /** @var string[] $street */
6673 $ street = array_merge ($ address ->getStreet (), [$ streetLine2 ]);
6774 $ address ->setStreet ($ street );
6875 }
6976
7077 $ shippingInformation ->setBillingAddress ($ address );
71- if (!$ quote ->isVirtual ()) {
78+ if ($ quote ->isVirtual ()) {
79+ $ shippingInformation ->setShippingAddress ($ address ); // to avoid an error with gift cart registry
80+ } else {
7281 $ explodedShippingOption = explode ('_ ' , $ response ['shippingOptionIdentifier ' ]);
7382 $ carrierCode = array_shift ($ explodedShippingOption );
7483 $ methodCode = implode ('_ ' , $ explodedShippingOption );
0 commit comments