简体   繁体   English

Magento:以编程方式添加的产品未在搜索中显示

[英]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. 我写了一个PHP脚本,该脚本将.csv文件中的许多产品添加到我的Magento数据库中。 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. 当我进入Magento后端时,单击该文章,进行任何更改,但单击save ,然后在搜索时出现该产品。 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. 我保存了正确的网站ID,并为这些项目设置了visible = 4(目录和搜索),但是它无法正常工作。

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: data.csv看起来像这样:

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. 我试图仅对“搜索索引”进行重新索引,但这不起作用,您必须对所有内容进行重新索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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