5.04

Basic Usage of Quote Data

Given a scenario, describe quote data usage: obtaining quotes, addresses (multi-shipping vs single), totals, items, item options, and product attributes in items.

Why This Matters: Quote data access is central to cart/checkout customizations. Understanding how to retrieve quotes, addresses, totals, items, item options, and product attributes ensures reliable operations across single and multi-shipping scenarios.

Quote Data Usage Overview

mindmap root((Quote Data)) Get Quote Checkout Session getQuote Initial loading subtleties Quote merging Addresses Multi-shipping - many shipping + 1 billing Single - 1 shipping or 0 if virtual + 1 billing Quote address methods Totals Address getTotals Total amounts collection Items Quote getAllItems Quote getAllVisibleItems Quote getItemsCollection Deleted items in collection Item Options quote_item_option table Custom options bundle info etc Product Attributes catalog_attributes.xml Limited attributes loaded

Obtaining the Quote

The typical way to retrieve the active quote:

\Magento\Checkout\Model\Session::getQuote()
// Example in controller or block
public function __construct(
    \Magento\Checkout\Model\Session $checkoutSession
) {
    $this->checkoutSession = $checkoutSession;
}

public function execute() {
    $quote = $this->checkoutSession->getQuote();
    // ...
}
Note: Review the source of getQuote() for subtleties around initial loading and session handling. Also consider quote merging (guest → customer login scenario, see 5.01).

Quote Addresses

How Many Addresses?

  • Multi-shipping enabled: Many shipping addresses, one billing address.
  • Multi-shipping disabled: At most one shipping address, one billing address.
  • Virtual quote: No shipping address (only billing).
\Magento\Quote\Model\Quote (lines ~1143-1400)

Key Methods

$quote->getBillingAddress();
$quote->getShippingAddress();         // Returns default/first shipping address
$quote->getAllShippingAddresses();    // Multi-shipping scenario
$quote->getIsVirtual();               // true if only virtual items

Total Amounts

Use the Address object to retrieve calculated totals.

$address = $quote->getShippingAddress();
$totals = $address->getTotals();  // Returns collection of Total objects

foreach ($totals as $total) {
    echo $total->getCode() . ': ' . $total->getValue();
}

See 5.01 for details on the Totals framework.

Items

Items belong to both Quote and Address objects. In 95% of scenarios, retrieve items via Quote.

\Magento\Quote\Model\Quote (lines ~1400-1600)

Key Item Methods

  • getAllItems(): Returns all items, including children (e.g., configurable parent + selected child).
  • getAllVisibleItems(): Returns items visible to the customer (excludes some children).
  • getItemsCollection(): Returns the collection object for advanced operations.
// All items including children
$allItems = $quote->getAllItems();

// Visible items only
$visibleItems = $quote->getAllVisibleItems();

// Collection for filtering
$itemsCollection = $quote->getItemsCollection();
Note: Deleted items may remain in the collection. Check $item->isDeleted() if needed.

See 5.01 for parent/child relationships (configurable, bundle, etc.).

Item Options

Additional data that doesn't fit into standard item fields is stored in item options.

quote_item_option table

Use Cases

  • Product custom options (text fields, dropdowns, etc.)
  • Technical data for product types (bundle selections, configurable variants)
  • Custom data for extensions (often easier than creating new tables)
// Get an option by code
$option = $item->getOptionByCode('option_code');

// Set an option
$item->addOption([
    'code' => 'custom_data',
    'value' => 'some_value'
]);
Tip: Storing custom data in item options is often simpler than maintaining separate tables.

Product Attributes Available in Items

When loading items, Magento doesn't load all product EAV attributes (for performance). Only attributes defined in catalog_attributes.xml are loaded.

etc/catalog_attributes.xml

Example from Magento_Sales

Magento/Sales/etc/catalog_attributes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
    <group name="quote_item">
        <attribute name="sku" />
        <attribute name="name" />
        <attribute name="price" />
        <!-- Additional attributes -->
    </group>
</config>

Loading Items with Attributes

\Magento\Quote\Model\ResourceModel\Quote\Item\Collection
$productCollection = $this->_productCollectionFactory->create()
    ->setStoreId($this->getStoreId())
    ->addIdFilter($this->_productIds)
    ->addAttributeToSelect($this->_quoteConfig->getProductAttributes());
Key: To use custom product attributes in cart/checkout, add them to catalog_attributes.xml under the quote_item group.

Further Reading

Exam Tips

  • Get Quote: Use Checkout\Model\Session::getQuote(); review source for loading subtleties.
  • Addresses: Multi-shipping = many shipping + 1 billing; single = at most 1 shipping + 1 billing; virtual = billing only.
  • Totals: Address::getTotals() returns collection of total amounts.
  • Items: getAllItems() includes children; getAllVisibleItems() for customer view; getItemsCollection() for advanced ops.
  • Item Options: Stored in quote_item_option table; useful for custom data.
  • Attributes: Only attributes in catalog_attributes.xml (group quote_item) are loaded with items.