1

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>
6
  • Pls attach your di.xml Commented Jul 8, 2016 at 12:56
  • Edited the question with di.xml Riccardo
    – user2520
    Commented Jul 8, 2016 at 13:04
  • Just for a test: remove "\Magento\Store\Model\ScopeInterface" from your contructor. Commented Jul 8, 2016 at 13:17
  • yeah will try that and let you know Riccardo
    – user2520
    Commented Jul 8, 2016 at 13:18
  • Hi Riccardo, yes it is working when i remove scopeinterface. But i want to get the store config values, so i tried to add helper data of my custom module again not working...
    – user2520
    Commented Jul 8, 2016 at 13:32

1 Answer 1

1

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);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.