1

I want to get items that added to checkout cart and show that/those in other page.

I have tried many different ways, but in Magento 2 im not been able to do it.

Can you help me out?

Thank you

3
  • Could you please provide more info? what page eaxctly? How did you tied to do it? Basically you just need to copy checkout cart layout to your pages layout. – Stepan Furman Jan 6 '18 at 11:06
  • I think it doesn't matter at which page because the code will be the same. Anyway I have a phtml page (lets say site.com/buy) and what to show all the cart items that any user add products to cart to show here. I have tried some codes that have seen here in stackoverflow but none of them seems to work. I have tried to copy the cart layout but didn't worked. – Algert Zh Jan 6 '18 at 11:11
  • See answer below. Let me know if you need some additional changes. You can also contact me via skype f0rman – Stepan Furman Jan 6 '18 at 11:36
0

Here is the simpliest example

Folder structure:

enter image description here

1 magento_root/app/code/Furman/NewChekout/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Furman_NewChekout" setup_version="0.1.0">
    <sequence>
        <module name="Magento_Catalog"/>
        <module name="Magento_Checkout"/>

    </sequence>
</module>

2 magento_root/app/code/Furman/NewChekout/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Furman_NewChekout',
    __DIR__
);

3 magento_root/app/code/Furman/NewChekout/composer.json

{
"name": "furman/newchekout",
"description": "",
"require": {
    "php": "~5.5.0|~5.6.0|~7.0.0",

    "magento/magento-composer-installer": "*"
},
"suggest": {

},
"type": "magento2-module",
"version": "0.1.0",
"license": [

],
"autoload": {
    "files": [
        "registration.php"
    ],
    "psr-4": {
        "Furman\\NewChekout\\": ""
    }
},
"extra": {
    "map": [
        [
            "*",
            "Furman/NewChekout"
        ]
    ]
}
}

4 magento_root/app/code/Furman/NewChekout/etc/frontend/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
    <route id="newcheckout" frontName="newcheckout">
        <module name="Furman_NewChekout"/>
    </route>
</router>
</config>

5 magento_root/app/code/Furman/NewChekout/Controller/Cart/Index.php

<?php

namespace Furman\NewChekout\Controller\Cart;

class Index extends \Magento\Checkout\Controller\Cart\Index
{

}

6 magento_root/app/code/Furman/NewChekout/view/frontend/layout/newcheckout_cart_index.xml

(all xml please copy from magento core file to your file: ./magento_root/vendor/magento/module-checkout/view/frontend/layout/checkout_cart_index.xml or from here )

And here is the result: enter image description here

You can also delete all blocks that you don't want to show, or set your own phtml in xml

11
  • Hello, I tried this but when I go to domain.com/cart/index I have error 404 not found. Also I changed "furman" to my theme vendor. – Algert Zh Jan 6 '18 at 15:14
  • @AlgertZh What front name do you use? It's what you specified in routes.xml – Stepan Furman Jan 6 '18 at 15:16
  • I have used: newcheckout and I also tried domain.com/newcheckout/index and I got the same thing – Algert Zh Jan 6 '18 at 15:20
  • and also tried domain.com/newcheckout/cart/index – Algert Zh Jan 6 '18 at 15:22
  • @AlgertZh try, domain.com/newcheckout/cart/index – Stepan Furman Jan 6 '18 at 15:23
0

Try this:

Get all items:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 
$itemsCollection = $cart->getQuote()->getItemsCollection();
$items = $cart->getQuote()->getAllItems();
    if(count($items)) {
        foreach($items 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 "<br />";            
    }   
} 

Get Visible items:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 
$itemsCollection = $cart->getQuote()->getItemsCollection();
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
    if(count($itemsVisible)) {
        foreach($itemsVisible 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 "<br />";            
    }   
}
1
  • I tried both of them, and nothing is displayed. – Algert Zh Jan 6 '18 at 14:37
0

In addition to the valid answer, only a thing.

In your newcheckout_cart_index.xml you don't need copy entire checkout_cart_index.xml. Only need to add an update handle layout task.

<?xml version="1.0" ?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="checkout_cart_index"/>
</page>

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.