简体   繁体   中英

Magento: Products added programmatically are not showing up in search

I wrote a PHP script which adds a bunch of products from a .csv file into my Magento database. Everything works fine, the product is visible on the front page (under newest articles), but it is not searchable. When I go into the Magento backend, click on the article, change nothing but hit save , then the product comes up when I search for it. What am I doing wrong? I save the correct Website-ID, I give the items also visibility=4 (catalog and search), but it doesn't work as expected.

The code looks like that:

set_time_limit(0);
ini_set('memory_limit', '1024M');
include_once "../app/Mage.php";
include_once "../downloader/Maged/Controller.php";

Mage::init();

$app = Mage::app('default');

$row = 0;
if (($handle = fopen("data.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        echo 'Importing product: '.$data[0].'<br />';
        foreach($data as $d)
        {
            echo $d.'<br />';
        }
        $num = count($data);
        $row++;

        if($row != 1){
        $product = Mage::getModel('catalog/product');

        $productDescription = $data[2]."\n".$data[3]."\nHerstellerteilenummer: ".$data[1];
        $product->setSku($data[1]);
        $product->setName($data[1]." ".$data[3]);
        $product->setDescription($productDescription);
        $product->setShortDescription($productDescription);
        $product->setManufacturer($data[2]);
        $product->setPrice($data[4]);
        $product->setTypeId('simple');

        $fullpathThumb = Mage::getBaseDir('media').'/catalog/product/placeholder/thumb/'.$data[2].'.png';
        $fullpathSmall = Mage::getBaseDir('media').'/catalog/product/placeholder/small/'.$data[2].'.png';
        $fullpathHigh = Mage::getBaseDir('media').'/catalog/product/placeholder/high/'.$data[2].'.png';

        $product->addImageToMediaGallery($fullpathThumb, 'thumbnail', false);
        $product->addImageToMediaGallery($fullpathSmall, 'small-image', false);
        $product->addImageToMediaGallery($fullpathHigh, 'image', false);

        $product->setAttributeSetId(4); // need to look this up
        $product->setCategoryIds(array(4)); // need to look these up
        $product->setWeight(0);
        $product->setTaxClassId(1); // taxable goods
        $product->setVisibility(4); // catalog, search
        $product->setStatus(1); // enabled

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

        try{
            $product->save();
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        }
    }
    fclose($handle);
}

And the data.csv looks like that:

ID,TeileNr,Lieferant,VK,Bez,ModDatum
666123,01440AA090,CONTI,LICHTGENERATOR,2362.50000,"Sep 11 2014 04:20:52:747PM"
666124,01440AA0B0,CONTI,LICHTGENERATOR,1825.00000,"Sep 11 2014 04:20:52:757PM"
...

Ok, there is nothing wrong with the code. I just had to re-index everything after I add the products programmatically (under System -> Index Management -> Select All -> Reindex Data). I tried to re-index just the 'search index' but that doesn't work, you have to re-index everything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM