Bhaveshpp

Professional Magento Developr - Healp eachother to grow

Magento2: Add custom value in checkout config

28 Aug 2021 » magento2

define \app\code\Bhaveshpp\ShippingRestriction\etc\frontend\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\Model\CompositeConfigProvider">
       <arguments>
           <argument name="configProviders" xsi:type="array">
               <item name="cms_block_config_provider" xsi:type="object">Bhaveshpp\ShippingRestriction\Model\ConfigProvider</item>
           </argument>
       </arguments>
   </type>
</config>

Add setup config value \app\code\Bhaveshpp\ShippingRestriction\Model\ConfigProvider.php

<?php 
namespace Bhaveshpp\ShippingRestriction\Model;
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\View\LayoutInterface;

/**
 * Class ConfigProvider
 */
class ConfigProvider implements ConfigProviderInterface
{
    /** @var LayoutInterface  */
    protected $_layout;

    /** @var Check */
    protected $_checkModel;
    
    /**
     * initialize
     *
     * @param LayoutInterface $layout
     * @param Check $checkModel
     */
    public function __construct(
        LayoutInterface $layout,
        Check $checkModel
    )
    {
        $this->_checkModel = $checkModel;
        $this->_layout = $layout;
    }
    
    /**
     * return config value
     *
     * @return array
     */
    public function getConfig()
    {
        $cmsBlockId = $this->_checkModel->scopConfig->getValue(Check::CONFIG_CHECKOUT_PAGE_NOTE);
        $block = $this->_layout->createBlock('Magento\Cms\Block\Block')
           ->setBlockId($cmsBlockId)->toHtml();
        $flag = ($this->_checkModel->getDateCondition() && $this->_checkModel->checkCartItems());
        return [
            'shipping_restricted' => [
                "is_restricted" => $flag,
                "cms_block" => $block

            ]
        ];
    }
}

use config value \app\design\frontend\MageBig\martfury\child\Magento_Checkout\web\template\shipping.html

<div if="window.checkoutConfig.shipping_restricted.is_restricted">
    <div data-bind="html: window.checkoutConfig.shipping_restricted.cms_block"></div>
</div>