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

+Import products via external script (small issue)
 
zep007
Sr. Member
 
Avatar
Total Posts:  131
Joined:  2008-05-15
Connecticut
 

I am creating products via a php script which will hooks into an api, and will run 10 times a day.  the script needs to query the API, parse the products’ fields, then decide if they need to be updated (stock qty) or need to be imported into hte catelog.

My issue lies in finding out if the product exists by looking up its SKU… I have been trying to get this bit of code to work:

require_once $dpath;    //intialize Magento enviornment
Mage::app($si);           //declare which store
//create a new product

$exists Mage::getModel('cataloginventory/stock_item');

if (
$exists->getSku('4you6')) {
    
echo "Hey Knucklehead! that product already exists!";


else{                                                  
//if the product does not exist do this

    
try {
    
//if ->getSku() = '4you';  //need to see if product exists




  
$newProduct Mage::getModel('catalog/product')
       
    ->
setStoreId($si)
    ->
setCategoryIds($cid)    //Works //apparel-->shirts
    
->setAttributeSetId($asid)  //Works//shirts attributes
    
    
->setSku('4you6')        //THIS MUST BE UNIQUE!! 
    
->setPrice(15.49)        //Works   
    
->setName('4You T Shirt 4')  //Works
    
->setManufacturer(20)    //Works
    
->setStatus(1)          //Works - 1 assumes enabled
    
->setWeight(5)          //Works 
    
->setCost(13.50)        //Works 
    
->setMetaDescription('Descript'//Works
    
->setShortDescription('short-desc'//Works
    
->setDescription('Desc')            //Works
    
->setModel('4you4')                 //Works
    
->setMetaTitle('test')              //Works
    
          //new try   
    
    
->setBackorders(2)                //does not work
    
->setStockAvailability(1)         //does not work
    
->setMetaKeywords('keywords')     //does not work  change MetaKeywords to MetaKW        
    
->setQty(10)                      //does not work    
    
->setImage('test.gif')            //does not work 
    
    //can also specify Related Products, Upsells, crosssells   
    
->save(); 
    
  echo 
'OK Product ID: '.$newProduct->getId();
}
catch (Mage_Core_Exception $e{
  
echo $e->getMessage();
}
catch (Exception $e{
  
echo $e;
}
}

The first time a product is encountered, it is inputted fine, but the second time my script does not realize the SKU exists, and tries to force feed another product in there, screwing up my database, until I delete the product and start over.

So how do I detect if the SKU already exists???

 
Magento Community Magento Community
Magento Community
Magento Community
 
zep007
Sr. Member
 
Avatar
Total Posts:  131
Joined:  2008-05-15
Connecticut
 

hummmmm… I seem to be making a habit of answering my own posts..

anyways, here is a little function that checks if a sku exists, even though I would have liked to use a Magento built in method

function checkSku($sku2check){     
$username 
"yourUserName";
$password "YourPassWord";
$hostname "10.000.000.00"
$database "Mage_DB";
$table 'catalog_product_entity';
$row1 'sku';

$dbh mysql_connect($hostname$username$password);  
$selected mysql_select_db($database,$dbh);                                                                                          
$quer3=mysql_query("SELECT * FROM $table WHERE $row1='$sku2check'");
$r mysql_fetch_array($quer3);
if (!
$r{
   
echo 'this product does not yet exist!!';
   
$total 'original';
   return 
$total;  


$mod2 
$r[type_id];
if (
$mod2 'simple'{
$total 
'duplicate';

return $total
}

Well there you go, drop in you DB info and check for SKUs, hey if this helped you, search for my unanswered posts and drop me a line!!  LOL

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top