从外部访问virtuemart产品数据

时间:2016-07-17 13:55:03

标签: php joomla virtuemart

是否存在使用其sku从外部文件访问产品的所有数据的快速方法?

我看过How can I get prodcut data of a VirtueMart 2 product in an external file?

并尝试用我想要的ID替换产品ID,但无济于事:

if (!class_exists( 'VmConfig' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if (!class_exists( 'VmModel' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'vmmodel.php');

$productModel = VmModel::getModel('Product');
$product = $productModel->getProduct(Product_ID);

简而言之,我正在寻找一种访问产品数据的方法。

1 个答案:

答案 0 :(得分:1)

产品sku可能不是唯一的。所以我找到了一个适合我的解决方案。希望这对你有所帮助。这是您通过外部sku调用产品所需的完整代码。

<?php

define( '_JEXEC', 1 );

define('JPATH_BASE', 'C:\Server\www\joomla' );//you will have diff location for your site

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');
$mainframe->initialise();

function getproductBySKU($sku){
  $db = JFactory::getDbo();
  $db->setQuery(true);
  $db->setQuery("SELECT virtuemart_product_id FROM #__virtuemart_products WHERE product_sku= $sku");
  $productids = $db->loadAssocList();
  return $productids;
}

function getProduct($id)
{
    if (!class_exists('VmConfig')) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'helpers' . DS . 'config.php');
    VmConfig::loadConfig();
    if (!class_exists('VmModel')) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'helpers' . DS . 'vmmodel.php');

    $productModel = VmModel::getModel('Product');
    $product = $productModel->getProduct($id);
    return $product;
}

$products = getproductBySKU(1);//In this example SKU is 1 having 2 products
var_dump($products);//Gives the product id's in the SKU
foreach($products as $product){
    var_dump(getProduct($product));
}
相关问题