0

I have Successfully Created a module for custom message on shipping method but i am not able to retrieve those data under the shipping method on checkout page, please help me.

Thanks in advance.enter image description here

1 Answer 1

0

add argument view_model inside block of your layout file

<arguments>
     <argument name="view_model" xsi:type="object">PackageName\ModuleName\ViewModel\CustomText</argument>
</arguments>

add viewModel CustomText.php

<?php
namespace PackageName\ModuleName\ViewModel;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\Element\Block\ArgumentInterface;

class CustomText implements ArgumentInterface
{
    const CUSTOM_COMMENT = 'custom_data/general/custom_message';
    /**
     * @var ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * @param ScopeConfigInterface $scopeConfig
     */
    public function __construct(
        ScopeConfigInterface $scopeConfig
    ) {
        $this->scopeConfig = $scopeConfig;
    }

    public function getCustomText($scope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
    {
        return $this->scopeConfig->getValue(self::CUSTOM_COMMENT, $scope);
    }
}

use viewModel in template(phtml) file

<?php
use PackageName\ModuleName\ViewModel\CustomText;
use Magento\Checkout\Block\Onepage; /** path of block class which you defined in layout.xml 
as example:
 <block class="Magento\Checkout\Block\Onepage" name="checkout.custom" template="Codilar_CustomCheckout::customMsg.phtml" cacheable="false">
       <arguments>
            <argument name="view_model" xsi:type="object">Codilar\CustomCheckout\ViewModel\CustomText</argument>
       </arguments>
 </block> this comment is just for explain the usage */
/** @var Onepage $block */
/** @var $viewModel CustomText */
$viewModel = $block->getViewModel();
$customText = $viewModel->getCustomText();
?>
<h1><?= $customText ?></h1>

I hope it will be Helpful

2
  • It's Not Working Manav Commented Dec 29, 2020 at 12:41
  • @HemantKumar i have updated the answer, please try now, also check sortOrder in your module's system.xml file Commented Jan 4, 2021 at 10:42

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.