0

I am trying to get all the information for each product in the cart at checkout so I can do a few things but I cannot get it to work. I have create the below module:

Block/Quote.php

namespace Module\Name\Block;

 class Quote extends \Magento\Framework\View\Element\Template
 {
     public function __construct(
         \Magento\Framework\View\Element\Template\Context $context,
         \Magento\Checkout\Model\Session $checkoutSession,
         array $data = []
     ) {
         $this->_checkoutSession = $checkoutSession;
         parent::__construct($context, $data);
     }

     /**
      * Get quote object associated with cart. By default it is current customer session quote
      *
      * @return \Magento\Quote\Model\Quote
      */
     public function getQuoteData()
     {
         $this->_checkoutSession->getQuote();
         if (!$this->hasData('quote')) {
             $this->setData('quote', $this->_checkoutSession->getQuote());
         }
         return $this->_getData('quote');
     }
 }

view/frontend/layout/checkout.index.index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceBlock name="checkout.root">
                <action method="setTemplate">
                    <argument name="template" xsi:type="string">Module_Name::onepage.phtml</argument>
                </action>
            </referenceBlock>
        </body>
    </page>

view/frontend/templates/onepage.phtml

<?php
// Get all visible items in cart
$quote = $block->getQuoteData();
// print_r($quote);

foreach($quote->getAllVisibleItems() as $_item) {
    // echo "<pre>";print_r($_item->debug());
    echo 'ID: '.$_item->getProductId().'<br/>';
    echo 'Name: '.$_item->getName().'<br/>';
    echo 'Sku: '.$_item->getSku().'<br/>';
    echo 'Quantity: '.$_item->getQty().'<br/>';
    echo 'Price: '.$_item->getPrice().'<br/>';
    echo 'Product Type: '.$_item->getProductType().'<br/>';
    echo 'Discount: '.$_item->getDiscountAmount();echo "<br/>";
    echo "<br/>";
}

// Get total items and total quantity in current cart
echo $totalItems    = $quote->getItemsCount();echo "<br/>";
echo $totalQuantity = $quote->getItemsQty();echo "<br/>";

//Get subtotal and grand total of customer current cart
echo $subTotal = $quote->getSubtotal();echo "<br/>";
echo $grandTotal = $quote->getGrandTotal();echo "<br/>";

//Get billing and shipping addresses of current cart
$billingAddress = $quote->getBillingAddress();
echo "<pre>";print_r($billingAddress->getData());
$shippingAddress = $quote->getShippingAddress();
echo "<pre>";print_r($billingAddress->getData());


?>
<div id="checkout" data-bind="scope:'checkout'" class="checkout-container">
    <div id="checkout-loader" data-role="checkout-loader" class="loading-mask" data-mage-init='{"checkoutLoader": {}}'>
        <div class="loader">
            <img src="<?= /* @escapeNotVerified */ $block->getViewFileUrl('images/loader-1.gif') ?>"
                 alt="<?= /* @escapeNotVerified */ __('Loading...') ?>"
                 style="position: absolute;">
        </div>
    </div>
    <!-- ko template: getTemplate() --><!-- /ko -->
    <script type="text/x-magento-init">
        {
            "#checkout": {
                "Magento_Ui/js/core/app": <?= /* @escapeNotVerified */ $block->getJsLayout() ?>
            }
        }
    </script>
    <script>
        window.checkoutConfig = <?= /* @escapeNotVerified */ $block->getSerializedCheckoutConfig() ?>;
        // Create aliases for customer.js model from customer module
        window.isCustomerLoggedIn = window.checkoutConfig.isCustomerLoggedIn;
        window.customerData = window.checkoutConfig.customerData;
    </script>
    <script>
        require([
            'mage/url',
            'Magento_Ui/js/block-loader'
        ], function(url, blockLoader) {
            blockLoader("<?= /* @escapeNotVerified */ $block->getViewFileUrl('images/loader-1.gif') ?>");
            return url.setBaseUrl('<?= /* @escapeNotVerified */ $block->getBaseUrl() ?>');
        })
    </script>
</div>

All I get is a blank page. Any idea what may be wrong?

Thanks, Stan

  • Why are you doing this destructive way? You can get this easily from JS. – Sohel Rana Apr 8 at 13:54
  • So why is this destructive? I basically need to see if a particular SKU is present at checkout and if it is display some fields. The fields are currently displaying and I am using CSS to display the fields according to SKU. I am open to suggestions but this should work. – stanhook Apr 8 at 16:36
1

Please update the below code in your module and hope it will work for you.

\Module\Name\view\frontend\layout\checkout_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.root">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Module_Name::onepage.phtml</argument>
            </action>
        </referenceBlock>
        <referenceBlock name="checkout.root">
            <arguments>
                <argument 
                    name="view_model" 
                    xsi:type="object">Module\Name\Block\Quote</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

Module\Name\view\frontend\templates\onepage.phtml: replace line number 11 and remove pre code

$quote = $block->getData("view_model")->getQuoteData();

Module\Name\Block\Quote.php

namespace Module\Name\Block;

use Magento\Framework\View\Element\Block\ArgumentInterface;


 class Quote implements ArgumentInterface
 {
     public function __construct(
         \Magento\Checkout\Model\Session $checkoutSession         
     ) {
         $this->_checkoutSession = $checkoutSession;
     }

     /**
      * Get quote object associated with cart. By default it is current customer session quote
      *
      * @return \Magento\Quote\Model\Quote
      */
     public function getQuoteData()
     {
         return $this->_checkoutSession->getQuote();
     }
 }

I have tested it is working for me and let me know if you are facing any issue with this.

Please accept this as an answer, so it helps to others.

| improve this answer | |
  • This worked perfect, thanks! – stanhook Apr 10 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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