Import Products Programmatically ,Get Products image from external URL , Update if product SKU id exists and redirect to particular page
<?php
$product = $_REQUEST;
include_once("app/Mage.php");
Mage::app();
$newproduct = Mage::getModel('catalog/product');
$sku = $product['product'];
$quant = $product['available'];
$productId = Mage::getModel('catalog/product')->getCollection();
$skuid = array();
foreach ($productId as $check) {
$i = $i+1;
$skuid[] = $check->getSku();
}
if (!in_array($sku,$skuid)) {
$newproduct->setSku($product['product']);
$newproduct->setTypeId('simple');
$newproduct->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$newproduct->setStatus(1);
$newproduct->setWeight($product['weight']);
$newproduct->setTaxClassId(0);
$newproduct->setWebsiteIDs(array(1));
$newproduct->setStoreIDs(array(1));
$newproduct->setStockData(array(
'is_in_stock' => 1,
'qty' =>$quant ,
'manage_stock' => 1
));
$newproduct->setAttributeSetId(4);
$newproduct->setName($product['product']);
$newproduct->setCategoryIds(array(2,3)); // array of categories it will relate to
$newproduct->setDescription($product['productText']);
$newproduct->setShortDescription($product['productText']);
$newproduct->setBrand($product['brand']);
$newproduct->setBrandtext($product['brandText']);
$newproduct->setList($product['list']);
$newproduct->setPrice($product['price']);
$newproduct->setCat($product['category']);
$newproduct->setCategorytext($product['categoryText']);
$newproduct->setYear($product['year']);
$newproduct->setMake($product['make']);
$newproduct->setModel($product['model']);
$newproduct->setCore($product['core']);
$newproduct->setShips($product['ships']);
$newproduct->setApplication($product['application']);
$newproduct->setCatalogproduct($product['catalogProduct']);
$newproduct->setPartnersession($product['partnerSession']);
$from = $product['imageurl'] ;
$prod = $product['imageurl'];
$prod1 = explode("/",$prod);
$prod2 = end($prod1);
$to = "/home/domains/spd1/media/import/".$prod2 ;
copy($from,$to);
$fullImagePath = "/home/domains/spd1/media/import/".$prod2;
$visibility = array (
'thumbnail',
'small_image',
'image'
);
$newproduct->addImageToMediaGallery( $fullImagePath, $visibility, true, false);
} else {
$newproduct1 = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$newproduct1->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$newproduct1->setStatus(1);
$newproduct1->setWeight($product['weight']);
$newproduct1->setTaxClassId(0);
$newproduct1->setAttributeSetId(4);
$newproduct1->setName($product['product']);
$newproduct1->setCategoryIds(array(2,3)); // array of categories it will relate to
$newproduct1->setDescription($product['productText']);
$newproduct1->setShortDescription($product['productText']);
$newproduct1->setBrand($product['brand']);
$newproduct1->setBrandtext($product['brandText']);
$newproduct1->setList($product['list']);
$newproduct1->setPrice($product['price']);
$newproduct1->setCat($product['category']);
$newproduct1->setCategorytext($product['categoryText']);
$newproduct1->setYear($product['year']);
$newproduct1->setMake($product['make']);
$newproduct1->setModel($product['model']);
$newproduct1->setCore($product['core']);
$newproduct1->setShips($product['ships']);
$newproduct1->setApplication($product['application']);
$newproduct1->setCatalogproduct($product['catalogProduct']);
$newproduct1->setPartnersession($product['partnerSession']);
try {
if (is_array($errors = $newproduct1->validate())) {
$strErrors = array();
foreach($errors as $code=>$error) {
$strErrors[] = ($error === true)? Mage::helper('catalog')->__('Attribute "%s" is invalid.', $code) : $error;
}
$this->_fault('data_invalid', implode("\n", $strErrors));
}
$newproduct1->save();
} catch (Mage_Core_Exception $e) {
$this->_fault('data_invalid', $e->getMessage());
}
}
try {
if (is_array($errors = $newproduct->validate())) {
$strErrors = array();
foreach($errors as $code=>$error) {
$strErrors[] = ($error === true)? Mage::helper('catalog')->__('Attribute "%s" is invalid.', $code) : $error;
}
$this->_fault('data_invalid', implode("\n", $strErrors));
}
$newproduct->save();
} catch (Mage_Core_Exception $e) {
$this->_fault('data_invalid', $e->getMessage());
}
echo $extra = strtolower($product['product']);
header("Location: redirect path");
?>