在joomla框架之外显示购物车总数

时间:2016-05-30 23:43:34

标签: php joomla

我一直在努力解决这个问题。

我让我的joomla / virtuemart在名为“store”的目录中运行,我希望在joomla框架之外显示购物车中的产品总数。所以我想出了这个代码,它适用于旧版本的virtuemart(< v3)。但是,在使用Virtuemart 3.0.16进行尝试时,我遇到了这些问题:

代码(位于名为“includes”的目录中)

<?php

// Set flag that this is a parent file

define( '_JEXEC', 1 );

define('JPATH_BASE', dirname(realpath(__FILE__)). '/../store' );

define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');

$mainframe = JFactory::getApplication('site');

    $array = unserialize($_SESSION['__vm']['vmcart']); 
    $total = 0;
    foreach($array->products as $product){
        $total += $product->amount;
    }
    echo "" . $total;
?>

错误:

  

警告:为foreach()提供的参数无效   第20行/home/me/public_html/includes/header.php

     

0

其中0代表购物车总数,应该显示为1,因为当时我有购物车中的商品

我不是一个PHP专家,但打开谷歌搜索,在我看来,我的$数组不是导致问题的数组?

这让我很困惑,因为它在旧版本的virtmart中运行良好。

-

鉴于以下答案,我试图运行这个:

<?php
// Set flag that this is a parent file

define( '_JEXEC', 1 );

define('JPATH_BASE', dirname(realpath(__FILE__)). '/../store' );

define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');

$mainframe = JFactory::getApplication('site');
defined('DS') or define('DS', DIRECTORY_SEPARATOR);

     if (!class_exists( 'VmConfig' ))
     require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');

     if(!class_exists('VirtueMartCart'))
     require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
echo sizeof($cart->products);
    ?>

......但没有成功。它不断说0

1 个答案:

答案 0 :(得分:0)

试试这个,

加载Joomla框架后,只需使用下面的代码即可使用

     defined('DS') or define('DS', DIRECTORY_SEPARATOR);

     if (!class_exists( 'VmConfig' ))
     require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');

     if(!class_exists('VirtueMartCart'))
     require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');

        $cart = VirtueMartCart::getCart();
        if(sizeof($cart->products) > 0){
          echo "<pre/>";
          print_r($cart);
        }
        else{
         echo 'Your cart is empty';
        }

数组中包含所有购物车商品详情,只需选择您想要的商品。

希望它有所帮助。