如何在Magento之外创建Magento会话? [英] How do I create a Magento session outside of Magento?

查看:87
本文介绍了如何在Magento之外创建Magento会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用下面流行的方法很好地访问Magento之外的现有会话.

require 'app/Mage.php';
$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' ) );

这很好用,但是我如何在Magento之外真正创建一个Magento会话,该会话将填充log_url,log_visitor等表格并为该会话分配访问者数据? >

当前,用户直接从另一个网站到达我网站上的页面.此特定页面位于Magento的外部,但我需要使用以下代码访问其访问者ID:

Mage::getSingleton ( 'log/visitor' )->getId()

如果用户以前曾去过我的Magento商店,则此方法工作正常,但如果不是,则仅返回布尔值false.我想做的是检查是否为访问者ID设置了一个值,如果没有,请在Magento之外的第一页上创建访问者,这样我就可以在此页面上使用访问者ID.同样重要的是,一旦用户进入我的Magento商店,在他们浏览我的目录(即同一会话)的整个过程中,将应用相同的访客ID".有什么想法吗?

解决方案

好,我已经弄清楚了.尽管我必须承认这不是最干净的解决方案,但它的工作方式完全符合我的期望.对于其他想要这样做的人,我在下面粘贴了我的代码摘录:

require 'app/Mage.php';
$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 );
$core_session = Mage::getSingleton ( 'core/session', array ('name' => 'frontend' ) );
$write = Mage::getSingleton ( 'core/resource' )->getConnection ( 'core_write' );

$url = Mage::getUrl ( '*/*/*', array ('_current' => true ) );

Mage::getSingleton ( 'core/session' )->setLastUrl ( $url );

$visitor_id = $_SESSION ['core'] ['visitor_data'] ['visitor_id'];

if (! empty ( $visitor_id )) {
    Mage::getSingleton ( 'log/visitor' )->setId ( $visitor_id );
} else {
    Mage::getSingleton ( 'customer/session' )->setWishlistItemCount ( 0 );
    Mage::getSingleton ( 'catalog/session' )->setCatalogCompareItemsCount ( 0 );

    $write->query ( "INSERT INTO log_url_info (url, referer) VALUES (?, ?)", array ($url, Mage::helper ( 'core/http' )->getHttpReferer ( true ) ) );
    $url_id = $write->lastInsertId ();
    $log_visitor = Mage::getSingleton ( 'log/visitor' )->initServerData ()->setFirstVisitAt ( now () )->setIsNewVisitor ( true )->setLastVisitAt ( now () )->setLastUrlId ( $url_id )->save ();
    $write->query ( "INSERT INTO log_url (url_id, visitor_id, visit_time) VALUES (?, ?, ?)", array ($url_id, $log_visitor->getId (), now () ) );
    $core_session->setVisitorData ( $log_visitor->getData () );

    $visitor_id = $log_visitor->getId ();
}

我希望这对其他人有帮助,这样他们就不会像我以前那样扯掉头发了.

I am able to access an existing session outside of Magento perfectly fine using the popular method below.

require 'app/Mage.php';
$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' ) );

This works great, but how do I actually create a Magento session outside of Magento that would populate the log_url, log_visitor, etc. tables as well as assigning the visitor data to the session?

Currently, the user arrives at a page on my site directly from another website. This specific page is outside of Magento but I need to access their Visitor ID using the following code:

Mage::getSingleton ( 'log/visitor' )->getId()

This works fine if the user has previously been to my Magento store, but if not, it just returns a boolean false. What I would like to do is check to see if there is a value set for the Visitor ID and if not, create the visitor on this first page which is outside of Magento so I can use the Visitor ID right on this page. It is also important that as soon as the user enters my Magento store, the same Visitor ID will be applied throughout their navigation of my catalog, i.e. the same session. Any ideas?

解决方案

Well, I've figured it out. Although I must admit it's not the cleanest solution, it works exactly as I had hoped. For anyone else who's looking to do this, I've pasted my code excerpt below:

require 'app/Mage.php';
$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 );
$core_session = Mage::getSingleton ( 'core/session', array ('name' => 'frontend' ) );
$write = Mage::getSingleton ( 'core/resource' )->getConnection ( 'core_write' );

$url = Mage::getUrl ( '*/*/*', array ('_current' => true ) );

Mage::getSingleton ( 'core/session' )->setLastUrl ( $url );

$visitor_id = $_SESSION ['core'] ['visitor_data'] ['visitor_id'];

if (! empty ( $visitor_id )) {
    Mage::getSingleton ( 'log/visitor' )->setId ( $visitor_id );
} else {
    Mage::getSingleton ( 'customer/session' )->setWishlistItemCount ( 0 );
    Mage::getSingleton ( 'catalog/session' )->setCatalogCompareItemsCount ( 0 );

    $write->query ( "INSERT INTO log_url_info (url, referer) VALUES (?, ?)", array ($url, Mage::helper ( 'core/http' )->getHttpReferer ( true ) ) );
    $url_id = $write->lastInsertId ();
    $log_visitor = Mage::getSingleton ( 'log/visitor' )->initServerData ()->setFirstVisitAt ( now () )->setIsNewVisitor ( true )->setLastVisitAt ( now () )->setLastUrlId ( $url_id )->save ();
    $write->query ( "INSERT INTO log_url (url_id, visitor_id, visit_time) VALUES (?, ?, ?)", array ($url_id, $log_visitor->getId (), now () ) );
    $core_session->setVisitorData ( $log_visitor->getData () );

    $visitor_id = $log_visitor->getId ();
}

I hope this helps someone else so they're not ripping their hair out like I was.

这篇关于如何在Magento之外创建Magento会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆