Magento 2 get cart items in block class The Next CEO of Stack OverflowContent block not showingMagento 2 RC - Invalid block typeAdding a css class / div wrapper to a blockBlock Template not LoadingMagento 2 Mini Cart Plugin di.xml doesn't pass attribute informationMagento2: How to show subtotal in minicartConditionally Add Body Class Within Module Block?Magento 2 in minicart popup need qty incrementer and decrementer$block Class Some Method is won't appear in get_class_methods() call

Expressing the idea of having a very busy time

Yu-Gi-Oh cards in Python 3

Inexact numbers as keys in Association?

Which one is the true statement?

Easy to read palindrome checker

Is there a reasonable and studied concept of reduction between regular languages?

Is it ever safe to open a suspicious HTML file (e.g. email attachment)?

What was Carter Burke's job for "the company" in Aliens?

Is it ok to trim down a tube patch?

Why do we say 'Un seul M' and not 'Une seule M' even though M is a "consonne"

Help! I cannot understand this game’s notations!

How to use ReplaceAll on an expression that contains a rule

Is there an equivalent of cd - for cp or mv

Is it professional to write unrelated content in an almost-empty email?

Towers in the ocean; How deep can they be built?

A question about free fall, velocity, and the height of an object.

Does destroying a Lich's phylactery destroy the soul within it?

Is it okay to majorly distort historical facts while writing a fiction story?

How did Beeri the Hittite come up with naming his daughter Yehudit?

(How) Could a medieval fantasy world survive a magic-induced "nuclear winter"?

Why don't programming languages automatically manage the synchronous/asynchronous problem?

What is the process for cleansing a very negative action

Why am I getting "Static method cannot be referenced from a non static context: String String.valueOf(Object)"?

Small nick on power cord from an electric alarm clock, and copper wiring exposed but intact



Magento 2 get cart items in block class



The Next CEO of Stack OverflowContent block not showingMagento 2 RC - Invalid block typeAdding a css class / div wrapper to a blockBlock Template not LoadingMagento 2 Mini Cart Plugin di.xml doesn't pass attribute informationMagento2: How to show subtotal in minicartConditionally Add Body Class Within Module Block?Magento 2 in minicart popup need qty incrementer and decrementer$block Class Some Method is won't appear in get_class_methods() call










0















I have created block for sidebar.



I have added below code in my block for compare products. It's working fine



<div class="block widget block-compare">
<div class="block-title">
</div>
block class="MagentoFrameworkViewElementTemplate" name="catalog.compare.sidebar" template="Magento_Catalog::product/compare/sidebar.phtml" /
</div>


I got output like this: http://prntscr.com/n4jcsj



Now i want to show shopping cart items there above compare. Can anyone help me with this.



I also try with minicart but it is not working as per my requirement. It gives me only link of minicart. I need to show content of minicart. I used this code in block.



<div class="minicart">
<div>
block class="MagentoCheckoutBlockCartSidebar" name="mini_cart" template="Magento_Checkout::cart/minicart.phtml"
</div>
</div>


Which gives me output like this : https://prnt.sc/n3h35j



Please help me if anyone has some idea about this.










share|improve this question
























  • But i want to add it in block.

    – Khooshbu Patel
    2 days ago











  • means i don't understand

    – Rakesh Donga
    2 days ago











  • Can we add this code in static block ?

    – Khooshbu Patel
    2 days ago












  • Yes, exactly same

    – Khooshbu Patel
    2 days ago











  • I added this code in static block and got this output : prntscr.com/n4jw9r

    – Khooshbu Patel
    2 days ago















0















I have created block for sidebar.



I have added below code in my block for compare products. It's working fine



<div class="block widget block-compare">
<div class="block-title">
</div>
block class="MagentoFrameworkViewElementTemplate" name="catalog.compare.sidebar" template="Magento_Catalog::product/compare/sidebar.phtml" /
</div>


I got output like this: http://prntscr.com/n4jcsj



Now i want to show shopping cart items there above compare. Can anyone help me with this.



I also try with minicart but it is not working as per my requirement. It gives me only link of minicart. I need to show content of minicart. I used this code in block.



<div class="minicart">
<div>
block class="MagentoCheckoutBlockCartSidebar" name="mini_cart" template="Magento_Checkout::cart/minicart.phtml"
</div>
</div>


Which gives me output like this : https://prnt.sc/n3h35j



Please help me if anyone has some idea about this.










share|improve this question
























  • But i want to add it in block.

    – Khooshbu Patel
    2 days ago











  • means i don't understand

    – Rakesh Donga
    2 days ago











  • Can we add this code in static block ?

    – Khooshbu Patel
    2 days ago












  • Yes, exactly same

    – Khooshbu Patel
    2 days ago











  • I added this code in static block and got this output : prntscr.com/n4jw9r

    – Khooshbu Patel
    2 days ago













0












0








0








I have created block for sidebar.



I have added below code in my block for compare products. It's working fine



<div class="block widget block-compare">
<div class="block-title">
</div>
block class="MagentoFrameworkViewElementTemplate" name="catalog.compare.sidebar" template="Magento_Catalog::product/compare/sidebar.phtml" /
</div>


I got output like this: http://prntscr.com/n4jcsj



Now i want to show shopping cart items there above compare. Can anyone help me with this.



I also try with minicart but it is not working as per my requirement. It gives me only link of minicart. I need to show content of minicart. I used this code in block.



<div class="minicart">
<div>
block class="MagentoCheckoutBlockCartSidebar" name="mini_cart" template="Magento_Checkout::cart/minicart.phtml"
</div>
</div>


Which gives me output like this : https://prnt.sc/n3h35j



Please help me if anyone has some idea about this.










share|improve this question
















I have created block for sidebar.



I have added below code in my block for compare products. It's working fine



<div class="block widget block-compare">
<div class="block-title">
</div>
block class="MagentoFrameworkViewElementTemplate" name="catalog.compare.sidebar" template="Magento_Catalog::product/compare/sidebar.phtml" /
</div>


I got output like this: http://prntscr.com/n4jcsj



Now i want to show shopping cart items there above compare. Can anyone help me with this.



I also try with minicart but it is not working as per my requirement. It gives me only link of minicart. I need to show content of minicart. I used this code in block.



<div class="minicart">
<div>
block class="MagentoCheckoutBlockCartSidebar" name="mini_cart" template="Magento_Checkout::cart/minicart.phtml"
</div>
</div>


Which gives me output like this : https://prnt.sc/n3h35j



Please help me if anyone has some idea about this.







magento2 blocks






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Rakesh Donga

2,258316




2,258316










asked 2 days ago









Khooshbu PatelKhooshbu Patel

255




255












  • But i want to add it in block.

    – Khooshbu Patel
    2 days ago











  • means i don't understand

    – Rakesh Donga
    2 days ago











  • Can we add this code in static block ?

    – Khooshbu Patel
    2 days ago












  • Yes, exactly same

    – Khooshbu Patel
    2 days ago











  • I added this code in static block and got this output : prntscr.com/n4jw9r

    – Khooshbu Patel
    2 days ago

















  • But i want to add it in block.

    – Khooshbu Patel
    2 days ago











  • means i don't understand

    – Rakesh Donga
    2 days ago











  • Can we add this code in static block ?

    – Khooshbu Patel
    2 days ago












  • Yes, exactly same

    – Khooshbu Patel
    2 days ago











  • I added this code in static block and got this output : prntscr.com/n4jw9r

    – Khooshbu Patel
    2 days ago
















But i want to add it in block.

– Khooshbu Patel
2 days ago





But i want to add it in block.

– Khooshbu Patel
2 days ago













means i don't understand

– Rakesh Donga
2 days ago





means i don't understand

– Rakesh Donga
2 days ago













Can we add this code in static block ?

– Khooshbu Patel
2 days ago






Can we add this code in static block ?

– Khooshbu Patel
2 days ago














Yes, exactly same

– Khooshbu Patel
2 days ago





Yes, exactly same

– Khooshbu Patel
2 days ago













I added this code in static block and got this output : prntscr.com/n4jw9r

– Khooshbu Patel
2 days ago





I added this code in static block and got this output : prntscr.com/n4jw9r

– Khooshbu Patel
2 days ago










1 Answer
1






active

oldest

votes


















2














Create Block file



<?php
namespace VendorModuleBlock;

class ShoppingCart extends MagentoFrameworkViewElementTemplate

public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCheckoutModelSession $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 MagentoQuoteModelQuote
*/
public function getQuoteData()

$this->_checkoutSession->getQuote();
if (!$this->hasData('quote'))
$this->setData('quote', $this->_checkoutSession->getQuote());

return $this->_getData('quote');




Call in template(.phtml) file,



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

foreach($quote->getAllVisibleItems() as $_item)
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/>";






share|improve this answer

























  • i think issue in implementing

    – Rakesh Donga
    yesterday











  • Hello.. your code works fine when i disable page cache. But when page cache is enable it doesn't show any item in cart.

    – Khooshbu Patel
    yesterday











  • You can create extension for that and create block file also phtml file create like above my code

    – Rakesh Donga
    yesterday











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f267928%2fmagento-2-get-cart-items-in-block-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Create Block file



<?php
namespace VendorModuleBlock;

class ShoppingCart extends MagentoFrameworkViewElementTemplate

public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCheckoutModelSession $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 MagentoQuoteModelQuote
*/
public function getQuoteData()

$this->_checkoutSession->getQuote();
if (!$this->hasData('quote'))
$this->setData('quote', $this->_checkoutSession->getQuote());

return $this->_getData('quote');




Call in template(.phtml) file,



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

foreach($quote->getAllVisibleItems() as $_item)
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/>";






share|improve this answer

























  • i think issue in implementing

    – Rakesh Donga
    yesterday











  • Hello.. your code works fine when i disable page cache. But when page cache is enable it doesn't show any item in cart.

    – Khooshbu Patel
    yesterday











  • You can create extension for that and create block file also phtml file create like above my code

    – Rakesh Donga
    yesterday















2














Create Block file



<?php
namespace VendorModuleBlock;

class ShoppingCart extends MagentoFrameworkViewElementTemplate

public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCheckoutModelSession $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 MagentoQuoteModelQuote
*/
public function getQuoteData()

$this->_checkoutSession->getQuote();
if (!$this->hasData('quote'))
$this->setData('quote', $this->_checkoutSession->getQuote());

return $this->_getData('quote');




Call in template(.phtml) file,



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

foreach($quote->getAllVisibleItems() as $_item)
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/>";






share|improve this answer

























  • i think issue in implementing

    – Rakesh Donga
    yesterday











  • Hello.. your code works fine when i disable page cache. But when page cache is enable it doesn't show any item in cart.

    – Khooshbu Patel
    yesterday











  • You can create extension for that and create block file also phtml file create like above my code

    – Rakesh Donga
    yesterday













2












2








2







Create Block file



<?php
namespace VendorModuleBlock;

class ShoppingCart extends MagentoFrameworkViewElementTemplate

public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCheckoutModelSession $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 MagentoQuoteModelQuote
*/
public function getQuoteData()

$this->_checkoutSession->getQuote();
if (!$this->hasData('quote'))
$this->setData('quote', $this->_checkoutSession->getQuote());

return $this->_getData('quote');




Call in template(.phtml) file,



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

foreach($quote->getAllVisibleItems() as $_item)
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/>";






share|improve this answer















Create Block file



<?php
namespace VendorModuleBlock;

class ShoppingCart extends MagentoFrameworkViewElementTemplate

public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCheckoutModelSession $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 MagentoQuoteModelQuote
*/
public function getQuoteData()

$this->_checkoutSession->getQuote();
if (!$this->hasData('quote'))
$this->setData('quote', $this->_checkoutSession->getQuote());

return $this->_getData('quote');




Call in template(.phtml) file,



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

foreach($quote->getAllVisibleItems() as $_item)
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/>";







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









Rakesh DongaRakesh Donga

2,258316




2,258316












  • i think issue in implementing

    – Rakesh Donga
    yesterday











  • Hello.. your code works fine when i disable page cache. But when page cache is enable it doesn't show any item in cart.

    – Khooshbu Patel
    yesterday











  • You can create extension for that and create block file also phtml file create like above my code

    – Rakesh Donga
    yesterday

















  • i think issue in implementing

    – Rakesh Donga
    yesterday











  • Hello.. your code works fine when i disable page cache. But when page cache is enable it doesn't show any item in cart.

    – Khooshbu Patel
    yesterday











  • You can create extension for that and create block file also phtml file create like above my code

    – Rakesh Donga
    yesterday
















i think issue in implementing

– Rakesh Donga
yesterday





i think issue in implementing

– Rakesh Donga
yesterday













Hello.. your code works fine when i disable page cache. But when page cache is enable it doesn't show any item in cart.

– Khooshbu Patel
yesterday





Hello.. your code works fine when i disable page cache. But when page cache is enable it doesn't show any item in cart.

– Khooshbu Patel
yesterday













You can create extension for that and create block file also phtml file create like above my code

– Rakesh Donga
yesterday





You can create extension for that and create block file also phtml file create like above my code

– Rakesh Donga
yesterday

















draft saved

draft discarded
















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f267928%2fmagento-2-get-cart-items-in-block-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Sum ergo cogito? 1 nng

三茅街道4182Guuntc Dn precexpngmageondP