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.
2 Answers 2
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>
-
Can I do same to append CMS block into minicart content.html?Ronak Chauhan– Ronak Chauhan2017年08月10日 07:01:34 +00:00Commented Aug 10, 2017 at 7:01
-
For the minicart you will need to make a plugin in for
getConfigmethod of\Magento\Checkout\Block\Cart\Sidebar. This method returns an array that is then passed to javascript as thewindow.checkoutobject (this occurs inMagento/Checkout/view/frontend/templates/cart/minicart.phtml).Aaron Allen– Aaron Allen2017年08月10日 22:43:43 +00:00Commented Aug 10, 2017 at 22:43 -
@AaronAllen, it's working thank you for this post.Sarfaraj Sipai– Sarfaraj Sipai2017年12月28日 05:49:01 +00:00Commented 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 SipaiHaerriz– Haerriz2020年01月02日 10:32:03 +00:00Commented Jan 2, 2020 at 10:32
-
Thank @AaronAllen, This have working on ver 2.3.3.River– River2021年01月19日 16:51:04 +00:00Commented Jan 19, 2021 at 16:51
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