How to add product in a cart with Logged In user SOAP API magento?

hey, i am working on magento api and i want to add products from android and the added products should show in my account but its not showing any product which i have added i dont know whats the problem i think its session issue can please anyone see in this matter thanks.

Add Comment
1 Answer(s)

hey, you can use this code i am using this code and its working fine for me and adding products in my account

 

try{         

       $session = Mage::getSingleton(“core/session”, array(“name” => “frontend”));

       $user = $_REQUEST[‘user’];

       $pass = $_REQUEST[‘pass’];

//load product which is passed in query string

       $product = Mage::getModel(‘catalog/product’)->load($_REQUEST[‘product_id’]);

       Mage::getSingleton(“core/session”, array(“name” => “frontend”));

            //getting website id  

       $websiteId = Mage::app()->getWebsite()->getId();

//getting store id

       $store = Mage::app()->getStore();

//getting customer

       $customer = Mage::getModel(“customer/customer”);

//assigning customer website id

       $customer->website_id = $websiteId;

 //assigning customer store id

       $customer->setStore($store);

//loading customer by mail id

        $customer->loadByEmail($user);

 // flag to check if customer is logged in or not

         if(!Mage::getSingleton(‘customer/session’)->isLoggedIn())

  {

// logging in a customer and starting a session  

                    $session = Mage::getSingleton(‘customer/session’)

                                 ->setCustomerAsLoggedIn($customer);

// login user

        $session->login($user, $pass);

 }

$quote = Mage::getModel(‘sales/quote’)->loadByCustomer($customer);

// set store id

$quote->setStoreId($storeId);

$data=array();

$stockProduct = Mage::getModel(‘cataloginventory/stock_item’)

                          ->loadByProduct($product);

  $productActualQty = $stockProduct->getQty();

   $data[‘cart_total’] = getCartItemcount();

   if($product->getStockItem()->getIsInStock())

{

     $quote = Mage::getSingleton(‘checkout/session’)->getQuote();

     $_item = $quote ? $quote->getItemByProduct($product) : false;

     $cartItemQty = $_item ? $_item->getQty() : 0;

     $remaningQty=$productActualQty – $cartItemQty;

     if($remaningQty > 0 )

{

    if (isset($_REQUEST[‘opt_value’])) {

            $options = array();

foreach($_REQUEST[‘opt_value’] as $attribute_code => $option){

$attributeId =      Mage::getResourceModel(‘eav/entity_attribute’)

->getIdByCode(‘catalog_product’, $attribute_code);       $options[$attributeId]= $option;

}

$params = array(

“product_id” => $product->getId(),

“qty” => $_REQUEST[‘qty’],

“super_attribute” => $options

);

} else {

$params = array(

“product_id” => $product->getId(),

“qty” => $_REQUEST[‘qty’]

);

}

$quote = Mage::getSingleton(‘checkout/cart’)->init();

$quote->addProduct($product, $params);

$quote->save();

$session->setCartWasUpdated(true);

$data[‘status’]=’SUCCESS’;

echo json_encode($data);

}

Else {

$data[‘status’]=’Out Of stock’;

echo json_encode($data);

}

}

// add product and its qty

else

{

$data[‘status’]=’Out Of stock’;

echo json_encode($data);

}

// save

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

//header(‘Location: http://stage.tools4trade.co.uk/checkout/cart/’);

} catch( Exception $e ) {

$data=array();

if(!Mage::getSingleton(‘customer/session’)->isLoggedIn()) {

           $data[‘status’]=’FAILED’;

echo json_encode ($data);

}

}

Answered on June 18, 2018.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.