3

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.

2 Answers 2

1

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();
0
2

You should collect totals at some point. I'm not sure where did you get the code to create the orders, but this guide is quite useful and worked for me in the past. Anyway try

$quote->collectTotals()->save();

after you add your products to cart.

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.