If you activate the debug option, you can see all data available for each object that you use.
Remember that you don't have access to all methods of the original objects because they are not accessed directly.
Global Variables
These variables are global variables and can be accessed everywhere (use the global
keyword when you are inside a function).
The request object (\Magento\Quote\Model\Quote\Address\RateRequest
) given by Magento:
$request->all_items
: array of items in cart
- The source address:
$request->country_id
*
$request->region_id
$request->city
$request->postcode
- The destination address:
- The package:
$request->package_value
: value of the package
$request->package_value_with_discount
: value of the package with discount
$request->package_weight
: weight of the package
$request->package_qty
: number of products in the package
- Other attributes:
$request->free_shipping
: free shipping calculated by Magento rules
$request->*
: property of the request object
The quote (\Magento\Quote\Model\Quote
):
(known issue #2, known issue #3):
$quote->subtotal
: subtotal (excluding tax)
$quote->subtotal_with_discount
: subtotal (excluding tax) with discount
$quote->grand_total
: total (including tax) with discount
(known issues #3, you should avoid using this value)
$quote->base_subtotal
: subtotal (excluding tax) in base currency
$quote->base_subtotal_with_discount
: subtotal (excluding tax) with discount in base currency
$quote->base_grand_total
: total (including tax) with discount in base currency
(known issues #3, you should avoid using this value)
$quote->coupon_code
: the coupon code used in cart
$quote->*
: property of the quote (e.g. $quote->subtotal
)
The customer group (\Magento\Customer\Model\Group
):
$customer_group->id
: alias of $customer_group->customer_group_id
$customer_group->code
: alias of $customer_group->customer_group_code
$customer_group->name
: alias of $customer_group->customer_group_code
$customer_group->*
: property of the customer group (e.g. $customer_group->tax_class_id
)
The customer (\Magento\Customer\Model\Customer
):
$customer->id
: alias of $customer->entity_id
$customer->*
: attribute of the customer (e.g. email, lastname, firstname, group_id…)
$customer->getCustomAttribute('my_custom_attribute')->value
: custom attribute of the customer (where 'my_custom_attribute' should be replaced by custom attribute's code)
Custom variables (\Magento\Variable\Model\Variable
, examples):
-
$variable->*
: custom variable defined in Magento
e.g. with $variable->my_var
:
$variable->my_var->plain_value
$variable->my_var->html_value
$variable->my_var->code
$variable->my_var->name
The store (\Magento\Store\Model\Store
):
$store->id
, $store->code
, $store->name
, $store->address
, $store->phone
The app (see \Magento\Framework\App\State
):
$app->area_code
$app->isAdminArea()
(area_code == 'adminhtml')
$app->isFrontendArea()
(area_code == 'webapi_rest')
* Magento apparently uses the ISO 3166-1 alpha-2 codes.
Note that the country code for United Kingdom is GB and not UK.
Accessing Items
You can use $request->all_items
with array functions.
The item (\Magento\Quote\Model\Quote\Item
)
-
$item->qty
quantity of the item
-
$item->weight
weight of the item
-
$item->base_original_price
price excluding tax without discount
-
$item->price_incl_tax
price including tax without discount
-
$item->options->*
option (the available options depend on the product)
The product (\Magento\Catalog\Model\Product
)
-
$item->product->*
sku
name
weight
price
(as defined in Magento backoffice)
special_price
: (as defined in Magento backoffice)
tax_class_id
: the taxclass id
getAttributeText('tax_class_id')
: the tax class name
To get the text value of an attribute of type select, use getAttributeText('attribute_code')
getSourceItems()
: returns an array of \Magento\InventoryApi\Api\Data\SourceItemInterface
to handle Inventory Management (Magento ≥ 2.3)
Source item: (\Magento\InventoryApi\Api\Data\SourceItemInterface
)
$sourceItem->*
: attribute of the source item
source_code
: the code of the inventory source
quantity
: the quantity of the product in the inventory source
- …
- …
-
Categories of the product: (
\Magento\Catalog\Model\Category
)
$item->product->category->*
- attribute of the first category
-
All product categories (returns an array, examples)
-
The attribute set (
\Magento\Eav\Model\Entity\Attribute\Set
)
-
$item->product->attribute_set
the attribute set
-
$item->product->attribute_set->*
attribute of the attribute set
-
The stock item (
\Magento\CatalogInventory\Model\Stock\Item
)
-
$item->product->stock_item->*
attribute of the product stock