/** * Distribute discount at parent item to children items * * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item * @return $this */ protected function distributeDiscount(\Magento\Quote\Model\Quote\Item\AbstractItem $item) { $parentBaseRowTotal = $item->getBaseRowTotal(); $keys = ['discount_amount', 'base_discount_amount', 'original_discount_amount', 'base_original_discount_amount']; $roundingDelta = []; foreach ($keys as $key) { //Initialize the rounding delta to a tiny number to avoid floating point precision problem $roundingDelta[$key] = 1.0E-7; } foreach ($item->getChildren() as $child) { $ratio = $child->getBaseRowTotal() / $parentBaseRowTotal; foreach ($keys as $key) { if (!$item->hasData($key)) { continue; } $value = $item->getData($key) * $ratio; $roundedValue = $this->priceCurrency->round($value + $roundingDelta[$key]); $roundingDelta[$key] += $value - $roundedValue; $child->setData($key, $roundedValue); } } foreach ($keys as $key) { $item->setData($key, 0); } return $this; }