Search code examples
phpjoomlavirtuemart

Access virtuemart product data externally


Is there a quick fire method of accessing all the data for a product from an external file by using its sku?

I have looked at How can I get prodcut data of a VirtueMart 2 product in an external file?

and tried to replace the Product ID with my desired id but to no avail:

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);

In short terms, I'm looking for a way to access product data.


Solution

  • Product sku may not be unique. so I have found a solution that works for me. Hope this helps you. This is the full code that you need to call product by sku externally.

    <?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));
    }