Configure Prestashop faceted search programmatically

2.4k views Asked by At

I have to make a Prestashop website for a client so I have to import all their products with a script every day. Everything is working fine (categories, products, photos, features) except for the attributes, which I need to enable the ps_facetedsearch module. I can insert the attributes values in my database but I cannot find how to assign them to the products (that's problem #1). And then I need in my script to configure the aforesaid module so that these attributes can show up as filters in the search block. Precisely, I have to tell which categories are used in my filter model, and I cannot do it manually every day.

Here is the code I use to insert the data as attributes ($data is the object with all the data to import):

$attributesGroupArray=array('1' => 'Attribute1','2' => 'Attribute2','3' => 'Attribute3','4' => 'Attribute4');
foreach($attributesGroupArray as $id_attribute_group => $name){
    $attribute=new Attribute();
    $attribute->id_attribute_group=$id_attribute_group;
    $attribute->name=array((int)Configuration::get('PS_LANG_DEFAULT') => $data->$name);
    $attribute->url_name=array((int)Configuration::get('PS_LANG_DEFAULT') => url_rewrite($data->$name));
    $attribute->add();
    $attributeid=$attribute->id;
}

Can you point me in the right direction about these two problems?

Edit: I accepted the answer below for the first part of my problem. As for the second part, I finally managed to find the right code so I'll put it here in case it can help someone someday.

$cats=Db::getInstance()->executeS('SELECT id_category FROM `'._DB_PREFIX_.'category` WHERE `id_category`>\'1\'');
foreach($cats as $cle => $resultat){
    $comptattribut=0;
    $atts=DB::getInstance()->executeS('SELECT id_attribute_group FROM `'._DB_PREFIX_.'attribute_group` ORDER BY position ASC');
    foreach($atts as $cleatt => $resultatatt){
        $comptattribut++;
        DB::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'layered_category` SET `id_shop`=\'1\', `id_category`=\''.$resultat['id_category'].'\', `id_value`=\''.$resultatatt['id_attribute_group'].'\', `type`=\'id_attribute_group\', `position`=\''.$comptattribut.'\', `filter_type`=\'0\', `filter_show_limit`=\'0\'');
    }
DB::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'layered_category` SET `id_shop`=\'1\', `id_category`=\''.$resultat['id_category'].'\', `id_value`=NULL, `type`=\'price\', `position`=\''.($comptattribut+1).'\', `filter_type`=\'1\', `filter_show_limit`=\'0\'');
}
1

There are 1 answers

4
PrestaAlba On BEST ANSWER

For the first part of the problem you should use something like this:

$id_combination = $product->addCombinationEntity($wholesale_price, $price, $weight, $unit_price_impact, 0, $quantity, $ids_image, $reference, 0, $ean13, 0, 0, $upc);
foreach ($product_option_value as $id_attribute)
{
     Db::getInstance()->execute('INSERT IGNORE INTO ' . _DB_PREFIX_ . 'product_attribute_combination (id_attribute, id_product_attribute) VALUES (' . (int) $id_attribute. ',' . (int) $id_combination . ')');
}

Good luck.