DEV Community

Paboda Hettiarachchi
Paboda Hettiarachchi

Posted on

Adding a static block in checkout

  • Create a CMS block with identifier set as "cms_block_identifier_notice"

  • In Vendor\Checkout\etc\frontend\di.xml

<type name="Magento\Checkout\Model\CompositeConfigProvider">
        <arguments>
            <argument name="configProviders" xsi:type="array">
                <item name="vendor_show_notice" xsi:type="object">Vendor\Checkout\Model\ConfigProvider</item>
            </argument>
        </arguments>
    </type>
<type name="Vendor\Checkout\Model\ConfigProvider">
        <arguments>
            <argument name="blockIdShowNotice" xsi:type="string">cms_block_identifier_notice</argument>
        </arguments>
    </type>
Enter fullscreen mode Exit fullscreen mode
  • In a Vendor/Checkout/Model/ConfigProvider.php
<?php
namespace Vendor\Checkout\Model;

use Magento\Framework\View\LayoutInterface;

class ConfigProvider implements ConfigProviderInterface
{
    /** @var LayoutInterface  */
    private $layout;

    /**
     * @var string
     */
    private $cmsBlock;

    /**
     * @param LayoutInterface $layout
     * @param $blockIdShowNotice
     */
    public function __construct (
        LayoutInterface $layout,
        $blockIdShowNotice
    ) {
        $this->layout = $layout;
        $this->cmsBlockNotice = $this->constructBlock($blockIdShowNotice);
    }

    /**
     * Get block data
     *
     * @param $blockId
     * @return mixed
     */
    public function constructBlock($blockId)
    {
        return $this->layout->createBlock('Magento\Cms\Block\Block')
            ->setBlockId($blockId)->toHtml();
    }

    /**
     * Pass config values to checkout
     *
     * @return array
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function getConfig()
    {
        $config = [];
        $config['cms_block_notice'] = $this->cmsBlockNotice;
        return $config;
    }
}
Enter fullscreen mode Exit fullscreen mode
  • In your .html file and the content will be displayed
<div data-bind="html: window.checkoutConfig.cms_block_notice"></div>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)