Reputation: 11
I am using the below code for adding a product to quote object for getting the product total price. The getGrandTotal does not give any result. What could be the issue? How can i get total product price WITHOUT ADDING ITEM TO THE CART(as doing that would create issues in my application).
$data = $this->getRequest()->getParams();
$product = Mage::getModel('catalog/product')->load($data['product']);
$customer = Mage::getSingleton('customer/session')->getCustomer();
$bill_address_id = $customer->getDefaultBilling();
$ship_address_id = $customer->getDefaultShipping();
$bill_address = Mage::getModel('customer/address')->load($bill_address_id);
$ship_address = Mage::getModel('customer/address')->load($ship_address_id);
$store = Mage::app()->getStore();
$quote = Mage::getModel('sales/quote');
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($product)->setQty($data['qty']);
$quote->addItem($quoteItem);
$quote->setStore($store);
$quote->getShippingAddress()->setCountryId($ship_address->getCountryId())
->setRegion($ship_address->getRegion())
->setPostcode($ship_address->getPostcode());
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();
$quote->getShippingAddress()->setShippingMethod('flatrate_flatrate');
$quote->getShippingAddress()->collectTotals()->save();
Mage::log("Get Data for Quote:" . print_r($quote->getData(), true));
$totals = $quote->getGrandTotal();
Upvotes: 0
Views: 3775
Reputation: 45
Use below code:
$data = $this->getRequest()->getParams();
$product = Mage::getModel('catalog/product')->load($data['product']);
$customer = Mage::getSingleton('customer/session')->getCustomer();
$store = Mage::app()->getStore();
$quote = Mage::getModel('sales/quote');
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($product)->setQty($data['qty']);
$quote->addItem($quoteItem);
$quote->setStore($store);
$billingAddressData = array(
'firstname' => 'Test',
'lastname' => 'Test',
'street' => 'Sample Street 10',
'city' => 'Somewhere',
'postcode' => '123456',
'telephone' => '123456',
'country_id' => 'US',
'region_id' => 12, // id from directory_country_region table
); // billing address
$shippingAddressData = array(
'firstname' => 'Test',
'lastname' => 'Test',
'street' => 'Sample Street 10',
'city' => 'Somewhere',
'postcode' => '123456',
'telephone' => '123456',
'country_id' => 'US',
'region_id' => 12, // id from directory_country_region table
);// shipping address
$billingAddress = $quote->getBillingAddress()->addData($billingAddressData);
$shippingAddress = $quote->getShippingAddress()->addData($shippingAddressData);
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod('flatrate_flatrate')
->setPaymentMethod('checkmo');
$quote->getPayment()->importData(array('method' => 'checkmo'));
$quote->collectTotals()->save();
$quote->save();
echo "Grand Total is = ". $totals = $quote->getGrandTotal();
Upvotes: 3