Question

I have created a plugin to add custom field in checkout page, it was working when i give static text but i want to make the text label Dynamic, so i tried to use store config object in construct method, but when i define construct method inside plugin class. Plugin is not working.Can you please suggest where i am wrong.

Working Code:

<?php

namespace Sigma\Newslettersubscrb\Plugin\Checkout\Model\Checkout;

class LayoutProcessor
{

    /**
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    public function afterProcess(
    \Magento\Checkout\Block\Checkout\LayoutProcessor $subject, array $jsLayout
    )
    {
                $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
                ['shippingAddress']['children']['shipping-address-fieldset']['children']['delivery_date'] = [
            'component' => 'Magento_Ui/js/form/element/abstract',
            'config' => [
                'customScope' => 'shippingAddress',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/checkbox',
                'options' => [],
                'id' => 'delivery-date'
            ],
            'dataScope' => 'shippingAddress.delivery_date',
            'label' => 'Delivery Date',
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' => [],
            'sortOrder' => 250,
            'id' => 'delivery-date'
        ];

        return $jsLayout;
    }

Not Working Code:

<?php

namespace Sigma\Newslettersubscrb\Plugin\Checkout\Model\Checkout;

class LayoutProcessor
{

    protected $_objectManager;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $_logger;
    protected $_storeconfig;

    public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager, \Psr\Log\LoggerInterface $logger, \Magento\Store\Model\ScopeInterface $scopeInterface
    )
    {
        $this->_objectManager = $objectManager;
        $this->_logger = $logger;
        $this->_storeconfig = $scopeInterface;
    }

    /**
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    public function afterProcess(
    \Magento\Checkout\Block\Checkout\LayoutProcessor $subject, array $jsLayout
    )
    {
        //$helper = \Magento\Framework\App\ObjectManager::getInstance()->get('Sigma\Newslettersubscrb\Helper\Data');
        //$helper->getStoreConfig($node);
        //$this->helper->getConfig('Sigma\Newsletterubscrb\Helper\Data');
        //$label = $helper->getConfig('newsletter_general/newsletter_setting/enabled');
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
                ['shippingAddress']['children']['shipping-address-fieldset']['children']['delivery_date'] = [
            'component' => 'Magento_Ui/js/form/element/abstract',
            'config' => [
                'customScope' => 'shippingAddress',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/checkbox',
                'options' => [],
                'id' => 'delivery-date'
            ],
            'dataScope' => 'shippingAddress.delivery_date',
            'label' => 'Delivery Date',
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' => [],
            'sortOrder' => 250,
            'id' => 'delivery-date'
        ];

        return $jsLayout;
    }

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
        <plugin name="sr_add_custom_field" type="Sigma\Newslettersubscrb\Model\Plugin\LayoutProcessor" sortOrder="100"/>
    </type>
</config>
Was it helpful?

Solution

It happens when you have problem in your constructor.

Remove \Magento\Store\Model\ScopeInterface $scopeInterface from your constructor and use \Magento\Framework\App\Config\ScopeConfigInterface $scopeInterface instead of it.

Use the following to read your config:

$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
return $this->_storeconfig->getValue('my/data/path', $storeScope);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top