How do I create a Magento session outside of Magento?

前端 未结 4 778
傲寒
傲寒 2020-12-05 17:04

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

require \'app/Mage.php\';
$mageRunCode = isset ( $_         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 17:35

    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.

提交回复
热议问题