Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Addresses.php
Go to the documentation of this file.
1 <?php
7 
9 
18 {
23 
27  protected $_multishipping;
28 
33 
37  private $_addressConfig;
38 
42  protected $addressMapper;
43 
55  public function __construct(
56  \Magento\Framework\View\Element\Template\Context $context,
57  \Magento\Framework\Filter\DataObject\GridFactory $filterGridFactory,
58  \Magento\Multishipping\Model\Checkout\Type\Multishipping $multishipping,
59  \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
60  AddressConfig $addressConfig,
61  \Magento\Customer\Model\Address\Mapper $addressMapper,
62  array $data = []
63  ) {
64  $this->_filterGridFactory = $filterGridFactory;
65  $this->_multishipping = $multishipping;
66  $this->customerRepository = $customerRepository;
67  $this->_addressConfig = $addressConfig;
68  parent::__construct($context, $data);
69  $this->addressMapper = $addressMapper;
70  $this->_isScopePrivate = true;
71  }
72 
78  public function getCheckout()
79  {
80  return $this->_multishipping;
81  }
82 
86  protected function _prepareLayout()
87  {
88  $this->pageConfig->getTitle()->set(
89  __('Ship to Multiple Addresses') . ' - ' . $this->pageConfig->getTitle()->getDefault()
90  );
91  return parent::_prepareLayout();
92  }
93 
97  public function getItems()
98  {
99  $items = $this->getCheckout()->getQuoteShippingAddressesItems();
101  $itemsFilter = $this->_filterGridFactory->create();
102  $itemsFilter->addFilter(new \Magento\Framework\Filter\Sprintf('%d'), 'qty');
103  return $itemsFilter->filter($items);
104  }
105 
114  {
115  $select = $this->getLayout()->createBlock(\Magento\Framework\View\Element\Html\Select::class)
116  ->setName('ship[' . $index . '][' . $item->getQuoteItemId() . '][address]')
117  ->setId('ship_' . $index . '_' . $item->getQuoteItemId() . '_address')
118  ->setValue($item->getCustomerAddressId())
119  ->setOptions($this->getAddressOptions());
120 
121  return $select->getHtml();
122  }
123 
129  public function getAddressOptions()
130  {
131  $options = $this->getData('address_options');
132  if ($options === null) {
133  $options = [];
134  $addresses = [];
135 
136  try {
137  $addresses = $this->customerRepository->getById($this->getCustomerId())->getAddresses();
138  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
140  }
142  foreach ($addresses as $address) {
143  $label = $this->_addressConfig
144  ->getFormatByCode(AddressConfig::DEFAULT_ADDRESS_FORMAT)
145  ->getRenderer()
146  ->renderArray($this->addressMapper->toFlatArray($address));
147 
148  $options[] = [
149  'value' => $address->getId(),
150  'label' => $label,
151  ];
152  }
153  $this->setData('address_options', $options);
154  }
155 
156  return $options;
157  }
158 
164  public function getCustomerId()
165  {
166  return $this->getCheckout()->getCustomerSession()->getCustomerId();
167  }
168 
173  public function getItemUrl($item)
174  {
175  return $this->getUrl('catalog/product/view/id/' . $item->getProductId());
176  }
177 
182  public function getItemDeleteUrl($item)
183  {
184  return $this->getUrl('*/*/removeItem', ['address' => $item->getQuoteAddressId(), 'id' => $item->getId()]);
185  }
186 
190  public function getPostActionUrl()
191  {
192  return $this->getUrl('*/*/addressesPost');
193  }
194 
198  public function getNewAddressUrl()
199  {
200  return $this->getUrl('*/checkout_address/newShipping');
201  }
202 
206  public function getBackUrl()
207  {
208  return $this->getUrl('checkout/cart/');
209  }
210 
214  public function isContinueDisabled()
215  {
216  return !$this->getCheckout()->validateMinimumAmount();
217  }
218 }
getData($key='', $index=null)
Definition: DataObject.php:119
$addresses
Definition: address_list.php:7
__()
Definition: __.php:13
$address
Definition: customer.php:38
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Filter\DataObject\GridFactory $filterGridFactory, \Magento\Multishipping\Model\Checkout\Type\Multishipping $multishipping, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, AddressConfig $addressConfig, \Magento\Customer\Model\Address\Mapper $addressMapper, array $data=[])
Definition: Addresses.php:55
$label
Definition: details.phtml:21
setData($key, $value=null)
Definition: DataObject.php:72
$index
Definition: list.phtml:44
$items