4

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?

2 Answers 2

2

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

8
  • Return null using above case. Dec 14, 2016 at 13:05
  • Please confirm Magento_Reward module name and change it if different Dec 14, 2016 at 13:13
  • yes i have checked this is correct module name but always return null. I have worked on enterprise edition. Dec 14, 2016 at 13:16
  • Just try any default magento block and get content is not working. Dec 14, 2016 at 13:28
  • Please change $this->getLayout()-> to $this->layout-> Dec 14, 2016 at 13:34
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(),
        ];
    }

Your Answer

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

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