| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Copyright © 2021 O2TI. All rights reserved. |
||
| 4 | * |
||
| 5 | * @author Bruno Elisei <[email protected]> |
||
| 6 | * See LICENSE.txt for license details. |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace O2TI\ThemeFullCheckout\Plugin; |
||
| 10 | |||
| 11 | use Magento\Checkout\Block\Checkout\LayoutProcessor; |
||
| 12 | use O2TI\ThemeFullCheckout\Helper\Config; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * ThemeFullCheckoutLayoutPlugin - Change Components. |
||
| 16 | */ |
||
| 17 | class ThemeFullCheckoutLayoutPlugin |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var Config |
||
| 21 | */ |
||
| 22 | private $config; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param Config $config |
||
| 26 | */ |
||
| 27 | public function __construct( |
||
| 28 | Config $config |
||
| 29 | ) { |
||
| 30 | $this->config = $config; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Change Components in Create Account. |
||
| 35 | * |
||
| 36 | * @param array $jsLayout |
||
| 37 | * |
||
| 38 | * @return array |
||
| 39 | */ |
||
| 40 | public function changeCreateAccount(array $jsLayout): ?array |
||
| 41 | { |
||
| 42 | if (isset($jsLayout['components']['checkout']['children']['steps']['children']['identification-step'])) { |
||
| 43 | // phpcs:ignore |
||
| 44 | $createAccountFields = &$jsLayout['components']['checkout']['children']['steps']['children']['identification-step']['children']['identification']['children']['createAccount']['children']['create-account-fieldset']['children']; |
||
| 45 | |||
| 46 | $createAccountFields = $this->changeFields($createAccountFields); |
||
| 47 | // phpcs:ignore |
||
| 48 | $jsLayout['components']['checkout']['children']['steps']['children']['identification-step']['children']['identification']['template'] = 'O2TI_ThemeFullCheckout/O2TI/CheckoutIdentificationStep/form/step/identification'; |
||
| 49 | } |
||
| 50 | |||
| 51 | return $jsLayout; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Change Components in Shipping. |
||
| 56 | * |
||
| 57 | * @param array $jsLayout |
||
| 58 | * |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | public function changeShippingFields(array $jsLayout): ?array |
||
| 62 | { |
||
| 63 | if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step'])) { |
||
| 64 | // phpcs:ignore |
||
| 65 | $shippingFields = &$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']; |
||
| 66 | $shippingFields = $this->changeFields($shippingFields); |
||
| 67 | } |
||
| 68 | |||
| 69 | return $jsLayout; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Change Components in Billing. |
||
| 74 | * |
||
| 75 | * @param array $jsLayout |
||
| 76 | * |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | public function changeBillingFields(array $jsLayout): array |
||
| 80 | { |
||
| 81 | // phpcs:ignore |
||
| 82 | foreach ($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'] as &$payment) { |
||
| 83 | if (isset($payment['children']['form-fields'])) { |
||
| 84 | $billingFields = &$payment['children']['form-fields']['children']; |
||
| 85 | $billingFields = $this->changeFields($billingFields); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | // phpcs:ignore |
||
| 89 | if (isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['afterMethods']['children']['billing-address-form'])) { |
||
| 90 | // phpcs:ignore |
||
| 91 | $billingAddressOnPage = &$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['afterMethods']['children']['billing-address-form']['children']['form-fields']['children']; |
||
| 92 | $billingAddressOnPage = $this->changeFields($billingAddressOnPage); |
||
| 93 | } |
||
| 94 | |||
| 95 | return $jsLayout; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Change Components in Fields. |
||
| 100 | * |
||
| 101 | * @param array $fields |
||
| 102 | * |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | public function changeFields(array $fields): array |
||
| 106 | { |
||
| 107 | foreach ($fields as $key => $data) { |
||
| 108 | if ($key === 'street') { |
||
| 109 | if (array_key_exists('fieldTemplate', $fields[$key]['config'])) { |
||
| 110 | if ($fields[$key]['config']['fieldTemplate'] === 'O2TI_AdvancedStreetAddress/form/field') { |
||
| 111 | $fields[$key]['config']['fieldTemplate'] = 'O2TI_ThemeFullCheckout/form/field'; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | if ($key === 'password') { |
||
| 116 | if (array_key_exists('elementTmpl', $fields[$key]['config'])) { |
||
| 117 | // phpcs:ignore |
||
| 118 | $fields[$key]['config']['elementTmpl'] = 'O2TI_ThemeFullCheckout/O2TI/CheckoutIdentificationStep/form/element/password'; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | return $fields; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Move Components in Address Billing. |
||
| 128 | * |
||
| 129 | * @param array $jsLayout |
||
| 130 | * |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | public function moveAddressBilling(array $jsLayout): array |
||
| 134 | { |
||
| 135 | // phpcs:ignore |
||
| 136 | if (isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['afterMethods']['children']['billing-address-form'])) { |
||
| 137 | // phpcs:ignore |
||
| 138 | $billingAddressOnPage = &$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['afterMethods']['children']['billing-address-form']; |
||
| 139 | // phpcs:ignore |
||
| 140 | $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['beforeMethods']['children']['billing-address-form'] = $billingAddressOnPage; |
||
| 141 | // phpcs:ignore |
||
| 142 | unset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['afterMethods']['children']['billing-address-form']); |
||
| 143 | } |
||
| 144 | |||
| 145 | return $jsLayout; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Move Components Discount. |
||
| 150 | * |
||
| 151 | * @param array $jsLayout |
||
| 152 | * |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | public function moveDiscountComponent(array $jsLayout): array |
||
| 156 | { |
||
| 157 | // phpcs:ignore |
||
| 158 | $discount = $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['afterMethods']['children']['discount']; |
||
| 159 | // phpcs:ignore |
||
| 160 | $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['beforeMethods']['children']['discount'] = $discount; |
||
| 161 | // phpcs:ignore |
||
| 162 | unset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['afterMethods']['children']['discount']); |
||
| 163 | |||
| 164 | return $jsLayout; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Implement elements in sidebar. |
||
| 169 | * |
||
| 170 | * @param array $jsLayout |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | public function changeSidebar(array $jsLayout): array |
||
| 175 | { |
||
| 176 | $newSidebar['components']['checkout']['children']['sidebar']['children']['summary'] = [ |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 177 | 'children' => [ |
||
| 178 | 'cart_items' => [ |
||
| 179 | 'sortOrder' => 10, |
||
| 180 | ], |
||
| 181 | 'discount' => [ |
||
| 182 | 'sortOrder' => 20, |
||
| 183 | 'component' => 'Magento_SalesRule/js/view/payment/discount', |
||
| 184 | 'children' => [ |
||
| 185 | 'errors' => [ |
||
| 186 | 'sortOrder' => 0, |
||
| 187 | 'component' => 'Magento_SalesRule/js/view/payment/discount-messages', |
||
| 188 | 'displayArea' => 'messages', |
||
| 189 | ], |
||
| 190 | ], |
||
| 191 | ], |
||
| 192 | 'totals' => [ |
||
| 193 | 'config' => [ |
||
| 194 | 'sortOrder' => 30, |
||
| 195 | ], |
||
| 196 | ], |
||
| 197 | ], |
||
| 198 | ]; |
||
| 199 | // phpcs:ignore |
||
| 200 | if ($jsLayout['components']['checkout']['children']['sidebar']['config']['template'] === 'Magento_Checkout/sidebar') { |
||
| 201 | // phpcs:ignore |
||
| 202 | $jsLayout['components']['checkout']['children']['sidebar']['config']['template'] = 'O2TI_ThemeFullCheckout/sidebar'; |
||
| 203 | // phpcs:ignore |
||
| 204 | } elseif ($jsLayout['components']['checkout']['children']['sidebar']['config']['template'] === 'O2TI_CheckoutIdentificationStep/sidebar') { |
||
| 205 | // phpcs:ignore |
||
| 206 | $jsLayout['components']['checkout']['children']['sidebar']['config']['template'] = 'O2TI_ThemeFullCheckout/O2TI/CheckoutIdentificationStep/sidebar'; |
||
| 207 | // phpcs:ignore |
||
| 208 | unset($jsLayout['components']['checkout']['children']['sidebar']['children']['identification-information']['config']['template']); |
||
| 209 | // phpcs:ignore |
||
| 210 | $jsLayout['components']['checkout']['children']['sidebar']['children']['identification-information']['config']['template'] = 'O2TI_ThemeFullCheckout/O2TI/CheckoutIdentificationStep/identification-information'; |
||
| 211 | } |
||
| 212 | |||
| 213 | return array_merge_recursive($jsLayout, $newSidebar); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Change Components Template ReCaptcha. |
||
| 218 | * |
||
| 219 | * @param array $jsLayout |
||
| 220 | * |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | public function changeTemplateReCaptcha(array $jsLayout): ?array |
||
| 224 | { |
||
| 225 | // phpcs:ignore |
||
| 226 | $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['beforeMethods']['children']['place-order-recaptcha-container']['template'] = 'O2TI_ThemeFullCheckout/ReCaptchaCheckout/payment-recaptcha-container'; |
||
| 227 | |||
| 228 | return $jsLayout; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Select Components for Change. |
||
| 233 | * |
||
| 234 | * @param LayoutProcessor $layoutProcessor |
||
| 235 | * @param callable $proceed |
||
| 236 | * @param array $args |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | public function aroundProcess(LayoutProcessor $layoutProcessor, callable $proceed, array $args): array |
||
| 241 | { |
||
| 242 | $jsLayout = $proceed($args); |
||
| 243 | if ($this->config->isEnabled()) { |
||
| 244 | $jsLayout = $this->changeCreateAccount($jsLayout); |
||
| 245 | $jsLayout = $this->changeShippingFields($jsLayout); |
||
| 246 | $jsLayout = $this->changeBillingFields($jsLayout); |
||
| 247 | $jsLayout = $this->changeSidebar($jsLayout); |
||
| 248 | $jsLayout = $this->moveDiscountComponent($jsLayout); |
||
| 249 | if ($this->config->isMoveAddressBilling()) { |
||
| 250 | $jsLayout = $this->moveAddressBilling($jsLayout); |
||
| 251 | } |
||
| 252 | $jsLayout = $this->changeTemplateReCaptcha($jsLayout); |
||
| 253 | $layoutProcessor = $layoutProcessor; |
||
|
0 ignored issues
–
show
|
|||
| 254 | } |
||
| 255 | |||
| 256 | return $jsLayout; |
||
| 257 | } |
||
| 258 | } |
||
| 259 |