繁体   English   中英

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

[英]Magento: Products added programmatically are not showing up in search

我写了一个PHP脚本,该脚本将.csv文件中的许多产品添加到我的Magento数据库中。 一切正常,该产品在首页上可见(在最新文章下方),但不可搜索。 当我进入Magento后端时,单击该文章,进行任何更改,但单击save ,然后在搜索时出现该产品。 我究竟做错了什么? 我保存了正确的网站ID,并为这些项目设置了visible = 4(目录和搜索),但是它无法正常工作。

代码如下所示:

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);
}

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"
...

好的,代码没有错。 以编程方式添加产品后,我只需要重新索引所有内容(在“系统”->“索引管理”->“全选”->“重新索引数据”下)。 我试图仅对“搜索索引”进行重新索引,但这不起作用,您必须对所有内容进行重新索引。

暂无
暂无

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

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