Collect Totals:
We should base on shipping address.
\Magento\Quote\Model\Quote::collectTotals
(can be called at the first) loops through each quote address (quote address total):
$total = $this->totalsCollector->collect($this);
The \Magento\Quote\Model\Quote\TotalsCollector::collect()
and collectAddressTotals()
will be called for the next step
\Magento\Quote\Model\Quote\TotalsCollector::collectAddressTotals()
foreach ($this->collectorList->getCollectors($quote->getStoreId()) as $collector) {
/** @var CollectorInterface $collector */
$collector->collect($quote, $shippingAssignment, $total);
}
In the foreach loop, we will get the collect total of each total Model, for example: \Magento\Quote\Model\Quote\Address\Total\Subtotal::collect()
, Magento\Weee\Model\Total\Quote\Weee::collect()
, Magento\SalesRule\Model\Quote\Discount::collect()
. Our custom total will be called in this loop also.
Each total Model based on the shipping address.
So, we need to check: if having shipping address items, we will process the custom totals
For example, in the collect method, we need to check: vendor/magento/module-sales-rule/Model/Quote/Discount.php
parent::collect($quote, $shippingAssignment, $total);
$items = $shippingAssignment->getItems();
if (!count($items)) {
return $this;
}
Or (your case can be used)
if ($shippingAssignment->getShipping()->getAddress()->getAddressType() != Address::TYPE_SHIPPING) {
return $this;
}
Grand Totals:
As far as I know, the grand total is the "final" total after tax, discount,....
This is an example of getting default customer's billing and shipping address if you have the customerId and using the customerRepository Magento\Customer\Api\CustomerRepositoryInterface
and the addressRepository Magento\Customer\Api\AddressRepositoryInterface
$customer = $this->customerRepository->getById($customerId);
$billingAddressId = $customer->getDefaultBilling();
$shippingAddressId = $customer->getDefaultShipping();
//get default billing address
try {
$billingAddress = $this->addressRepository->getById($billingAddressId);
} catch (\Exception $e) {
//
}
Best Answer
I know, this was late, but just for use of other developers
It will print all field inside array, then you will get the idea which field to retrive from it for example
to get postcode of shipping address
output of print_r($shippingaddress);