在 Magento 之外但在同一域中获取购物车内容

2023-12-07

我需要以某种方式使 Magento 商店中当前的购物车和客户信息可供 Magento 之外的网站的其余部分访问。

例如,mysite.com/blog 位于 mysite.com/store 之外。

在我的域的基础上,我运行了这段代码,但它只返回 NULL。

require_once 'store/app/Mage.php';
umask(0);
Mage::app();

Mage::getSingleton('core/session', array('name'=>'frontend'));
$totalItems = Mage::getModel('checkout/cart')->getQuote();

$cart = Mage::getModel('checkout/cart')->getQuote()->getAllItems();
foreach ($cart->getAllItems() as $item) {
    $productName = $item->getProduct()->getName();
    $productPrice = $item->getProduct()->getPrice();
}

这是一个工作的独立文件:

<?php
    require_once( 'app/Mage.php' );

    umask(0);
    Mage::app('default');

    // This has to run to authenticate customer and checkout session calls.
    Mage::getSingleton('core/session', array('name' => 'frontend'));

    // Get any customer model you desire.
    $oSession = Mage::getSingleton( 'customer/session' );
    $oCustomer = $oSession->getCustomer();
    $oCheckout = Mage::getSingleton( 'checkout/session' );
    $oQuote = $oCheckout->getQuote();

    var_dump( $oCustomer );
    var_dump( $oSession );
    var_dump( $oQuote );
    var_dump( $oCheckout );

    $oCart = $oQuote->getAllItems();
    if( !empty( $oCart ) )
    {
        foreach ( $oCart as $oItem ) 
        {
            $sName  = $oItem->getProduct()->getName();
            $fPrice = $oItem->getProduct()->getPrice();
            var_dump( $sName );
            var_dump( $fPrice );
        }
    }
?>

其他域的更多参考:

如何从 Magento 外部访问 Magento 客户会话?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Magento 之外但在同一域中获取购物车内容 的相关文章

随机推荐