添加项目以获取magento中的产品价格

时间:2014-07-07 16:15:22

标签: magento product

我使用以下代码将产品添加到报价对象以获取产品总价。 getGrandTotal不会给出任何结果。可能是什么问题?如何在不向CART添加项目的情况下获得总产品价格(因为这样做会在我的应用程序中产生问题)。

$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();

1 个答案:

答案 0 :(得分:2)

  

使用以下代码:

 $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();