show product collection programatically outside magento


<?php
require_once 'app/Mage.php';
umask(0);
Mage::app('default');


function empty_pk($data){
if($data!=''){return $data;}
else {return "&nbsp;";}
}
$collection = Mage::getModel('catalog/product')->getCollection();

echo '<table border="1"> ';
echo '<tr>';    
echo "<th>sku<th />";
echo "<th>name<th />"; 
echo "<th>qty<th />";   
echo "<th>is_in_stock<th />";  
echo "<th>price<th />"; 
echo '</tr>';    

foreach ($collection as $product_all) { 
    //echo '<pre>';print_r($product);echo '</pre>';   
    
$sku = $product_all['sku'];
// retrieve product id using sku
$product_id = Mage::getModel('catalog/product')->getIdBySku($sku);
// call product model and create product object
$product    = Mage::getModel('catalog/product');
// Load product using product id
$product ->load($product_id);


$pk_sku = empty_pk($product['sku']);
$pk_name = empty_pk($product['name']); 

$stockData = $product->getStockData();

$pk_qty = empty_pk($stockData['qty']);
$pk_in_stock = empty_pk($stockData['is_in_stock']);
$pk_price = empty_pk($product['price']);   


echo '<tr>';    
echo "<td>".$pk_sku."<td />";
echo "<td>".$pk_name."<td />";  
echo "<td>".$pk_qty."<td />";  
echo "<td>".$pk_in_stock."<td />";  
echo "<td>".$pk_price."<td />";  

echo '</tr>'; 

}
echo '</table>';      
?>

code 2

<?php
require_once 'app/Mage.php';
umask(0);
Mage::app('default');                                                         
    
    $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attribute_id');
    foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){
    echo $attributeArray[$option['value']] = $option['label'];
    }
    
    /*** Returns all product info ***/
    function get_product_info_all(){
        
        $all_product_obj = Mage::getModel('cataloginventory/stock_item')->getCollection();  /*load all the products*/
        $all_product_info = array();
        
        foreach($all_product_obj as $data){
            
            $product_id = $data['product_id'];
            $product_info = get_product_info($product_id);
            $all_product_info[$product_id] = array_merge($product_info);   
        }
        
        //return $all_product_obj;
        return $all_product_info;   
    }
    
    /*** Returns the product info of the product id passes ***/
    function get_product_info($product_id){
        
        $product_obj = Mage::getModel('catalog/product')->load($product_id);  /*load the product*/
        $stock_obj = Mage::getModel('cataloginventory/stock_item')->load($product_id);  /*load the stock*/     
        
        $product_sku = $product_obj->getSku();
        $product_name = $product_obj->getName();  
        $product_qty = $stock_obj->getQty();
        $product_is_in_stock = $stock_obj->getIs_in_stock();
        $product_price = $product_obj->getPrice();
        
    
        
        
        $product_has_option = $product_obj->getAssociatedProducts(); 
        
        
         $product_type = $product_obj->getTypeId(); 
         
         
          if($product_type == "configurable") {
          //  $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts('NULL',$product_id);
            //->getUsedProducts(null,$product_id);       
          } 
          else{
                      $childProducts = '';
          } 
        
         $product_attribute = $product_obj->getAttributeText('at_tablet_power');    
         
          $product_websiteid = $product_obj->getWebsiteIds();
          $product_storeid = $product_obj->getStoreIds(); 
           $product_description = $product_obj->getDescription();     
               
        
        $product_info = array(
            "product_sku" => $product_sku,
            "product_name" => $product_name,
            "product_qty" => $product_qty,
            "product_is_in_stock" => $product_is_in_stock,
            "product_price" => $product_price,
            "product_type" => $product_type,        
            "product_has_option" => $product_has_option,
            "product_attribute" => $product_attribute,
            "product_websiteid" => $product_websiteid,
            "product_storeid" => $product_storeid,
            "childProducts" => $childProducts,
            "product_description" => $product_description
        );
        return $product_info;    
    }
    
    /*** Example Calls ***/
    //echo "<pre>"; print_r ( get_product_info('4') ); echo "</pre>";
    echo "<pre>"; print_r ( get_product_info_all() ); echo "</pre>";       
    
?> 

Share on Facebook




About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools