Magento 客户以编程方式登录

时间:2022-10-22
本文介绍了Magento 客户以编程方式登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有代码:

function loginUser( $email, $password )
{
    /** @var $session Mage_Customer_Model_Session */
    $session = Mage::getSingleton( 'customer/session' );

    try
    {
        $session->login( $email, $password );
        $session->setCustomerAsLoggedIn( $session->getCustomer() );
        return 1;
    }
    catch( Exception $e )
    {
        return $e;
    }
}

$test = loginUser("login","password");
echo "test";
print_r($test);
die;

但我总是得到

Mage_Core_Exception Object ( [_messages:protected] => Array ( ) [message:protected] => 无效的登录名或密码.

登录名和密码正确,我尝试了很多帐户,结果都一样.

Login and password is correct, I tried many accounts, with the same result.

如何正确登录?

推荐答案

<?php
function loginUser( $email, $password )
    require_once ("app/Mage.php");
    umask(0);
    ob_start();
    session_start();
    Mage::app('default');
    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);
    }catch(Exception $e){

    }


  } 
?>

这篇关于Magento 客户以编程方式登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:Magento:将产品最大数量限制为每个订单 1 个.数量 2 = 2 个订单 下一条:没有了

相关文章

最新文章