Friday, January 22, 2016

Add to cart product in magento.
Make a rest api for add to cart product in magento based on the particular customer id .
Here is given code for add to cart api .
In this code I take customer id , product id , and quantity for add to cart product .
In this process I use the check out /cart for adding the product.
<?php 
require_once("../../app/Mage.php");
umask(0);
Mage::app();
ini_set('display_errors', 1);
$id = $_REQUEST['product_id']; // Replace id with your product id
$qty = $_REQUEST['qty']; // Replace qty with your qty
$uid=$_REQUEST['customer_id'];
if(!empty($id) && !empty($qty) && !empty($uid))
{
                try{
                $_product = Mage::getModel('catalog/product')->load($id);
                $cart = Mage::getModel('checkout/cart');
                $cart->init(); 
                if($_product->isConfigurable()){
                $attribute_id = $_REQUEST['attribute_id'];
                $option_id = $_REQUEST['option_id'];
                $params = array(
                                                'product' => $id,
                                                'super_attribute' => array(
                                                                $attribute_id => $option_id,         //525 is the attribute id of size and 100 is the selected option value (small) of that attribute.
                                                 ),
                                                'qty' => $qty,
                                );
                }else{
                                $params = array('qty' => $qty);
                }

$cart->addProduct($_product, $params);
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

                if(!empty($uid))
                {
                                $current_cart = array();
                                $customer = Mage::getModel('customer/customer');
                                $customer = $customer->load($uid);
                                $quote = Mage::getModel('sales/quote')->loadByCustomer($customer);
                                $i = 0;
                                foreach ($quote->getItemsCollection() as $item) {                          
                                                $_product = Mage::getModel('catalog/product')->load($item->getProductId());
                                                $params = array('qty' => $item->getQty());
                                                $cart->addProduct($_product, $params);
                                                Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
                                }
                }
    $cart->save();
    $currenQuoteId = Mage::getModel('checkout/cart')->getQuote()->getId();
    $customer = Mage::getModel('customer/customer');
    $customer = $customer->load($uid);
    $quote = Mage::getModel('sales/quote')->loadByCustomer($customer);
   
$store = Mage::getSingleton('core/store')->load(Mage::app()->getStore()->getId());
$quote = Mage::getModel('sales/quote')->setStore($store)->load($currenQuoteId);
$quote->setCustomerId($uid);
$quote->save();
$cid['cart_id']=Mage::getModel('checkout/cart')->getQuote()->getId();
$quote = Mage::helper('checkout/cart')->getCart()->getQuote();
$titem['ttl_in_cart']= Mage::helper('checkout/cart')->getCart()->getItemsCount();
$gt=array_merge($cid,$titem);
$result['status']='success!';
$result['add_to_cart']=$gt;
                }
                catch (Exception $e) {
                                $result['status']='failier';
         $result['add_to_cart']= $e->getMessage();
                                //echo 'Caught exception: ',  $e->getMessage(), "\n";
                }
}
echo json_encode($result);
?>

Retrieve product from the cart based on the customer id.
<?php 
require_once("../../app/Mage.php");
umask(0);
Mage::app('default');
ini_set('display_errors', 1);
ob_start();
session_start();
$customerId=$_REQUEST['customer_id'];
if(!empty($customerId))
{
$current_cart = array();

    $customer = Mage::getModel('customer/customer');
    $customer = $customer->load($customerId);
    $quote = Mage::getModel('sales/quote')->loadByCustomer($customer);
$i = 0;
foreach ($quote->getItemsCollection() as $item) {
                $product = $item->getProduct();
                $current_cart[$i]['cart_item_id'] = $item->getId();
                $current_cart[$i]['name'] = $product->getName();
                $current_cart[$i]['product_id'] = $product->getId();
                $current_cart[$i]['price'] = $product->getPrice();
                $current_cart[$i]['special_price'] = (string)$product->getFinalPrice();
                $current_cart[$i]['qty'] = $item->getQty();
                $current_cart[$i]['image'] = (string)Mage::helper('catalog/image')->init($product, 'thumbnail');
                $i++;
                $result['status']='success';
                $result['retrive_cart']=$current_cart;
}
}else
{
                $result['status']='failier';
                $result['retrive_cart']='parameter mising';
}
if($i>0)
{
                echo  json_encode($result);
}else{
                $result['status']='failier';
                $result['retrive_cart']='cart empty!';
                echo  json_encode($result);
}


?>


Delete and update the cart in magento . in this api I use the customer id for update and delete the cart based on the customer id and action that are given by the input parameter by the customer to choose as the delete and upade In delete we give the customer id and product id based on this we delete the particular product from the cart . in update we give the customer id , product id and quantity for update the particular product.
Here code
<?php
require_once("../../app/Mage.php");
umask(0);
Mage::app('default');
ini_set('display_errors', 1);
ob_start();
session_start();

Mage::app()->getConfig()->getTempVarDir();
$customer = Mage::getModel('customer/customer')->setWebsiteId(Mage::app()->getWebsite()->getId());
$customerId=$_REQUEST['customer_id'];
$productId=$_REQUEST['product_id'];
$qty=$_REQUEST['qty'];
$action=$_REQUEST['action'];
$i=0;
if(!empty($customerId) && !empty($productId) && !empty($qty) && !empty($action))
{
                $customer=Mage::getModel('customer/customer')->load($customerId);
                if ($customer->getId()) {
                                $storeIds = Mage::app()->getWebsite(Mage::app()->getWebsite()->getId())->getStoreIds();
                                $quote = Mage::getModel('sales/quote')->setSharedStoreIds($storeIds)->loadByCustomer($customer);
                                                if ($quote) {
                                                                $collection = $quote->getItemsCollection(false);
                                                                                if ($collection->count() > 0) {
                                                                                                foreach( $collection as $item ) {
                                                                                                                if($item && $item->getId()) {
                                                                                                                                if($productId==$item->getProductId()){
                                                                                                                                                $i++;
                                                                                                                                                if($action=='delete')
                                                                                                                                                                {             
                                                                                                                                                                                $quote->removeItem($item->getId());
                                                                                                                                                                                $result['status']='success!';
                                                                                                                                                                                $result['delete_update_cart'] ='product remove from the cart!';
                                                                                                                                                                }else if($action=='update')
                                                                                                                                                                                {
                                                                                                                                                                                                $item->setQty($qty);
                                                                                                                                                                                                $result['status']='success!';
                                                                                                                                                                                                $result['delete_update_cart'] ='cart update!';
                                                                                                                                                                                }

                                                                                                                                                                $quote->collectTotals()->save();
                                                                                                                                }
                                                                                                                }
                                                                                                }
                                                                                }
                                                else{
                                                                                $result['status']='failier!';
                                                                                $result['delete_update_cart'] ='product not available in cart!';
                                                                }
                                                }
                }
}
else{
                $result['status']='failier!';
                $result['delete_update_cart']='parameter mising!';
}
if($i>0)
{
echo json_encode($result);       
}else{
                $result['status']='failier!';
                $result['delete_update_cart'] ='product not available in cart!';
                echo json_encode($result);
}
//echo json_encode($result);

?>

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...