Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

I need to get the partID of a product using the sku
 
ddoddsr
Jr. Member
 
Total Posts:  24
Joined:  2012-02-08
 

I can get the part object but I need just the PartID to supply a later function.  (Moved from How Do I...)

<?php
require_once '../mage1/app/Mage.php';
$app Mage::app();
Mage::register('isSecureArea'true); //no output before here, will get a session header error

// get csv file into array or object

$csvfilepath ='\xampp\htdocs\RemoteTest\bulk.txt';

    
$fp fopen($csvfilepath,'r'or die("can't open file");          // remove header 
    
$csv_line fgetcsv($fp,1024);  
    
$i 0;
    while(
$csv_line fgetcsv($fp,1024)) {
        
if( $csv_line === true ) die('PAY ATTENTION DUMMY');
        
$PartCart[$i]=array(
//            $product1 = Mage::getModel('catalog/product')->loadByAttribute('sku', $csv_line[0]);
//            $PartCart[$i]=array('PartId'=>$product1->$entity_id, 
//Right here I'd like just to have the partid in the $PartCart array  so it looks like:  $PartCart=array(array("PartId"=>"4954", "$qty"=>"1"));
            
"Partid"=>$product1 Mage::getModel('catalog/product')->loadByAttribute('sku'$csv_line[0]), 
//            "sku"=>$csv_line[0], 
            
"qty"=>$csv_line[1]
//            "UnitCost"=>$csv_line[2]
            
);
        
$i++;
    
}
    fclose
($fp) or die("can't close file");   
    
var_dump$PartCart) ;

Thanks Dan

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

hello
If you do this:

$product Mage::getModel('catalog/product')->loadByAttribute($sku'sku');
You can get the product id like this
$id $product->getId();
But there is a simpler way so you won’t have to load all the product data.
before looping through the csv rows do this:
$productModel Mage::getModel('catalog/product');
and inside you loop you can get the id like this:
$id $productModel->getIdBySku($sku);

Cheers,
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ddoddsr
Jr. Member
 
Total Posts:  24
Joined:  2012-02-08
 

Marius,
Thanks those both worked. Yes the 2nd was cleaner..
Dan

<?php
require_once '../mage1/app/Mage.php';
$app Mage::app();
Mage::register('isSecureArea'true); //no output before here, will get a session header error

// get csv file into array or object

$csvfilepath ='\xampp\htdocs\RemoteTest\bulk.txt';

    
$fp fopen($csvfilepath,'r'or die("can't open file");          // remove header 
    
$csv_line fgetcsv($fp,1024);  
    
$i 0;
    
$productModel Mage::getModel('catalog/product');
    while(
$csv_line fgetcsv($fp,1024)) {
        
if( $csv_line === true ) die('PAY ATTENTION DUMMY');
        
$PartCart[$i]=array("PartId"=>$productModel->getIdBySku(), 
//            "sku"=>$csv_line[0], 
            
"qty"=>$csv_line[1]
            
"UnitCost"=>$csv_line[2]
            
);
        
$i++;
    
}
    fclose
($fp) or die("can't close file");   
    
var_dump$PartCart) ;
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top