首页 文章

检查外部页面上的Magento登录

提问于
浏览
1

我试图在外部页面(相同的服务器,相同的域等等)上访问Magento的项目时在这里碰壁 . 我想看看用户是否登录Magento,然后在网站上显示某些部分 .

请记住,此代码存在于Magento之外 .

Mage::app("default");
Mage::getSingleton("core/session", array("name" => "frontend"));

if (empty($session)) 
{
  $session = Mage::getSingleton("customer/session");
}

if($session->isLoggedIn()) 
  echo "hi";

$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
echo $cart;

$ cart返回0,我的购物车中肯定有产品 . isLoggedIn()也返回false . 我在这做错了什么? Magento中是否有选项可以打开或关闭以便能够在Magento之外访问此信息?

3 回答

  • 0

    使用上面的代码,我在Magento文件夹中创建了一个php文件 . 从那里,添加了购物车中的项目数量,以及您是否登录到数组并将其编码为json . 我在外部页面上使用了一些jquery来获取文件并提取我需要的数据 .

    不太理想的情况,但它现在有效 .

  • 1

    你是否包括Mage.php(定义 getSingleton )?

    require_once ($_SERVER['DOCUMENT_ROOT'] . '/app/Mage.php');
    

    getSingleton() 电话之后,$ session会有什么?

    print_r ($session);
    

    编辑:我在我的结束尝试了这个并且无法获得准确的isLoggedIn()或getItemsCount()数据 . 当我抛出$ session时,它将所有字段显示为“受保护” .

    我对要求用户登录不感兴趣...我只想访问已登录会话中的数据 .

    其他人对此有何看法?所有的例子似乎都在1.4.x之前 .

  • 1
    require_once "app/Mage.php";
    
    umask(0); 
    
    Mage::app();
    
    //  require_once $_SERVER['DOCUMENT_ROOT'] . "/mage1/app/Mage.php";
    
    // Customer Information
    
    $firstname = "krishana";
    
    $lastname = "singh";
    
    $email = "krish.bhati@gmail.com";
    
    $password = "myverysecretpassword";
    
    
    // Website and Store details
    
    $websiteId = Mage::app()->getWebsite()->getId();
    
    $store = Mage::app()->getStore();
    
    $customer = Mage::getModel("customer/customer");
    
    $customer->website_id = $websiteId;
    
    $customer->setStore($store);
    
    
    $mageRunCode = isset ( $_SERVER ['MAGE_RUN_CODE'] ) ? $_SERVER ['MAGE_RUN_CODE'] : '';
    
    $mageRunType = isset ( $_SERVER ['MAGE_RUN_TYPE'] ) ? $_SERVER ['MAGE_RUN_TYPE'] : 'store';
    
    $app = Mage::app ( $mageRunCode, $mageRunType );
    
    Mage::getSingleton('core/session', array('name' => 'frontend')); 
    
    $session = Mage::getSingleton('customer/session');
    
    $session->start();
    
    $customer->loadByEmail($email); 
    
    $customer_id= $customer->getId();
    
    
    if($customer_id){   
    
    Mage_Core_Model_Session_Abstract_Varien::start();
    
    $session->login($email, $password);
    
    $session->setCustomerAsLoggedIn($session->getCustomer()); 
    
    echo $session->isLoggedIn() ? $session->getCustomer()->getName().' is online!' : 'not logged in';
    
    
    }
    

相关问题