drqvsx1228 2015-02-16 16:28 采纳率: 0%
浏览 53

Magento API:如何按ID加载客户会话

I'm working on magento module to log in customer with API.

I can log in customer and get frontend sessionId, but when I want to load session with this sessionId to check if the customer is already logged in I can't.

Here the API function I used:

public function login($email,$password,$storecode){
    $result = array();
    $result['code'] = '';
    $result['messages'] =  '';
    try{
        $session = Mage::getSingleton('customers/session');
        $storemodel = Mage::getModel('core/store')->load($storecode);
        $store_id = $storemodel->getId();
        $websiteId = $storemodel->getWebsiteId();
        if($session->loginByApi($email, $password,$websiteId)){
            $result['code'] = 'SUCCESS';
            $result['sessionId'] = $session->getSessionId();
            $customer = $session->getCustomer();
            $result['customer']= array(
                'customerid' => $customer->getId(),
                'firstname' => $customer->getFirstname(),
                'lastname' => $customer->getLastname(),
            );
        }
    } catch (Mage_Core_Exception $e) {
        $result['code'] = 'ERRORS';
        $result['messages'] =  $e->getMessage();
    }
    return $result;
}

public function isloggedin($customerId,$customersessionId ,$storecode){
    if(!isset($storecode)){
        $storecode = 'default';
    }

    Mage::app($storecode, 'store');

    $core_session = Mage::getSingleton('core/session', array('name'=>'frontend'));
    if($customersessionId != null){
        $core_session->setSessionId($customersessionId);
        $core_session->start('frontend');
    }

    $session = Mage::getSingleton('customer/session', array('name'=>'frontend'));

    $customer = Mage::getModel('customer/customer')->load($customerId);
    $session->setCustomer($customer);


   if($session->isLoggedIn()){
        $session->setCustomerAsLoggedIn($customer);
        $result['sessionId'] = $session->getSessionId();
    }else{
        $result['logged'] = false;
    }
    return $result;

}

Anyone have an idea?

  • 写回答

2条回答 默认 最新

  • douzen1896 2015-02-16 17:09
    关注

    Not sure if this helps too much, but this code:

    Mage::app('2', 'store');
    $s = Mage::getSingleton('customer/session', array('name'=>'frontend'));
    $c = Mage::getModel('customer/customer')->load(1);
    $s->setCustomer($c);
    
    if($s->isLoggedIn()){
        echo $c->getName()." is logged in, session: ".$s->getSessionId().PHP_EOL;
    } else {
        echo "not logged in".PHP_EOL;
    }
    

    Did seem to work for me:

    John Smith is logged in, session: d3rcgvd56md4u3cfctcvnt2ou6
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line