I am looking for entire example/sample code for adding the first item to your magento cart via php programatically. This code should create the ENTIRE SHOPPING CART and send the user to the checkout page. I am not looking to create a complete order already paid. I am looking to create a complete CART that will allow the user to finish checkout. Why is this so complicated? My current code will only allow checkout if the first item was added from normal website (cart not empty). My module allows user to add a custom product, which could obviously be added before the user has items in the cart.
here is my module:
http://jaycon.sceneserver.com/index.php/quickrfq/index/?service=PCB%20Production
and here is the source code that should use the existing quote, or create a new one:
// do the new addtocart action
$quote = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
if(!empty($quote)){
$session = Mage::getSingleton('customer/session');
// Get cart instance
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$productInstance = Mage::getModel('catalog/product')->load($productId);
$request = new Varien_Object();
$request->setData($param);
$cart->addProduct($productInstance, $param);
$session->setCartWasUpdated(true);
$cart->save();
// get cartItems and store last item ID
$cartItems = Mage::getModel("checkout/cart")->getItems();
foreach($cartItems as $item) {
$cart_ids[] = $item->getId();
}
$cart_id = end($cart_ids);
Mage::getSingleton('core/session')->setPCBCartId($cart_id);
}
else { // need to run the full code to create a quote
// Get customer session
$session = Mage::getSingleton('customer/session');
$customer_id = Mage::getSingleton('customer/session')->getId();
$store_id = Mage::app()->getStore()->getId();
$customerObj = Mage::getModel('customer/customer')->load($customer_id);
$quoteObj = Mage::getModel('sales/quote')->assignCustomer($customerObj);
$storeObj = $quoteObj->getStore()->load($store_id);
$quoteObj->setStore($storeObj);
$quoteObj->setStoreId($store_id);
// Get cart instance
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$productInstance = Mage::getModel('catalog/product')->load($productId);
$request = new Varien_Object();
$request->setData($param);
$cart->addProduct($productInstance, $param);
// update session
$session->setCartWasUpdated(true);
// save the cart
$cart->save();
$quoteObj->collectTotals();
$quoteObj->save();
// add the payment method
$checkout = Mage::getSingleton('checkout/session');
$quote = $checkout->getQuote();
$quote->getPayment()->importData(array('method' => 'purchaseorder'));
$quote->collectTotals();
$quote->save();
}
PLEASE HELP I will send good vibes, prayers, and good karma to you.
I WILL MAKE GRAND DONATION TO YOUR FAVORITE PERSONAL CHARITY!!!!!