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.
Quote Data Usage Overview
Obtaining the Quote
The typical way to retrieve the active quote:
// Example in controller or block
public function __construct(
\Magento\Checkout\Model\Session $checkoutSession
) {
$this->checkoutSession = $checkoutSession;
}
public function execute() {
$quote = $this->checkoutSession->getQuote();
// ...
}
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).
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.
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();
$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.
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'
]);
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.
Example from Magento_Sales
<?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
$productCollection = $this->_productCollectionFactory->create()
->setStoreId($this->getStoreId())
->addIdFilter($this->_productIds)
->addAttributeToSelect($this->_quoteConfig->getProductAttributes());
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_optiontable; useful for custom data. - Attributes: Only attributes in
catalog_attributes.xml(groupquote_item) are loaded with items.