Skip to main content
replaced http://magento.stackexchange.com/ with https://magento.stackexchange.com/
Source Link

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 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 1the 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.

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 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.

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 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.

improve formatting, add information on service contracts
Source Link
Fabian Schmengler
  • 66.1k
  • 25
  • 188
  • 422
  1. 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.

  2. How to get all items in the cart?

    In your plugin inject the cart model

     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.

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 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.

  1. 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.

  2. How to get all items in the cart?

    In your plugin inject the cart model

     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.

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 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.

Reference answer for Magento 1 and use same explanation for quote methods
Source Link
Fabian Schmengler
  • 66.1k
  • 25
  • 188
  • 422

I'll answer the question in two parts:

  1. 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.

  2. How to get all items in the cart?

    In your plugin inject the cart model

     public function __construct(\Magento\Checkout\Model\Cart $cart)
     {
         $this->cart = $cart;
     }
    

    ThenNow 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->getItems>getQuote()->getItemsCollection()
    

    returns the cartReturns a quote item collection with allall items orassociated 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 if there are none, so you cannot rely on getting a collection instance.

     $this->cart->getQuote()->getAllItems()
    

    Loads the item collection, then returns an array witharray of all items that are not marked as deletedwhich 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 witharray of all items that are not marked as deleted and do not have a parent itemwhich are not marked as deleted AND do not have a parent (i. This means,e. you get items for bundled and configurable products you'll get the parent item (the configurable product), but not the child item (thetheir associated simple productchildren), same goes for bundle products. 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.

I'll answer the question in two parts:

  1. 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.

  2. How to get all items in the cart?

    In your plugin inject the cart model

     public function __construct(\Magento\Checkout\Model\Cart $cart)
     {
         $this->cart = $cart;
     }
    

    Then you can retrieve the items in several ways:

     $this->cart->getItems()
    

    returns the cart item collection with all items or an empty array if there are none.

     $this->cart->getQuote()->getAllItems()
    

    returns an array with all items that are not marked as deleted.

     $this->cart->getQuote()->getAllVisibleItems()
    

    returns an array with all items that are not marked as deleted and do not have a parent item. This means, for configurable products you'll get the parent item (the configurable product), but not the child item (the associated simple product), same goes for bundle products.

I'll answer the question in two parts:

  1. 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.

  2. How to get all items in the cart?

    In your plugin inject the cart model

     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.

Source Link
Fabian Schmengler
  • 66.1k
  • 25
  • 188
  • 422
Loading