0

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!!!!!

3
  • That's how to create a product not add a product to the cart?!? Commented Nov 26, 2014 at 16:04
  • sorry just now checked.I am checking that too
    – Venkat
    Commented Nov 26, 2014 at 16:05
  • @cordy You also setting payment method programmatically here
    – Venkat
    Commented Nov 26, 2014 at 17:02

1 Answer 1

0

I advise you to dig around the code yourself for this issue you have. Start off by looking at Mage_Checkout_CartController. Basically, you need to...

  1. Create an empty quote and set store ID.

  2. Add products you want to this cart.

  3. Save this quote.

  4. Load it into the session of your customer.

0

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.