Magento Enterprise – Item Has $0.00 Price in Cart When Creating Order Programmatically

cartcreate-ordermagento-enterpriseshopping-cartshopping-cart-price-rules

So, I am trying to create an order programmatically in Magento 1.12 EE. I can load the a product and see that the price property holds the correct value. When I add the item to the cart, the price changes to $0.00. Here's the script I'm using:

$customer = Mage::getModel('customer/customer');
$customAddress = ...;
//set customer data and log in customer, left out for this post

$checkout_session = Mage::getSingleton('checkout/session');
$quote = $checkout_session->getQuote();

$checkout_session->getQuote()
    ->setBillingAddress(Mage::getSingleton('sales/quote_address')
    ->importCustomerAddress($customAddress));

$product = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('entity_id', '634887')
    ->addAttributeToSelect('*')
    ->getFirstItem();

echo "product price: " . $product->getPrice() . "<br>";
//prints correct price

$pid = $product->getId();
$product->load($pid);

$cart = Mage::getSingleton('checkout/cart');
$cart->truncate();
$cart->save();
$cart->getItems()->clear()->save();

try {
    $cart->addProduct($product->getId(), array('qty'=>1));
    $cart->save();
    print "product added <br>";
}
catch (Exception $ex) {
    echo "product error " . $ex->getMessage() . "<br>";
}

$cart_grand_total = $cart->getQuote()->getGrandTotal();
$quote_grand_total = $quote->getGrandTotal();
$cart_items = $cart->getItems()->getData();
$quote_data = $quote->getData();

echo "CART GRAND TOTAL<br>";
var_dump($cart_grand_total);
//returns 0

echo "CHECKOUT SESSION QUOTE GRAND TOTAL<br>";
var_dump($quote_grand_total);
//returns 0

echo "CART ITEMS DUMP<br>";
var_dump($cart_items);
//shows item price as 0.00

echo "CHECKOUT SESSION QUOTE DATA DUMP<br>";
var_dump($quote_data);
//shows grand total = 0

Any ideas as to why the item goes from correct price before being added to the cart, then becomes $0.00 price when it is in the cart?

Thanks in advance for any help.

Best Answer

Remove these lines from your code and the order totals should appear. Somehow they are inhibiting your cart from getting any totals.

$cart->save();
$cart->getItems()->clear()->save();
Related Topic