123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Multishipping\Block\Checkout;
- use Magento\Customer\Model\Address\Config as AddressConfig;
- /**
- * Class Addresses
- * Multishipping checkout choose item addresses block
- *
- * @api
- * @since 100.0.2
- */
- class Addresses extends \Magento\Sales\Block\Items\AbstractItems
- {
- /**
- * @var \Magento\Framework\Filter\DataObject\GridFactory
- */
- protected $_filterGridFactory;
- /**
- * @var \Magento\Multishipping\Model\Checkout\Type\Multishipping
- */
- protected $_multishipping;
- /**
- * @var \Magento\Customer\Api\CustomerRepositoryInterface
- */
- protected $customerRepository;
- /**
- * @var \Magento\Customer\Model\Address\Config
- */
- private $_addressConfig;
- /**
- * @var \Magento\Customer\Model\Address\Mapper
- */
- protected $addressMapper;
- /**
- * Constructor
- *
- * @param \Magento\Framework\View\Element\Template\Context $context
- * @param \Magento\Framework\Filter\DataObject\GridFactory $filterGridFactory
- * @param \Magento\Multishipping\Model\Checkout\Type\Multishipping $multishipping
- * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
- * @param AddressConfig $addressConfig
- * @param \Magento\Customer\Model\Address\Mapper $addressMapper
- * @param array $data
- */
- public function __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 = []
- ) {
- $this->_filterGridFactory = $filterGridFactory;
- $this->_multishipping = $multishipping;
- $this->customerRepository = $customerRepository;
- $this->_addressConfig = $addressConfig;
- parent::__construct($context, $data);
- $this->addressMapper = $addressMapper;
- $this->_isScopePrivate = true;
- }
- /**
- * Retrieve multishipping checkout model
- *
- * @return \Magento\Multishipping\Model\Checkout\Type\Multishipping
- */
- public function getCheckout()
- {
- return $this->_multishipping;
- }
- /**
- * @return $this
- */
- protected function _prepareLayout()
- {
- $this->pageConfig->getTitle()->set(
- __('Ship to Multiple Addresses') . ' - ' . $this->pageConfig->getTitle()->getDefault()
- );
- return parent::_prepareLayout();
- }
- /**
- * @return array
- */
- public function getItems()
- {
- $items = $this->getCheckout()->getQuoteShippingAddressesItems();
- /** @var \Magento\Framework\Filter\DataObject\Grid $itemsFilter */
- $itemsFilter = $this->_filterGridFactory->create();
- $itemsFilter->addFilter(new \Magento\Framework\Filter\Sprintf('%d'), 'qty');
- return $itemsFilter->filter($items);
- }
- /**
- * Retrieve HTML for addresses dropdown
- *
- * @param mixed $item
- * @param int $index
- * @return string
- */
- public function getAddressesHtmlSelect($item, $index)
- {
- $select = $this->getLayout()->createBlock(\Magento\Framework\View\Element\Html\Select::class)
- ->setName('ship[' . $index . '][' . $item->getQuoteItemId() . '][address]')
- ->setId('ship_' . $index . '_' . $item->getQuoteItemId() . '_address')
- ->setValue($item->getCustomerAddressId())
- ->setOptions($this->getAddressOptions());
- return $select->getHtml();
- }
- /**
- * Retrieve options for addresses dropdown
- *
- * @return array
- */
- public function getAddressOptions()
- {
- $options = $this->getData('address_options');
- if ($options === null) {
- $options = [];
- $addresses = [];
- try {
- $addresses = $this->customerRepository->getById($this->getCustomerId())->getAddresses();
- } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
- /** Customer does not exist */
- }
- /** @var \Magento\Customer\Api\Data\AddressInterface $address */
- foreach ($addresses as $address) {
- $label = $this->_addressConfig
- ->getFormatByCode(AddressConfig::DEFAULT_ADDRESS_FORMAT)
- ->getRenderer()
- ->renderArray($this->addressMapper->toFlatArray($address));
- $options[] = [
- 'value' => $address->getId(),
- 'label' => $label,
- ];
- }
- $this->setData('address_options', $options);
- }
- return $options;
- }
- /**
- * Retrieve active customer ID
- *
- * @return int|null
- */
- public function getCustomerId()
- {
- return $this->getCheckout()->getCustomerSession()->getCustomerId();
- }
- /**
- * @param mixed $item
- * @return string
- */
- public function getItemUrl($item)
- {
- return $this->getUrl('catalog/product/view/id/' . $item->getProductId());
- }
- /**
- * @param mixed $item
- * @return string
- */
- public function getItemDeleteUrl($item)
- {
- return $this->getUrl('*/*/removeItem', ['address' => $item->getQuoteAddressId(), 'id' => $item->getId()]);
- }
- /**
- * @return string
- */
- public function getPostActionUrl()
- {
- return $this->getUrl('*/*/addressesPost');
- }
- /**
- * @return string
- */
- public function getNewAddressUrl()
- {
- return $this->getUrl('*/checkout_address/newShipping');
- }
- /**
- * @return string
- */
- public function getBackUrl()
- {
- return $this->getUrl('checkout/cart/');
- }
- /**
- * @return bool
- */
- public function isContinueDisabled()
- {
- return !$this->getCheckout()->validateMinimumAmount();
- }
- }
|