|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Grasch\CheckoutLoginStep\Block\Checkout; |
| 5 | + |
| 6 | +use Grasch\CheckoutLoginStep\Helper\Data as Helper; |
| 7 | +use Magento\Checkout\Block\Checkout\LayoutProcessorInterface; |
| 8 | +use Magento\Customer\Model\Context as CustomerContext; |
| 9 | +use Magento\Customer\Model\Url; |
| 10 | +use Magento\Framework\App\Http\Context; |
| 11 | +use Magento\Framework\Stdlib\ArrayManager; |
| 12 | +use Magento\Framework\Url\Helper\Data; |
| 13 | + |
| 14 | +class LoginStepProcessor implements LayoutProcessorInterface |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var ArrayManager |
| 18 | + */ |
| 19 | + private ArrayManager $arrayManager; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var Context |
| 23 | + */ |
| 24 | + private Context $httpContext; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected string $childrenStepsPath = 'components/checkout/children/steps'; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + protected string $customerEmailPath = 'components/checkout/children/steps' . |
| 35 | + '/children/shipping-step/children/shippingAddress/children/customer-email'; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var string |
| 39 | + */ |
| 40 | + protected string $loginFormPath = 'components/checkout/children/steps/children/' . |
| 41 | + 'login-step/children/login/children/login-form'; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var Url |
| 45 | + */ |
| 46 | + private Url $customerUrl; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var Data |
| 50 | + */ |
| 51 | + private Data $coreUrl; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var Helper |
| 55 | + */ |
| 56 | + private Helper $helper; |
| 57 | + |
| 58 | + /** |
| 59 | + * @param ArrayManager $arrayManager |
| 60 | + * @param Context $httpContext |
| 61 | + * @param Url $customerUrl |
| 62 | + * @param Data $coreUrl |
| 63 | + * @param Helper $helper |
| 64 | + */ |
| 65 | + public function __construct( |
| 66 | + ArrayManager $arrayManager, |
| 67 | + Context $httpContext, |
| 68 | + Url $customerUrl, |
| 69 | + Data $coreUrl, |
| 70 | + Helper $helper |
| 71 | + ) { |
| 72 | + $this->arrayManager = $arrayManager; |
| 73 | + $this->httpContext = $httpContext; |
| 74 | + $this->customerUrl = $customerUrl; |
| 75 | + $this->coreUrl = $coreUrl; |
| 76 | + $this->helper = $helper; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @inheritdoc |
| 81 | + */ |
| 82 | + public function process($jsLayout): array |
| 83 | + { |
| 84 | + if (!$this->helper->isEnabled()) { |
| 85 | + return $jsLayout; |
| 86 | + } |
| 87 | + |
| 88 | + if ($this->httpContext->getValue(CustomerContext::CONTEXT_AUTH)) { |
| 89 | + return $jsLayout; |
| 90 | + } |
| 91 | + |
| 92 | + if (!$this->arrayManager->exists($this->childrenStepsPath, $jsLayout)) { |
| 93 | + return $jsLayout; |
| 94 | + } |
| 95 | + if (!$this->arrayManager->exists($this->customerEmailPath, $jsLayout)) { |
| 96 | + return $jsLayout; |
| 97 | + } |
| 98 | + |
| 99 | + $jsLayout = $this->createLoginStepComponents($jsLayout); |
| 100 | + $jsLayout = $this->createLoginFormComponent($jsLayout); |
| 101 | + |
| 102 | + return $jsLayout; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Create login form component |
| 107 | + * |
| 108 | + * @param array $jsLayout |
| 109 | + * @return array |
| 110 | + */ |
| 111 | + protected function createLoginFormComponent(array $jsLayout): array |
| 112 | + { |
| 113 | + $component = array_merge( |
| 114 | + $this->arrayManager->get($this->customerEmailPath, $jsLayout), |
| 115 | + [ |
| 116 | + 'component' => 'Grasch_CheckoutLoginStep/js/view/form/login', |
| 117 | + 'template' => 'Grasch_CheckoutLoginStep/form/login', |
| 118 | + 'displayArea' => 'login-form' |
| 119 | + ] |
| 120 | + ); |
| 121 | + |
| 122 | + $jsLayout = $this->arrayManager->set($this->loginFormPath, $jsLayout, []); |
| 123 | + $jsLayout = $this->arrayManager->merge( |
| 124 | + $this->loginFormPath, |
| 125 | + $jsLayout, |
| 126 | + $component |
| 127 | + ); |
| 128 | + |
| 129 | + return $jsLayout; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Create login step components |
| 134 | + * |
| 135 | + * @param array $jsLayout |
| 136 | + * @return array |
| 137 | + */ |
| 138 | + protected function createLoginStepComponents(array $jsLayout): array |
| 139 | + { |
| 140 | + $createAccountComponent = [ |
| 141 | + 'component' => 'uiComponent', |
| 142 | + 'template' => 'Grasch_CheckoutLoginStep/login/create-account', |
| 143 | + 'displayArea' => 'create-account', |
| 144 | + 'createAccountUrl' => $this->getCreateAccountUrl() |
| 145 | + ]; |
| 146 | + $proceedWithoutLoginComponent = [ |
| 147 | + 'component' => 'uiComponent', |
| 148 | + 'template' => 'Grasch_CheckoutLoginStep/login/proceed-without-login', |
| 149 | + 'displayArea' => 'proceed-without-login' |
| 150 | + ]; |
| 151 | + |
| 152 | + return $this->arrayManager->merge( |
| 153 | + $this->childrenStepsPath . '/children', |
| 154 | + $jsLayout, |
| 155 | + [ |
| 156 | + 'login-step' => [ |
| 157 | + 'component' => 'uiComponent', |
| 158 | + 'sortOrder' => 0, |
| 159 | + 'children' => [ |
| 160 | + 'login' => [ |
| 161 | + 'component' => 'Grasch_CheckoutLoginStep/js/view/login', |
| 162 | + 'template' => 'Grasch_CheckoutLoginStep/login', |
| 163 | + 'sortOrder' => 0, |
| 164 | + 'config' => [ |
| 165 | + 'deps' => [ |
| 166 | + 'checkout.sidebar.summary' |
| 167 | + ] |
| 168 | + ], |
| 169 | + 'children' => [ |
| 170 | + 'create-account' => $createAccountComponent, |
| 171 | + 'proceed-without-login' => $proceedWithoutLoginComponent |
| 172 | + ] |
| 173 | + ] |
| 174 | + ] |
| 175 | + ] |
| 176 | + ] |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Get create account url |
| 182 | + * |
| 183 | + * @return string |
| 184 | + */ |
| 185 | + private function getCreateAccountUrl(): string |
| 186 | + { |
| 187 | + $url = $this->customerUrl->getRegisterUrl(); |
| 188 | + $url = $this->coreUrl->addRequestParam($url, ['context' => 'checkout']); |
| 189 | + |
| 190 | + return $url; |
| 191 | + } |
| 192 | +} |
0 commit comments