Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
Consolidated cart address information provider
Browse files Browse the repository at this point in the history
  • Loading branch information
rogyar committed Oct 30, 2018
1 parent 97b519c commit 42b2358
Showing 1 changed file with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;

/**
* Class AddressDataProvider
Expand Down Expand Up @@ -49,22 +50,45 @@ public function getCartAddresses(CartInterface $cart): array
if ($shippingAddress) {
$shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class);
$shippingData['address_type'] = 'SHIPPING';
$shippingData['selected_shipping_method'] = [
'code' => $shippingAddress->getShippingMethod(),
'label' => $shippingAddress->getShippingDescription(),
'free_shipping' => $shippingAddress->getFreeShipping(),
];
$shippingData['items_weight'] = $shippingAddress->getWeight();
$shippingData['customer_notes'] = $shippingAddress->getCustomerNotes();
$addressData[] = $shippingData;
$addressData[] = array_merge($shippingData, $this->extractAddressData($shippingAddress));
}

if ($billingAddress) {
$billingData = $this->dataObjectConverter->toFlatArray($billingAddress, [], AddressInterface::class);
$billingData['address_type'] = 'BILLING';
$addressData[] = $billingData;
$addressData[] = array_merge($billingData, $this->extractAddressData($billingAddress));
}

return $addressData;
}

/**
* Extract the necessary address fields from address model
*
* @param QuoteAddress $address
* @return array
*/
private function extractAddressData(QuoteAddress $address): array
{
$addressData = [
'country' => [
'code' => $address->getCountryId(),
'label' => $address->getCountry()
],
'region' => [
'code' => $address->getRegionCode(),
'label' => $address->getRegion()
],
'street' => $address->getStreet(),
'selected_shipping_method' => [
'code' => $address->getShippingMethod(),
'label' => $address->getShippingDescription(),
'free_shipping' => $address->getFreeShipping(),
],
'items_weight' => $address->getWeight(),
'customer_notes' => $address->getCustomerNotes()
];

return $addressData;
}
}

0 comments on commit 42b2358

Please sign in to comment.