Tuesday, January 5, 2016

Recently Viewed Product API in magento

<?php
require_once("../../app/Mage.php");
umask(0);
Mage::app();
ini_set('display_errors', 1);

 require_once("../../app/Mage.php");
umask(0);
Mage::app();
ini_set('display_errors', 1);

    $email = $_REQUEST['email'];
    $password = $_REQUEST['password'];
    $productId=2;
   
   
    Mage::getSingleton("core/session", array("name" => "frontend"));   
    $websiteId = Mage::app()->getWebsite()->getId();
    $store = Mage::app()->getStore();
    $customer = Mage::getModel("customer/customer");   
    $customer->website_id = $websiteId;
    $customer->setStore($store);
   
     try
    {       
        $customer->loadByEmail($email);
        $session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);       
        $session->login($email, $password);
        $customerData = Mage::getModel('customer/customer')->load($session->getId())->getData();
        json_encode($customerData['email']);
       
        if($customerData['email']):                        /*     checking db emails      */
            //$result['result'] = 'logged in';
            //$result['getdata'] = $customerData;
            $customerData = Mage::getSingleton('customer/session')->getCustomer();
            $customerId = $customerData->getId();
            $collection = Mage::getSingleton('Mage_Reports_Block_Product_Viewed')->getItemsCollection();
            //$data['recently_viewed']=$collection->getData();
            $i=0;
            foreach($collection as $item)
            {
                $products[$i]['id'] = $item->getId();
                $products[$i]['name'] = $item->getName();
                $products[$i]['price'] = $item->getPrice();   
                $products[$i]['image'] = (string)Mage::helper('catalog/image')->init($item, 'thumbnail');
                $i++;
            }
            $result['recently_viewed'] =$products;
            echo json_encode($result);
        endif;
    }
    catch(Exception $e){
            $result['result'] = 'Wrong Credentials! Please login First.';
            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...