0

I want to add product in a shopping cart for guest user and if product is out of stock then it should show message that product is out of stock otherwise add product in the cart. I am using Magento API . Please help.

1 Answer 1

0

Just try this. Get the cart-id from the query string and pass it in $_REQUEST[‘cart_id’]

Use this code to add product in a cart for guest user

try {

// if we already have cartid then pass otherwise create new cart with the help of  else part        

if(isset($_REQUEST['cart_id'])) {

      $cart_id = $_REQUEST['cart_id'];

} else {

    $cart_id = $client->call( $sessionId, 'cart.create', array( $storeId ) );

}



$product_id = $_REQUEST['product_id'];

$product = Mage::getModel('catalog/product')->load($product_id);

$qty = $_REQUEST['qty'];



if (isset($_REQUEST['opt_value'])) {

      $options=$_REQUEST['opt_value'];  

                      $options_arr = array();

      foreach($options as $attribute_code => $opt) {

        $attributeId = Mage::getResourceModel('eav/entity_attribute')

        ->getIdByCode('catalog_product', $attribute_code);

        $options_arr[$attributeId]= $opt;

         }

         $arrProducts = array(array(

            "product_id" => $product_id,

            "qty" => $qty,

            "super_attribute" => $options_arr

        ));

         } else {

          $arrProducts = array(array(

                "product_id" => $product_id,

             "qty" => $qty

        ));

        }   

      $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 )

                { 

                     $resultCartProductAdd =        $client->call($sessionId,"cart_product.add",array($cart_id,$arrProducts));





        $data['cart_id'] = $cart_id;

        $data['status'] = 'SUCCESS';

        echo json_encode($data);



        }

        else

              {

             $data['status']='Out Of stock';    

              echo json_encode($data); 



               }

        }

         else {

             $data['status']='Out Of stock';

             echo json_encode($data);

            }



    }

    catch(Exception $ex) {

        echo $ex->getMessage();

        $data=array();

        $data['status']='FAILED';

        echo json_encode($data);



    }

Make sure your cart is active or otherwise set your cart active and then proceed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.