8
8
use Magento \Framework \Pricing \PriceCurrencyInterface ;
9
9
use Magento \Store \Model \Store ;
10
10
use Magento \Customer \Model \Address ;
11
+ use Magento \Customer \Model \Session ;
11
12
use Magento \Tax \Model \Config ;
12
13
use Magento \Customer \Model \Session as CustomerSession ;
13
14
use Magento \Tax \Api \OrderTaxManagementInterface ;
@@ -100,6 +101,13 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
100
101
*/
101
102
private $ serializer ;
102
103
104
+ /**
105
+ * Customer session model.
106
+ *
107
+ * @var Session
108
+ */
109
+ protected $ customerSession ;
110
+
103
111
/**
104
112
* Constructor
105
113
*
@@ -114,6 +122,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
114
122
* @param OrderTaxManagementInterface $orderTaxManagement
115
123
* @param PriceCurrencyInterface $priceCurrency
116
124
* @param Json $serializer
125
+ * @param Session|null $customerSession
117
126
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
118
127
*/
119
128
public function __construct (
@@ -127,7 +136,8 @@ public function __construct(
127
136
\Magento \Catalog \Helper \Data $ catalogHelper ,
128
137
OrderTaxManagementInterface $ orderTaxManagement ,
129
138
PriceCurrencyInterface $ priceCurrency ,
130
- Json $ serializer = null
139
+ Json $ serializer = null ,
140
+ Session $ customerSession = null
131
141
) {
132
142
parent ::__construct ($ context );
133
143
$ this ->priceCurrency = $ priceCurrency ;
@@ -140,6 +150,7 @@ public function __construct(
140
150
$ this ->catalogHelper = $ catalogHelper ;
141
151
$ this ->orderTaxManagement = $ orderTaxManagement ;
142
152
$ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (Json::class);
153
+ $ this ->customerSession = $ customerSession ?: ObjectManager::getInstance ()->get (Session::class);
143
154
}
144
155
145
156
/**
@@ -797,4 +808,95 @@ public function isCatalogPriceDisplayAffectedByTax($store = null)
797
808
return $ this ->displayPriceIncludingTax ();
798
809
}
799
810
}
811
+
812
+ /**
813
+ * Set default Tax Billing and Shipping address into customer session after address save.
814
+ *
815
+ * @param Address $address
816
+ * @return void
817
+ */
818
+ public function setAddressCustomerSessionAddressSave (Address $ address )
819
+ {
820
+ if ($ this ->isDefaultBilling ($ address )) {
821
+ $ this ->customerSession ->setDefaultTaxBillingAddress (
822
+ [
823
+ 'country_id ' => $ address ->getCountryId (),
824
+ 'region_id ' => $ address ->getRegion () ? $ address ->getRegionId () : null ,
825
+ 'postcode ' => $ address ->getPostcode (),
826
+ ]
827
+ );
828
+ }
829
+ if ($ this ->isDefaultShipping ($ address )) {
830
+ $ this ->customerSession ->setDefaultTaxShippingAddress (
831
+ [
832
+ 'country_id ' => $ address ->getCountryId (),
833
+ 'region_id ' => $ address ->getRegion () ? $ address ->getRegionId () : null ,
834
+ 'postcode ' => $ address ->getPostcode (),
835
+ ]
836
+ );
837
+ }
838
+ }
839
+
840
+ /**
841
+ * Set default Tax Shipping and Billing addresses into customer session after login.
842
+ *
843
+ * @param \Magento\Customer\Api\Data\AddressInterface[] $addresses
844
+ * @return void
845
+ */
846
+ public function setAddressCustomerSessionLogIn (array $ addresses )
847
+ {
848
+ $ defaultShippingFound = false ;
849
+ $ defaultBillingFound = false ;
850
+ foreach ($ addresses as $ address ) {
851
+ if ($ address ->isDefaultBilling ()) {
852
+ $ defaultBillingFound = true ;
853
+ $ this ->customerSession ->setDefaultTaxBillingAddress (
854
+ [
855
+ 'country_id ' => $ address ->getCountryId (),
856
+ 'region_id ' => $ address ->getRegion () ? $ address ->getRegionId () : null ,
857
+ 'postcode ' => $ address ->getPostcode (),
858
+ ]
859
+ );
860
+ }
861
+ if ($ address ->isDefaultShipping ()) {
862
+ $ defaultShippingFound = true ;
863
+ $ this ->customerSession ->setDefaultTaxShippingAddress (
864
+ [
865
+ 'country_id ' => $ address ->getCountryId (),
866
+ 'region_id ' => $ address ->getRegion () ? $ address ->getRegionId () : null ,
867
+ 'postcode ' => $ address ->getPostcode (),
868
+ ]
869
+ );
870
+ }
871
+ if ($ defaultShippingFound && $ defaultBillingFound ) {
872
+ break ;
873
+ }
874
+ }
875
+ }
876
+
877
+ /**
878
+ * Check whether specified billing address is default for customer from address.
879
+ *
880
+ * @param Address $address
881
+ * @return bool
882
+ */
883
+ private function isDefaultBilling (Address $ address )
884
+ {
885
+ return $ address ->getId () && $ address ->getId () == $ address ->getCustomer ()->getDefaultBilling ()
886
+ || $ address ->getIsPrimaryBilling ()
887
+ || $ address ->getIsDefaultBilling ();
888
+ }
889
+
890
+ /**
891
+ * Check whether specified shipping address is default for customer from address.
892
+ *
893
+ * @param Address $address
894
+ * @return bool
895
+ */
896
+ private function isDefaultShipping (Address $ address )
897
+ {
898
+ return $ address ->getId () && $ address ->getId () == $ address ->getCustomer ()->getDefaultShipping ()
899
+ || $ address ->getIsPrimaryShipping ()
900
+ || $ address ->getIsDefaultShipping ();
901
+ }
800
902
}
0 commit comments