Results.php 4.95 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\Multishipping\Block\Checkout;

use Magento\Customer\Model\Address\Config as AddressConfig;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Session\SessionManagerInterface;
use Magento\Framework\View\Element\Template\Context;
use Magento\Multishipping\Model\Checkout\Type\Multishipping;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order\Address as OrderAddress;
use Magento\Theme\Block\Html\Title;

/**
 * Multi-shipping checkout results information
 *
 * @api
 * @since 100.2.1
 */
class Results extends Success
{
    /**
     * @var AddressConfig
     */
    private $addressConfig;

    /**
     * @var OrderRepositoryInterface
     */
    private $orderRepository;

    /**
     * @var SessionManagerInterface
     */
    private $session;

    /**
     * @param Context $context
     * @param Multishipping $multishipping
     * @param AddressConfig $addressConfig
     * @param OrderRepositoryInterface $orderRepository
     * @param SessionManagerInterface $session
     * @param array $data
     */
    public function __construct(
        Context $context,
        Multishipping $multishipping,
        AddressConfig $addressConfig,
        OrderRepositoryInterface $orderRepository,
        SessionManagerInterface $session,
        array $data = []
    ) {
        parent::__construct($context, $multishipping, $data);

        $this->addressConfig = $addressConfig;
        $this->orderRepository = $orderRepository;
        $this->session = $session;
    }

    /**
     * Returns shipping addresses from quote.
     *
     * @return array
     * @since 100.2.1
     */
    public function getQuoteShippingAddresses(): array
    {
        return $this->_multishipping->getQuote()->getAllShippingAddresses();
    }

    /**
     * Returns all failed addresses from quote.
     *
     * @return array
     * @since 100.2.1
     */
    public function getFailedAddresses(): array
    {
        $addresses = $this->getQuoteShippingAddresses();
        if ($this->getAddressError($this->getQuoteBillingAddress())) {
            $addresses[] = $this->getQuoteBillingAddress();
        }
        return $addresses;
    }

    /**
     * Retrieve order shipping address.
     *
     * @param int $orderId
     * @return OrderAddress|null
     * @since 100.2.1
     */
    public function getOrderShippingAddress(int $orderId)
    {
        return $this->orderRepository->get($orderId)->getShippingAddress();
    }

    /**
     * Retrieve quote billing address.
     *
     * @return QuoteAddress
     * @since 100.2.1
     */
    public function getQuoteBillingAddress(): QuoteAddress
    {
        return $this->getCheckout()->getQuote()->getBillingAddress();
    }

    /**
     * Returns formatted shipping address from placed order.
     *
     * @param OrderAddress $address
     * @return string
     * @since 100.2.1
     */
    public function formatOrderShippingAddress(OrderAddress $address): string
    {
        return $this->getAddressOneline($address->getData());
    }

    /**
     * Returns formatted shipping address from quote.
     *
     * @param QuoteAddress $address
     * @return string
     * @since 100.2.1
     */
    public function formatQuoteShippingAddress(QuoteAddress $address): string
    {
        return $this->getAddressOneline($address->getData());
    }

    /**
     * Checks if address type is shipping.
     *
     * @param QuoteAddress $address
     * @return bool
     * @since 100.2.1
     */
    public function isShippingAddress(QuoteAddress $address): bool
    {
        return $address->getAddressType() === QuoteAddress::ADDRESS_TYPE_SHIPPING;
    }

    /**
     * Get unescaped address formatted as one line string.
     *
     * @param array $address
     * @return string
     */
    private function getAddressOneline(array $address): string
    {
        $renderer = $this->addressConfig->getFormatByCode('oneline')->getRenderer();

        return $renderer->renderArray($address);
    }

    /**
     * Returns address error.
     *
     * @param QuoteAddress $address
     * @return string
     * @since 100.2.1
     */
    public function getAddressError(QuoteAddress $address): string
    {
        $errors = $this->session->getAddressErrors();

        return $errors[$address->getId()] ?? '';
    }

    /**
     * Add title to block head.
     *
     * @throws LocalizedException
     * @return Success
     * @since 100.2.1
     */
    protected function _prepareLayout(): Success
    {
        /** @var Title $pageTitle */
        $pageTitle = $this->getLayout()->getBlock('page.main.title');
        if ($pageTitle) {
            $title = $this->getOrderIds() ? $pageTitle->getPartlySuccessTitle() : $pageTitle->getFailedTitle();
            $pageTitle->setPageTitle($title);
        }

        return parent::_prepareLayout();
    }
}