-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Closed
Labels
Description
Hi,
I'm trying to programatically figure out the final price for a specific set of items without having to add them to the user cart before (mostly to avoid messing up customer's session and accidentally adding products their don't want).
I'm using the following code:
$quote = $this->quoteFactory->create();
$product = $this->productRepository->get('my-sku');
$quote->addProduct($product, 1);
// add more products here
$quote->getShippingAddress(); # otherwise $quote->getAllAddresses() returns empty
$quote->setTotalsCollectedFlag(false);
$quote->collectTotals();
var_dump($quote->getTotals());But, it looks like \Magento\Quote\Model\Quote\TotalsCollector::collect is ignoring the discount_amount, since the $quote->getTotals() does not report the discount key at all.
Interesting enough, the grand_total is mentioned with discount (10.00, 10800 => 10790). Seems that just adding the following line after $addressTotal fixes the issue (and it was how I generated the "expected result" section later on).
$total->setDiscountAmount($addressTotal->getDiscountAmount());Preconditions
- Magento 2.1.1;
- Enabled cart promotion giving 10% off for
my-sku, no customer group restrictions; - Sales rules reindexed (EE);
Steps to reproduce
- Run the php code mentioned above inside a controller or a Test app;
Expected result
- $quote->getTotals() should return the
discountkey with the giving discount for the applied rules.
/var/www/html/app/code/Vendor/Price/Model/Calculator.php:61:
array (size=5)
'subtotal' =>
object(Magento\Quote\Model\Quote\Address\Total)[2067]
protected 'totalAmounts' => null
protected 'baseTotalAmounts' => null
protected '_data' =>
array (size=5)
'code' => string 'subtotal' (length=8)
'title' =>
object(Magento\Framework\Phrase)[2093]
...
'value' => float 10800
'value_incl_tax' => float 10800
'value_excl_tax' => float 10800
'shipping' =>
object(Magento\Quote\Model\Quote\Address\Total)[2096]
protected 'totalAmounts' => null
protected 'baseTotalAmounts' => null
protected '_data' =>
array (size=3)
'code' => string 'shipping' (length=8)
'title' =>
object(Magento\Framework\Phrase)[2085]
...
'value' => float 0
'discount' =>
object(Magento\Quote\Model\Quote\Address\Total)[1671]
protected 'totalAmounts' => null
protected 'baseTotalAmounts' => null
protected '_data' =>
array (size=3)
'code' => string 'discount' (length=8)
'title' =>
object(Magento\Framework\Phrase)[2095]
...
'value' => float -10
'tax' =>
object(Magento\Quote\Model\Quote\Address\Total)[2101]
protected 'totalAmounts' => null
protected 'baseTotalAmounts' => null
protected '_data' =>
array (size=5)
'code' => string 'tax' (length=3)
'title' =>
object(Magento\Framework\Phrase)[2098]
...
'full_info' =>
array (size=0)
...
'value' => float 0
'area' => string 'taxes' (length=5)
'grand_total' =>
object(Magento\Quote\Model\Quote\Address\Total)[2105]
protected 'totalAmounts' => null
protected 'baseTotalAmounts' => null
protected '_data' =>
array (size=4)
'code' => string 'grand_total' (length=11)
'title' =>
object(Magento\Framework\Phrase)[2104]
...
'value' => float 10790
'area' => string 'footer' (length=6)
Actual result
- $quote->getTotals() does not contain the
discountkey.
/var/www/html/app/code/Vendor/Price/Model/Calculator.php:61:
array (size=4)
'subtotal' =>
object(Magento\Quote\Model\Quote\Address\Total)[2068]
protected 'totalAmounts' => null
protected 'baseTotalAmounts' => null
protected '_data' =>
array (size=5)
'code' => string 'subtotal' (length=8)
'title' =>
object(Magento\Framework\Phrase)[2096]
...
'value' => float 10800
'value_incl_tax' => float 10800
'value_excl_tax' => float 10800
'shipping' =>
object(Magento\Quote\Model\Quote\Address\Total)[2097]
protected 'totalAmounts' => null
protected 'baseTotalAmounts' => null
protected '_data' =>
array (size=3)
'code' => string 'shipping' (length=8)
'title' =>
object(Magento\Framework\Phrase)[2086]
...
'value' => float 0
'tax' =>
object(Magento\Quote\Model\Quote\Address\Total)[2094]
protected 'totalAmounts' => null
protected 'baseTotalAmounts' => null
protected '_data' =>
array (size=5)
'code' => string 'tax' (length=3)
'title' =>
object(Magento\Framework\Phrase)[1672]
...
'full_info' =>
array (size=0)
...
'value' => float 0
'area' => string 'taxes' (length=5)
'grand_total' =>
object(Magento\Quote\Model\Quote\Address\Total)[2104]
protected 'totalAmounts' => null
protected 'baseTotalAmounts' => null
protected '_data' =>
array (size=4)
'code' => string 'grand_total' (length=11)
'title' =>
object(Magento\Framework\Phrase)[2103]
...
'value' => float 10790
'area' => string 'footer' (length=6)
Thanks.