I'll answer the question in two parts:
I. Where should you change the checkout URL?
The destination in link.phtml
is $block->getCheckoutUrl()
. You should not change the template to change functionality, but change the block Magento\Checkout\Block\Onepage\Link
instead. To do so create a plugin for it with a method afterGetCheckoutUrl()
to change the return value.
II. How to get all items in the cart?
As of magento-2.0 the service contracts in Magento_Checkout and Magento_Quote do not provide the necessary methods yet, so we use the cart model. In your plugin inject it like this:
public function __construct(\Magento\Checkout\Model\Cart $cart)
{
$this->cart = $cart;
}
Now that you have access to the cart, you can retrieve the items in several ways, which is basically the same as in Magento 1:
$this->cart->getQuote()->getItemsCollection()
Returns a quote item collection with all items associated to the current quote.
$this->cart->getItems()
This is a shortcut for the method above, but if there is no quote it returns an empty array, so you cannot rely on getting a collection instance.
$this->cart->getQuote()->getAllItems()
Loads the item collection, then returns an array of all items which are not marked as deleted (i.e. have been removed in the current request).
$this->cart->getQuote()->getAllVisibleItems()
Loads the item collection, then returns an array of all items which are not marked as deleted AND do not have a parent (i.e. you get items for bundled and configurable products but not their associated children). Each array item corresponds to a displayed row in the cart page.
Choose what fits your needs best. In most cases the last method is what you need.
Here is the simpliest example
Folder structure:
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:
You can also delete all blocks that you don't want to show, or set your own phtml in xml
Best Answer
You can get the product via
quoteItem
inCart
. Use this code below:Use this code to get the product list:
Maybe you can use
productRepository
to load product by id:Hope it help!