0

I am setting up the custom price with some calculation during add to cart and update cart.

I am using following code to add custom price

$fPrice = 39.1467 // after calculation 
$qty = 24;
$item->setCustomPrice($fPrice);
$item->setOriginalCustomPrice($fPrice);
$item->getProduct()->setIsSuperMode(true);

Magento 2 calculating the row subtotal with this price and 24 quantity = $939.60 instead it should be $939.52. I have checked into the database and found that it is calculating with $fprice = 39.15 but I want to calculate with $fPrice = 39.1467 to correct the subtotal value.

Any help would be much appreciated. Thanks.

Database item row : enter image description here

0
$items = $this->_cart->getQuote()->getAllItems();
$totalItems = count($items);
if ($totalItems != 0) {
    foreach($items as $item) {
        if($item->getProductId() == $productId){
            $item->setData('qty', $qty);
            $item->addOption(array(
                   'code' => 'additional_options',
                   'value' => $this->_serialize->serialize($additionalOptions)
            ));
            $item->setCustomPrice($fPrice);
            $item->setOriginalCustomPrice($fPrice);
            $item->save();
        }
    }
}

Note: The price and qty will be set in all products cart, you need to get the values dynamically for all products.

  • My concern is, row total price is based on 39.15 while I have set custom price is 39.1467. I have checked into the database too. Price is saving correctly in db. – zed Blackbeard Aug 19 '18 at 8:53
  • By default magento consider 2 decimal places, So in frontend your price look like that. – Dhiren Vasoya Aug 20 '18 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.