Magento – Configurable products not visible until manually saved in admin panel

Solved this by going through the saveAction in the ProductController.

I wasnt setting the websiteIds correctly so the new products were never getting added to the site correctly.

Magento uses this method to get the currect website:

->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()))

 

protected function checkFile(){
// echo ‘ check file ‘.$this->file_name;
$media_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
$store_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$file_path = $media_url.’csv/’.$this->file_name.’.csv’;
// $file_path = $media_url.’csv/’.$this->file_name.’.txt’;
// $file_path = ‘/media/csv/’.$this->file_name.’.txt’;
// echo ‘ check file ‘.$file_path.’ ‘.$store_url;
$this->fileFunc($file_path,$store_url);
// $this->resave();

}
protected function fileFunc($file,$lib){
//define(‘MAGENTO_MAGE_LOCATION’,$lib.’app/Mage.php’);
//error_reporting(E_ALL);
if($handle = fopen($file, “r”)){
$count=0; $mediaAttribute = array(‘thumbnail’,’small_image’,’image’);
$imgUrl = ‘media/catalog/product/’;
// $imgUrl = ‘media/csv/img/’;
while (($data = fgetcsv($handle)) !== FALSE)
{
$count++;
// print_r($data); echo ‘<br />’;
// $cat = Mage::getResourceModel(‘catalog/category_collection’)->addFieldToFilter(‘name’, $data[0]);
// $catId = $cat->getFirstItem()->getEntityId();
// echo $catId.'<hr><br />’;
// echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).’csv/i/’.$data[17].'<br /><hr><br />’;
// print_r($data);echo ‘<hr><br />’;
// /*
if($count > 1)
{
$cat = Mage::getResourceModel(‘catalog/category_collection’)->addFieldToFilter(‘name’, $data[0]);
$catId = $cat->getFirstItem()->getEntityId();

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel(‘catalog/product’);

try{
$store_id = Mage::app()->getStore()->getStoreId();
$store_website_id = Mage::app()->getStore()->getWebsiteId();
$product
->setStoreId($store_id)
// ->setWebsiteId($store_website_id)
->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()))
// ->setWebsiteIds(array(1))
->setAttributeSetId(64)
->setTypeId(‘simple’)
->setCreatedAt(strtotime(‘now’))
->setSku($data[1])
->setName($data[2])
->setWeight($data[3])
->setStatus(1)
->setTaxClassId(2)
// ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setVisibility(4)
->setPrice($data[5])
->setCost($data[6])
->setSpecialPrice($data[7])
->setSpecialFromDate($data[8])
->setSpecialToDate($data[9])
->setMetaTitle($data[10])
->setMetaKeyword($data[11])
->setMetaDescription($data[12])
->setDescription($data[13])
->setShortDescription($data[14])
// ->addImageToMediaGallery(‘media/catalog/product/demo.jpg’, array(‘image’,’thumbnail’,’small_image’), false, false)
// ->addImageToMediaGallery($imgUrl.’demo.jpg’, array(‘image’,’thumbnail’,’small_image’), false, false)
->addImageToMediaGallery($imgUrl.$data[15], array(‘image’,’thumbnail’,’small_image’), false, false)
// ->addImageToMediaGallery($data[15], array(‘image’,’thumbnail’,’small_image’), false, false) //assigning image, thumb and small image to media gallery
// ->addImageToMediaGallery( $imgUrl , $mediaAttribute, false, false )
->setStockData(
array(
‘use_config_manage_stock’ => 0, //’Use config settings’ checkbox
‘manage_stock’=>$data[16], //manage stock
‘min_sale_qty’=>$data[17], //Minimum Qty Allowed in Shopping Cart
‘max_sale_qty’=>$data[18], //Maximum Qty Allowed in Shopping Cart
‘is_in_stock’ => $data[19], //Stock Availability
‘qty’ => $data[20] //qty
)
)
->setCountryOfManufacture(‘AF’)
->setNewsFromDate(’06/26/2014′)
->setNewsToDate(’06/26/2019′)
->setSize($data[21])->setColor($data[4])->setCategoryIds(array($catId));
//
if(!is_null($data[22])){$product->addImageToMediaGallery( $imgUrl.$data[22] , null, false, false );}
if(!is_null($data[23])){$product->addImageToMediaGallery( $imgUrl.$data[23], null, false, false );}
if(!is_null($data[24])){$product->addImageToMediaGallery( $imgUrl.$data[24] , null, false, false );}
$product->save();

echo ‘save success’.'<hr><br />’;

}catch(Exception $e){
Mage::log($e->getMessage());
echo ‘while saving product ‘.$this->errorFunc();
}
}//if statement ends here

// */

}// end of while loop
fclose($handle);
}else{
echo ‘<br />not yet<br />’;
$this->errorFunc();
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s