Your IP : 216.73.216.43


Current Path : /home/rtorresani/www/app/code/Torresani/Consent/Plugin/Checkout/
Upload File :
Current File : //home/rtorresani/www/app/code/Torresani/Consent/Plugin/Checkout/LayoutProcessorPlugin.php

<?php
namespace Torresani\Consent\Plugin\Checkout;

class LayoutProcessorPlugin
{
    /**
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        array  $jsLayout
    ) {
        $isForAdult = false;
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $cart = $objectManager->get('\Magento\Checkout\Model\Cart');
        $items = $cart->getQuote()->getAllItems();

        foreach($items as $item) {
            $product = $objectManager->get('Magento\Catalog\Model\Product')->load($item->getProductId());
            if ($product->getData('indaco_general_isforadult')) {
                $isForAdult = true;
            }

        }

        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['custom-checkout-form-mandatory']['children']['privacy'] = [
            'component' => 'Magento_Ui/js/form/element/boolean',
            'config' => [
                'customScope' => 'shippingAddress.privacy',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/checkbox',
                'options' => [],
                'id' => 'privacy'
            ],
            'dataScope' => 'shippingAddress.privacy',
            'label' => __(''),
            'description' => __('Ho letto e compreso l’informativa privacy in merito al trattamento dei miei dati personali.'),
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' => ['required-entry' => true],
            'sortOrder' => 200,
            'id' => 'privacy'
        ];

        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['custom-checkout-form-mandatory']['children']['terms'] = [
            'component' => 'Magento_Ui/js/form/element/boolean',
            'config' => [
                'customScope' => 'shippingAddress.terms',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/checkbox',
                'options' => [],
                'id' => 'terms'
            ],
            'dataScope' => 'shippingAddress.terms',
            'label' => __(''),
            'description' => __('Accetto i termini e le condizioni di vendita.'),
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' => ['required-entry' => true],
            'sortOrder' => 300,
            'id' => 'terms'
        ];

        if ($isForAdult) {
            $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
            ['shippingAddress']['children']['custom-checkout-form-mandatory']['children']['adult'] = [
                'component' => 'Magento_Ui/js/form/element/boolean',
                'config' => [
                    'customScope' => 'shippingAddress.adult',
                    'template' => 'ui/form/field',
                    'elementTmpl' => 'ui/form/element/checkbox',
                    'options' => [],
                    'id' => 'adult'
                ],
                'dataScope' => 'shippingAddress.adult',
                'label' => __(''),
                'description' => __('Confermo di avere l\'età legale per l\'acquisto dei prodotti nel carrello'),
                'provider' => 'checkoutProvider',
                'visible' => true,
                'validation' => ['required-entry' => true],
                'sortOrder' => 400,
                'id' => 'adult'
            ];
        }

        $email = null;
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->get('Magento\Customer\Model\Session');
        if($customerSession->isLoggedIn()) {
            $email = $customerSession->getCustomer()->getEmail();
        }

        if ($email) {
            $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
            ['shippingAddress']['children']['custom-checkout-form']['children']['username'] = [
                'component' => 'Magento_Ui/js/form/element/abstract',
                'config' => [
                    'customScope' => 'shippingAddress.username',
                    'template' => 'ui/form/field',
                    'elementTmpl' => 'ui/form/element/hidden',
                    'options' => [],
                    'id' => 'useremail'
                ],
                'value' => $email,
                'dataScope' => 'shippingAddress.username',
                'label' => __(''),
                'description' => __('username'),
                'provider' => 'checkoutProvider',
                'visible' => true,
                'validation' => ['required-entry' => false],
                'sortOrder' => 950,
                'id' => 'username'
            ];
        }

        return $jsLayout;
    }
}