How to get the current shopping cart details on the server-side (with PHP)?

I know in Magento 1 you can get the shopping cart details on any page with:

$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
    $productId = $item->getProduct()->getId();
    $productPrice = $item->getProduct()->getPrice();
}

How do I do the same thing in Magento 2?

Use the \Magento\Checkout\Model\Session::getQuote() method:

Usage examples

1. \Magento\Checkout\Block\Cart\AbstractCart::getQuote()

2. \Magento\Checkout\Block\Cart\Totals::getQuote()

3. \Magento\Checkout\Helper\Cart::getQuote()

A post was split to a new topic: How to get the \Magento\Checkout\Model\Session singleton in a *.phtml template?