9 use Magento\Customer\Api\Data\AddressInterfaceFactory;
10 use Magento\Customer\Api\Data\RegionInterfaceFactory;
100 class Address extends \Magento\Customer\Model\Address\AbstractAddress implements
256 private $storeManager;
301 \
Magento\Directory\Helper\Data $directoryData,
304 \
Magento\Directory\Model\RegionFactory $regionFactory,
305 \
Magento\Directory\Model\CountryFactory $countryFactory,
329 Json $serializer =
null,
332 $this->_scopeConfig = $scopeConfig;
333 $this->_addressItemFactory = $addressItemFactory;
334 $this->_itemCollectionFactory = $itemCollectionFactory;
335 $this->_addressRateFactory = $addressRateFactory;
336 $this->_rateCollector = $rateCollector;
337 $this->_rateCollectionFactory = $rateCollectionFactory;
338 $this->_rateRequestFactory = $rateRequestFactory;
339 $this->_totalCollectorFactory = $totalCollectorFactory;
340 $this->_addressTotalFactory = $addressTotalFactory;
341 $this->_objectCopyService = $objectCopyService;
342 $this->_carrierFactory = $carrierFactory;
388 parent::beforeSave();
401 $this->_dataSaveAllowed = (bool)$this->
getQuote()->getId();
411 if ($this->getCustomerAddressData()) {
415 if (!$this->
getId()) {
428 return $this->getAddressType() == \Magento\Quote\Model\Quote\Address::TYPE_SHIPPING &&
452 $defaultBillingAddress =
null;
453 $defaultShippingAddress =
null;
457 $defaultBillingAddress =
$customer->getDefaultBilling();
458 $defaultShippingAddress =
$customer->getDefaultShipping();
461 $defaultBillingAddress =
$customer->getDefaultBilling();
462 $defaultShippingAddress =
$customer->getDefaultShipping();
465 return !$defaultShippingAddress
466 || $defaultBillingAddress
467 && $defaultShippingAddress
468 && $defaultBillingAddress == $defaultShippingAddress;
480 $this->setQuoteId(
$quote->getId());
502 $this->_objectCopyService->copyFieldsetToTarget(
505 $this->addressMapper->toFlatArray(
$address),
510 if ($address->getCustomerId() && (!empty(
$quote) &&
$address->getCustomerId() ==
$quote->getCustomerId())) {
524 $customerAddressData = $this->_objectCopyService->getDataFromFieldset(
525 'sales_convert_quote_address',
526 'to_customer_address',
529 $customerAddressDataWithRegion = [];
530 $customerAddressDataWithRegion[
'region'][
'region'] = $customerAddressData[
'region'];
531 if (isset($customerAddressData[
'region_code'])) {
532 $customerAddressDataWithRegion[
'region'][
'region_code'] = $customerAddressData[
'region_code'];
534 if ($customerAddressData[
'region_id']) {
535 $customerAddressDataWithRegion[
'region'][
'region_id'] = $customerAddressData[
'region_id'];
537 $customerAddressData = array_merge($customerAddressData, $customerAddressDataWithRegion);
539 $addressDataObject = $this->addressDataFactory->create();
540 $this->dataObjectHelper->populateWithArray(
542 $customerAddressData,
545 return $addressDataObject;
554 public function toArray(array $arrAttributes = [])
556 $arr = parent::toArray($arrAttributes);
559 foreach ($this->
getTotals() as $k => $total) {
560 $arr[
'totals'][$k] = $total->toArray();
573 if (
null === $this->_items) {
574 $this->_items = $this->_itemCollectionFactory->create()->setAddressFilter($this->
getId());
575 if ($this->
getId()) {
576 foreach ($this->_items as
$item) {
577 $item->setAddress($this);
595 $key =
'cached_items_all';
597 $quoteItems = $this->
getQuote()->getItemsCollection();
601 if ($this->
getQuote()->getIsMultiShipping() && $addressItems->count() > 0) {
602 foreach ($addressItems as $aItem) {
603 if ($aItem->isDeleted()) {
607 if (!$aItem->getQuoteItemImported()) {
608 $qItem = $this->
getQuote()->getItemById($aItem->getQuoteItemId());
610 $aItem->importQuoteItem($qItem);
619 $addressType = $this->getAddressType();
620 $canAddItems = $this->
getQuote()->isVirtual()
625 foreach ($quoteItems as $qItem) {
626 if ($qItem->isDeleted()) {
652 if (!
$item->getParentItemId()) {
668 if ($this->
hasData(
'item_qty')) {
669 return $this->
getData(
'item_qty');
675 $qty +=
$item->getQty();
680 $qty =
$item->getQty();
706 if (
$item->getId() == $itemId) {
723 if (
$item->getId() == $itemId) {
740 if (
$item->getQuoteItemId() == $itemId) {
757 $item->isDeleted(
true);
773 if (
$item->getParentItemId()) {
776 $addressItem = $this->_addressItemFactory->create()->setAddress($this)->importQuoteItem(
$item);
779 if (
$item->getHasChildren()) {
780 foreach (
$item->getChildren() as $child) {
781 $addressChildItem = $this->_addressItemFactory->create()->setAddress(
792 $addressItem =
$item;
793 $addressItem->setAddress($this);
794 if (!$addressItem->getId()) {
800 $addressItem->setQty($qty);
813 if (
null === $this->_rates) {
814 $this->_rates = $this->_rateCollectionFactory->create()->setAddressFilter($this->
getId());
815 if ($this->
getId()) {
816 foreach ($this->_rates as
$rate) {
817 $rate->setAddress($this);
833 if (!
$rate->isDeleted()) {
850 if (!
$rate->isDeleted() && $this->_carrierFactory->get(
$rate->getCarrier())) {
856 $rates[
$rate->getCarrier()][0]->carrier_sort_order = $this->_carrierFactory->get(
861 uasort(
$rates, [$this,
'_sortRates']);
875 return (
int) $firstItem[0]->carrier_sort_order <=> (int) $secondItem[0]->carrier_sort_order;
887 if (
$rate->getId() == $rateId) {
920 $rate->isDeleted(
true);
933 $rate->setAddress($this);
946 if (!$this->getCollectShippingRates()) {
950 $this->setCollectShippingRates(
false);
958 $found = $this->requestShippingRates();
960 $this->
setShippingAmount(0)->setBaseShippingAmount(0)->setShippingMethod(
'')->setShippingDescription(
'');
978 $request = $this->_rateRequestFactory->create();
986 $request->setPackageValue(
$item ?
$item->getBaseRowTotal() : $this->getBaseSubtotal());
987 $packageWithDiscount =
$item ?
$item->getBaseRowTotal() -
989 $request->setPackageValueWithDiscount($packageWithDiscount);
996 $packagePhysicalValue =
$item ?
$item->getBaseRowTotal() : $this->getBaseSubtotal() -
997 $this->getBaseVirtualAmount();
998 $request->setPackagePhysicalValue($packagePhysicalValue);
1000 $request->setFreeMethodWeight(
$item ? 0 : $this->getFreeMethodWeight());
1005 $request->setStoreId($this->storeManager->getStore()->getId());
1006 $request->setWebsiteId($this->storeManager->getWebsite()->getId());
1007 $request->setFreeShipping($this->getFreeShipping());
1011 $request->setBaseCurrency($this->storeManager->getStore()->getBaseCurrency());
1012 $request->setPackageCurrency($this->storeManager->getStore()->getCurrentCurrency());
1013 $request->setLimitCarrier($this->getLimitCarrier());
1014 $baseSubtotalInclTax = $this->getBaseSubtotalTotalInclTax();
1015 $request->setBaseSubtotalInclTax($baseSubtotalInclTax);
1017 $result = $this->_rateCollector->create()->collectRates(
$request)->getResult();
1021 $shippingRates =
$result->getAllRates();
1023 foreach ($shippingRates as $shippingRate) {
1024 $rate = $this->_addressRateFactory->create()->importShippingRate($shippingRate);
1031 $item->setBaseShippingAmount(
$rate->getPrice());
1035 $store = $this->storeManager->getStore();
1036 $amountPrice =
$store->getBaseCurrency()
1037 ->convert(
$rate->getPrice(),
$store->getCurrentCurrencyCode());
1059 $totalsData = array_merge($this->
getData(), [
'address_quote_items' => $this->
getAllItems()]);
1062 $this->addTotal($total);
1074 public function addTotal($total)
1076 $addressTotal =
null;
1077 if (is_array($total)) {
1080 $addressTotal->setData($total);
1082 $addressTotal = $total;
1085 if ($addressTotal !==
null) {
1086 $addressTotal->setAddress($this);
1087 $this->_totals[$addressTotal->getCode()] = $addressTotal;
1132 $validateEnabled = $this->_scopeConfig->isSetFlag(
1133 'sales/minimum_order/active',
1137 if (!$validateEnabled) {
1141 if (!$this->
getQuote()->getIsVirtual() xor $this->getAddressType() == self::TYPE_SHIPPING) {
1145 $amount = $this->_scopeConfig->getValue(
1146 'sales/minimum_order/amount',
1150 $taxInclude = $this->_scopeConfig->getValue(
1151 'sales/minimum_order/tax_including',
1155 $taxes = $taxInclude ? $this->getBaseTaxAmount() : 0;
1167 $taxes = $this->
getData(
'applied_taxes');
1168 return $taxes ? $this->serializer->unserialize($taxes) : [];
1179 return $this->
setData(
'applied_taxes', $this->serializer->serialize(
$data));
1220 if (
$code !=
'subtotal') {
1238 if (
$code !=
'subtotal') {
1284 if (isset($this->_totalAmounts[
$code])) {
1285 return $this->_totalAmounts[
$code];
1299 if (isset($this->_baseTotalAmounts[
$code])) {
1300 return $this->_baseTotalAmounts[
$code];
1313 return $this->getBaseSubtotal() + $this->getBaseDiscountAmount();
1323 return $this->getSubtotal() + $this->getDiscountAmount();
1363 return $this->
getData(self::KEY_COUNTRY_ID);
1371 return $this->
setData(self::KEY_COUNTRY_ID, $countryId);
1379 $street = $this->
getData(self::KEY_STREET);
1380 return is_array($street) ? $street : explode(
"\n", $street);
1388 return $this->
setData(self::KEY_STREET, $street);
1396 return $this->
getData(self::KEY_COMPANY);
1404 return $this->
setData(self::KEY_COMPANY, $company);
1412 return $this->
getData(self::KEY_TELEPHONE);
1420 return $this->
setData(self::KEY_TELEPHONE, $telephone);
1428 return $this->
getData(self::KEY_FAX);
1436 return $this->
setData(self::KEY_FAX, $fax);
1444 return $this->
getData(self::KEY_POSTCODE);
1452 return $this->
setData(self::KEY_POSTCODE, $postcode);
1460 return $this->
getData(self::KEY_CITY);
1468 return $this->
setData(self::KEY_CITY, $city);
1476 return $this->
getData(self::KEY_FIRSTNAME);
1484 return $this->
setData(self::KEY_FIRSTNAME, $firstname);
1492 return $this->
getData(self::KEY_LASTNAME);
1500 return $this->
setData(self::KEY_LASTNAME, $lastname);
1508 return $this->
getData(self::KEY_MIDDLENAME);
1516 return $this->
setData(self::KEY_MIDDLENAME, $middlename);
1524 return $this->
getData(self::KEY_PREFIX);
1540 return $this->
getData(self::KEY_SUFFIX);
1556 return $this->
getData(self::KEY_VAT_ID);
1564 return $this->
setData(self::KEY_VAT_ID, $vatId);
1572 return $this->
getData(self::KEY_CUSTOMER_ID);
1609 return $this->
setData(self::KEY_REGION, $region);
1617 return $this->
setData(self::KEY_REGION_ID, $regionId);
1625 return $this->
setData(self::KEY_REGION_CODE, $regionCode);
1633 return $this->
getData(self::SAME_AS_BILLING);
1641 return $this->
setData(self::SAME_AS_BILLING, $sameAsBilling);
1649 return $this->
getData(self::CUSTOMER_ADDRESS_ID);
1657 return $this->
setData(self::CUSTOMER_ADDRESS_ID, $customerAddressId);
1667 return $this->
getData(self::SAVE_IN_ADDRESS_BOOK);
1678 return $this->
setData(self::SAVE_IN_ADDRESS_BOOK, $saveInAddressBook);
1711 return $this->
getData(
'shipping_method');
1719 return array_keys($this->attributeList->getAttributes());
_getExtensionAttributes()
elseif(isset( $params[ 'redirect_parent']))
getValidItemById($itemId)
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory, \Magento\Directory\Helper\Data $directoryData, \Magento\Eav\Model\Config $eavConfig, \Magento\Customer\Model\Address\Config $addressConfig, \Magento\Directory\Model\RegionFactory $regionFactory, \Magento\Directory\Model\CountryFactory $countryFactory, AddressMetadataInterface $metadataService, AddressInterfaceFactory $addressDataFactory, RegionInterfaceFactory $regionDataFactory, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Quote\Model\Quote\Address\ItemFactory $addressItemFactory, \Magento\Quote\Model\ResourceModel\Quote\Address\Item\CollectionFactory $itemCollectionFactory, \Magento\Quote\Model\Quote\Address\RateFactory $addressRateFactory, \Magento\Quote\Model\Quote\Address\RateCollectorInterfaceFactory $rateCollector, \Magento\Quote\Model\ResourceModel\Quote\Address\Rate\CollectionFactory $rateCollectionFactory, \Magento\Quote\Model\Quote\Address\RateRequestFactory $rateRequestFactory, \Magento\Quote\Model\Quote\Address\Total\CollectorFactory $totalCollectorFactory, \Magento\Quote\Model\Quote\Address\TotalFactory $addressTotalFactory, \Magento\Framework\DataObject\Copy $objectCopyService, \Magento\Shipping\Model\CarrierFactoryInterface $carrierFactory, Address\Validator $validator, \Magento\Customer\Model\Address\Mapper $addressMapper, Address\CustomAttributeListInterface $attributeList, \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector, \Magento\Quote\Model\Quote\TotalsReader $totalsReader, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], Json $serializer=null, StoreManagerInterface $storeManager=null)
shippingRatesCollectionWasSet()
setBaseTotalAmount($code, $amount)
getCustomAttributesCodes()
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
addTotalAmount($code, $amount)
importCustomerAddressData(\Magento\Customer\Api\Data\AddressInterface $address)
const ADDRESS_TYPE_SHIPPING
_sortRates($firstItem, $secondItem)
setTotalAmount($code, $amount)
getSubtotalWithDiscount()
setSameAsBilling($sameAsBilling)
setBaseShippingAmount($value, $alreadyExclTax=false)
getShippingRateById($rateId)
getGroupedAllShippingRates()
_isDefaultShippingNullOrSameAsBillingAddress()
_populateBeforeSaveData()
addItem(\Magento\Quote\Model\Quote\Item\AbstractItem $item, $qty=null)
setCustomerAddressId($customerAddressId)
setRegionCode($regionCode)
setMiddlename($middlename)
setExtensionAttributes(\Magento\Quote\Api\Data\AddressExtensionInterface $extensionAttributes)
addBaseTotalAmount($code, $amount)
getShippingRateByCode($code)
getData($key='', $index=null)
toArray(array $arrAttributes=[])
const ADDRESS_TYPE_BILLING
_isNotRegisteredCustomer()
getBaseSubtotalWithDiscount()
setSaveInAddressBook($saveInAddressBook)
addShippingRate(\Magento\Quote\Model\Quote\Address\Rate $rate)
setShippingAmount($value, $alreadyExclTax=false)
setCustomerId($customerId)
getItemByQuoteItemId($itemId)
getBaseTotalAmount($code)
_getValidationRulesBeforeSave()
setData($key, $value=null)
setQuote(\Magento\Quote\Model\Quote $quote)
getShippingRatesCollection()