1

I tired to get the total qty of products added to cart and also the subtotal in a custom phtml on product page.

For that i use this code but without success:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 
$totalQuantity = $cart->getQuote()->getItemsCount();
$grandTotal = $cart->getQuote()->getGrandTotal();

and:

$helper = $this->helper('\Magento\Checkout\Helper\Cart');
echo $helper->getItemsCount();

Is there another solution?

2

This is how i solve this problem:

in my block:

use Magento\Checkout\Model\Cart as CustomerCart;

protected $cart;

public function __construct(

        CustomerCart $cart
        array $data = array()

    ) {

        $this->cart = $cart;

        parent::__construct($context);
    }



public function getProductQtyCustom(){

        $counting = $this->cart->getSummaryQty();
        return $counting;
    }


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

        $subtotal = $totals['subtotal']['value'];
        return $subtotal;
    }

after this in my phtml i used this funcitons like this $block->function();

| improve this answer | |
  • The Magento\Checkout\Model\Cart class is marked as deprecated, it's not recommended to use it. – Siarhey Uchukhlebau Sep 28 '17 at 12:57
  • thank you for the infromation. I tried to solve in manny ways but this was the only way that was working – Alin Lupoiu Sep 28 '17 at 13:39
  • Why does my method not suitable for you? It almost the same as the method you use, except using a quote address. – Siarhey Uchukhlebau Sep 28 '17 at 13:43
  • i receive 0 all the time with your method, and i don't know why. But thank you any way! – Alin Lupoiu Sep 28 '17 at 13:45
  • Where you are trying to get this info? Is it event or plugin on something? – Siarhey Uchukhlebau Sep 28 '17 at 13:46
0

Update your block:

namespace Vendor\Module\Block;

class CustomBlock extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $checkoutSession;

    /**
     * Form constructor.
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Checkout\Model\Session $checkoutSession
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->checkoutSession = $checkoutSession;
    }

    /**
     * @param int $itemId
     * @return float|int
     */
    public function getItemsQty($itemId = 0) // 0 for all items
    {
        return $this->getActiveQuoteAddress()->getItemQty($itemId);
    }

    /**
     * @return float
     */
    public function getSubtotal()
    {
        return $this->getActiveQuoteAddress()->getBaseSubtotal(); // or any other type of subtotal like subtotal incl tax etc.
    }

    /**
     * @return \Magento\Quote\Model\Quote\Address
     */
    protected function getActiveQuoteAddress()
    {
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $this->checkoutSession->getQuote();
        if ($quote->isVirtual()) {
            return $quote->getBillingAddress();
        }

        return $quote->getShippingAddress();
    }
}

and use this methods in the template (without using object manager directly):

echo $block->getSubtotal();
echo $block->getItemsQty(();
| improve this answer | |
-1

use this in your block by this you don't have to make your cache false in your XML file and also don't have to use object manager easy pese i have used this at my checkout page

i am using session instead of cart because session has the same data as in cart and by using cart we nedd to to make cache false by this method we don't have to do that

block/backend.php

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

public function Summarydata()
{
   return $this->_checkoutSession;
}

and in phtml file templete/filename.phtml use quote to get attribute

<?php
$quote = $block->Summarydata()->getQuote();
$subTotal = $quote->getSubtotal();
?>
<b>Total points by this order : </b><?php echo $subTotal ?>

and your layoutfile checkout_cat_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>
        <referenceContainer name="cart.summary">
            <block name="cart-total" class="vendor\modulename\Block\Backend"
                   template="filename.phtml" before="checkout.cart.methods.bottom" />
        </referenceContainer>
    </body>
</page>
| improve this answer | |

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.