Monday, July 10, 2017

Adding a Simple product to Cart in Magento externally

In this article, we'll see how we can insert a simple product to Magento Cart from an external PHP script.

We are testing this on Magento 1.9.2.2 and we have a simple product with ID 375 which we'll push to Cart. 

Let's see the code below. First, we are showing the existing Cart, then we'll insert a simple product ID 375, and finally we'll print the new Cart.

<?php
// Load Magento 
require_once("app/Mage.php");
umask(0);
Mage::app();

/// We Build a String for printing
$str = "";
$str .= "1. Showing existing Cart<br>===============<br>";

/// This line connects to the FrontEnd session
Mage::getSingleton('core/session', array('name' => 'frontend'));  

// Get customer session
$session = Mage::getSingleton('customer/session'); 

// Get cart instance
$cart = Mage::getSingleton('checkout/cart'); 
$cart->init();

// Get CART Count
$cartItemsCount = $cart->getItemsCount();
$str .= "Count >> $cartItemsCount <br>";

// GET CART Items
$quote = $cart->getQuote();
$cartItems = $quote->getAllVisibleItems();

// Loop Through ITEMs
foreach ($cartItems as $item) 
{
    $productId = $item->getProductId();
    $product = Mage::getModel('catalog/product')->load($productId);
$str .= "Id: $productId, Price: " . $item->getPrice() 
                . ", Qty:" . $item->getQty() . "<br>";
    // Do something more
}

// Build String
$str .= "<br><br>2. Adding a New Product<br>===============<br>";

// SET Product ID
$productId = 375;

// Build Product Params
$productInstance = Mage::getModel('catalog/product')->load($productId);
$param = array(
'product' => $productInstance->getId(),
'qty' => 1
);

// Finally ADD the product to CART
$cart->addProduct($productInstance, $param);            
// Save The Cart
$cart->save(); 

// Update Session
$session->setCartWasUpdated(true);

// Building the String
$str .= "<br><br>3. Showing the new Cart<br>===============<br>";

// Get cart instance
$cart = Mage::getSingleton('checkout/cart'); 
$cart->init();

// Get CART Count
$cartItemsCount = $cart->getItemsCount();
$str .= "Count >> $cartItemsCount <br>";

// GET CART Items
$quote = $cart->getQuote();
$cartItems = $quote->getAllVisibleItems();

// Loop Through ITEMs
foreach ($cartItems as $item) 
{
    $productId = $item->getProductId();
    $product = Mage::getModel('catalog/product')->load($productId);
    $str .= "Id: $productId, Price: " . $item->getPrice() 
            . ", Qty:" . $item->getQty() . "<br>";
    // Do something more
}

// Print the String
echo $str;
?>

The above code is quite self explanatory. We tried to connect to frontend customer's session and get his Cart details. If customer is not logged in, then it does not create any problem.

See some screenshot here : 

Step 1. Existing Cart


Step 2. Our Script Runs



Step 3. Final Cart




In this example, we added a simple product. To add a configurable product to Cart, we need to do some extra work which I'll show in next article.

Hope this helps.