1

I'm manually adding some products to the cart:

$product = Mage::getModel('catalog/product')->load(113);
$params = array(
  'product' => 113,
  'qty' => 1
);
$request = new Varien_Object();
$request->setData($params);
$cartHelper->getCart()->addProduct($product, $request); 

But the price is always empty (or null).

I've been debugging the execution through

Mage_Checkout_Model_Cart->addProduct(...)
Mage_Sales_Model_Quote->addProduct(...)
Mage_Sales_Model_Quote->addProductAdvanced(...)

and finally:

protected function _addCatalogProduct(Mage_Catalog_Model_Product $product, $qty = 1)
{
    $newItem = false;
    $item = $this->getItemByProduct($product);
    if (!$item) {
        $item = Mage::getModel('sales/quote_item');
        $item->setQuote($this);
        if (Mage::app()->getStore()->isAdmin()) {
            $item->setStoreId($this->getStore()->getId());
        }
        else {
            $item->setStoreId(Mage::app()->getStore()->getId());
        }
        $newItem = true;
    }

    /**
     * We can't modify existing child items
     */
    if ($item->getId() && $product->getParentProductId()) {
        return $item;
    }

    $item->setOptions($product->getCustomOptions())
        ->setProduct($product);

    // Add only item that is not in quote already (there can be other new or already saved item
    if ($newItem) {
        $this->addItem($item);
    }

    return $item;
}

But the price is not there, in that $item :(

Any help would be much appreciated.

2 Answers 2

1

The price in a quote item is set in \Mage_Sales_Model_Quote::collectTotals

So you need to run \Mage_Sales_Model_Quote::collectTotals to have a price, this happens when a page is rendered, after reload.

You can run this manually, but be careful, unfortunately the method stores a bunch of cached prices, which may be reused in a wrong way and might lead to wrong prices if called twice!

0

Along with the suggested below collectTotals() call please try to set store id to the product before load. $storeId = Mage::app()->getStore()->getId(); $product = Mage::getModel('catalog/product') ->setStoreId($storeId) ->load(113);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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