Magento – Products not showing in frontend

importproductprogrammatically

I am programmatically importing a list of products in Magento 1.8.1.0.

Although the products are enabled they are not showing in the frontend pages (searches, list, …).

This are my product attributes:
*Grouped Products:

  • Status: Enabled
  • Visibility: Catalog, Search
  • Manage Stock: NO
  • They are in the correct categories
  • They are in the Main Website

*Simple Products:

  • Status: Enabled
  • Visibility: Not visible individually
  • Manage Stock: NO
  • Minimum Qty Allowed in Shopping Cart: 1
  • Maximum Qty Allowed in Shopping Cart: 10000
  • The price is set
  • They are in the Main Website

By manually disabling and then reenabling a product in the backend, the toggled product shows up. I created a script to disable and reenable all products and it seems to work locally (OSX) but not on my server (openSuse 11.4).

Is there something I can check in my import script to make it work without toggling all the product status?

EDIT: In the import script I am setting the store id and website like this:

Mage::app()->setCurrentStore($storeId);
$product = Mage::getModel('catalog/product')->setStoreId($storeId);
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));

EDIT 2: This is the script that seems to fix the problem (by setting the again the website Ids it work also on the server)

$products = Mage::getModel('catalog/product')->getCollection();

    foreach($products as $product){
        $product->load();
        echo "Disabling product: ".$product->getSku()."\n";
        $product->setStatus(2);

        $product->save();

        $product2 = Mage::getModel('catalog/product')->load($product->getId());
        echo "Enabling product: ".$product->getSku()."\n";
        $product2->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
        $product2->setStatus(1);

        $product2->save();
    }

Best Answer

OP noted that setting the website IDs seems to resolve the issue:

$products = Mage::getModel('catalog/product')->getCollection();

foreach($products as $product){
    $product->load();
    echo "Disabling product: ".$product->getSku()."\n";
    $product->setStatus(2);

    $product->save();

    $product2 = Mage::getModel('catalog/product')->load($product->getId());
    echo "Enabling product: ".$product->getSku()."\n";
    $product2->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
    $product2->setStatus(1);

    $product2->save();
}