13

I am Using Magento 2.1.

Want to show some custom shipping message for shipping method on checkout page, I have done it by editing directly to the knockout template file.

Magento_Checkout/web/template/shipping.html

but I want this setting on backend. So, is is possible to call a static block in knockout template file, if yes, how can I do it.

I want the contant in red box to come form static block.

enter image description here

2 Answers 2

29

You can create a module that will make your cms block message available to the KO template by adding to the checkout config.

In Your/Module/etc/frontend/di.xml we add a new config provider to the checkout config:

<?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">Your\Module\Model\ConfigProvider</item>
            </argument>
        </arguments>
    </type>
</config>

In Your/Module/Model/ConfigProvider.php we have the code that fetches the cms block's html:

<?php

namespace Your\Module\Model;


use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\View\LayoutInterface;

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

    public function __construct(LayoutInterface $layout)
    {
        $this->_layout = $layout;
    }

    public function getConfig()
    {
        $cmsBlockId = 1; // id of cms block to use

        return [
            'cms_block_message' => $this->_layout->createBlock('Magento\Cms\Block\Block')->setBlockId($cmsBlockId)->toHtml()
        ];
    }
}

Now you should overwrite the shipping.html KO template in your theme where you can display the cms block like so:

<div data-bind="html: window.checkoutConfig.cms_block_message"></div>

Note: if you want to use html tags that contain double quotations (for example an html a tag) in the static block you should escape the double quotations with a backslash. For example:

Accept our <a target=\"_blank\" href=\"/privacy-policy\">privacy policy</a>
6
  • Can I do same to append CMS block into minicart content.html? Commented Aug 10, 2017 at 7:01
  • For the minicart you will need to make a plugin in for getConfig method of \Magento\Checkout\Block\Cart\Sidebar. This method returns an array that is then passed to javascript as the window.checkout object (this occurs in Magento/Checkout/view/frontend/templates/cart/minicart.phtml). Commented Aug 10, 2017 at 22:43
  • @AaronAllen, it's working thank you for this post. Commented Dec 28, 2017 at 5:49
  • Can anyone say will this work for 2.3.3 as i don't see the cms static block content displayed? @Sarfaraj Sipai
    – Haerriz
    Commented Jan 2, 2020 at 10:32
  • Thank @AaronAllen, This have working on ver 2.3.3.
    – River
    Commented Jan 19, 2021 at 16:51
-6

I think you cant call static block in .html file you have to add that static block code in phtml file

Try the below path

Magento_Checkout/view/frontend/templates/onepage.phtml 

try to add the below code maintain by come css hacks

you have to change **static-block-id**


<div class ="static block" style="bottom: 127px; position: absolute;">
<?php   echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('static-block-id')->toHtml();   ?>
</div>

This is temporary solutions might be you will get some other good Answers

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.