Question

Magento 2 Enterprise,

With Magento Reward module, Inside layout file checkout_cart_index.xml file with reward block is defined as below.

<block class="Magento\Reward\Block\Tooltip\Checkout" name="reward.tooltip.checkout" template="tooltip.phtml">
    <arguments>
        <argument name="reward_type" xsi:type="string">Magento\Reward\Model\Action\OrderExtra</argument>
    </arguments>
    <action method="setWrapperClass">
        <argument name="class" xsi:type="string">reward-checkout</argument>
    </action>
    <action method="setRewardMessage">
        <argument translate="true" name="message" xsi:type="string">Check out now and earn %1 for this order.</argument>
    </action>
    <action method="setIsGuestNote">
        <argument name="value" xsi:type="string">1</argument>
    </action>
</block>

I want to this reward blocks html content into Checkout module Customerdata/Cart.php file,

How can i call above block content into

vendor/magento/module-checkout/CustomerData/Cart.php file with below function,

public function getSectionData()
{
    $totals = $this->getQuote()->getTotals();      
    return [
        'summary_count' => $this->getSummaryCount(),                        
        'subtotal' => isset($totals['subtotal'])
            ? $this->checkoutHelper->formatPrice($totals['subtotal']->getValue())
            : 0,
        'possible_onepage_checkout' => $this->isPossibleOnepageCheckout(),
        'items' => $this->getRecentItems(),
        'extra_actions' => $this->layout->createBlock('Magento\Catalog\Block\ShortcutButtons')->toHtml(),
        'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(),
        'rewards' => $this->layout->createBlock('Magento\Reward\Block\Tooltip\Checkout','reward.tooltip.checkout')    
                  ->toHtml(),
    ];
}

I have try to call this block using,

'rewards' => $this->layout->createBlock('Magento\Reward\Block\Tooltip\Checkout','reward.tooltip.checkout')->toHtml(),

But rewards always return null

In same calling in minicart.phtml file,

$this->getLayout()->createBlock('Magento\Reward\Block\Tooltip\Checkout','reward.tooltip.checkout')->toHtml()

Result,

Check out now and earn 100 Reward points for this order. Learn more This applies only to registered users and may vary when a user is logged in.

In above function with rewards argument, I want result of reward.toottip.checkout phtml content using above way.

How to resolve this issue in magento 2?

Was it helpful?

Solution 2

Just Solved issue using pass parent class construct dependency.

Just Pass ParentObject construct dependency inside createBlock() method and its working fine for getting html content of template file.

public function getSectionData()
    {
        $totals = $this->getQuote()->getTotals();

        //for rewards point
        $rewards = $this->layout->createBlock('Magento\Reward\Block\Tooltip\Checkout','reward.tooltip.checkout',
            [
                'data' => ['reward_type' => 'Magento\Reward\Model\Action\OrderExtra',
                           'rewardHelper' => 'Magento\Reward\Helper\Data',
                           'customerSession' => 'Magento\Customer\Model\Session',
                           'rewardInstance' => 'Magento\Reward\Model\Reward',
                           'storeManager' => 'Magento\Store\Model\StoreManager'
                          ]
            ]);
        $rewards->setWrapperClass('reward-checkout')->setRewardMessage("Check out now and earn %1 for this order.")->setIsGuestNote(1);
        $rewards->setTemplate('Magento_Reward::tooltip.phtml'); 

        return [
            'summary_count' => $this->getSummaryCount(),                        
            'subtotal' => isset($totals['subtotal'])
                ? $this->checkoutHelper->formatPrice($totals['subtotal']->getValue())
                : 0,
            'possible_onepage_checkout' => $this->isPossibleOnepageCheckout(),
            'items' => $this->getRecentItems(),
            'extra_actions' => $this->layout->createBlock('Magento\Catalog\Block\ShortcutButtons')->toHtml(),
            'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(),
            'rewards' => $rewards->toHtml(),
        ];
    }

OTHER TIPS

Change function as below in vendor/magento/module-checkout/CustomerData/Cart.php

public function getSectionData()
{
    $totals = $this->getQuote()->getTotals();      
    $rewards = $this->getLayout()->createBlock(
            'Magento\Reward\Block\Tooltip\Checkout','reward.tooltip.checkout'
        );
        $rewards->setTemplate('Magento_Reward::tooltip.phtml');         

    return [
        'summary_count' => $this->getSummaryCount(),                        
        'subtotal' => isset($totals['subtotal'])
            ? $this->checkoutHelper->formatPrice($totals['subtotal']->getValue())
            : 0,
        'possible_onepage_checkout' => $this->isPossibleOnepageCheckout(),
        'items' => $this->getRecentItems(),
        'extra_actions' => $this->layout->createBlock('Magento\Catalog\Block\ShortcutButtons')->toHtml(),
        'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(),
        'rewards' => $rewards->toHtml(),
    ];
}

Please have a try, I have not tested

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top