We would like to add a column displaying values of a specific feature in the products grid.
We will then be able to:
- rapidly export the products grid with this field
- edit quickly the feature on several products
To add the field to the list of available fields for your products grids, click on the in the 'Add a field' panel and enter the following information:
What is the field ID?: myfeature
SC creates the field, you now need to populate the grid with:
Field name: Delivery
Table: Another table
Type: multiple choice
Refresh combinations: No
From the Advanced Properties panel on the right handside:
- select the menu SQLSelect and enter:
return ' , (SELECT fp.id_feature_value FROM `' . _DB_PREFIX_ . 'feature_product` fp
WHERE fp.id_feature = "3"
AND fp.id_product = p.id_product) as myfeature ';
- select the menu Select options and enter:
$sql = "SELECT fvl.*
FROM " . _DB_PREFIX_ . "feature_value_lang fvl
INNER JOIN " . _DB_PREFIX_ . "feature_value fv ON (fv.id_feature_value=fvl.id_feature_value)
WHERE fvl.id_lang=1
AND fv.id_feature = 3";
$res = Db::getInstance()->ExecuteS($sql);
$tmp = array(0=>" ");
foreach ($res as $row) {
$tmp[$row["id_feature_value"]] = $row["value"];
}
return $tmp;
- select the menu PHP onAfterUpdateSQL and enter:
if ($feature_value = (int)Tools::getValue('myfeature', 0)) {
$sql = "SELECT * FROM " . _DB_PREFIX_ . "feature_product WHERE id_product='" . (int)$idproduct . "' AND id_feature='3'";
$res = Db::getInstance()->ExecuteS($sql);
if (count($res)) {
if ($feature_value) {
$sql = "UPDATE " . _DB_PREFIX_ . "feature_product SET id_feature_value='" . (int)$feature_value . "' WHERE id_product='" . (int)$idproduct . "' AND id_feature='3'";
Db::getInstance()->Execute($sql);
} else {
$sql = "DELETE FROM " . _DB_PREFIX_ . "feature_product WHERE id_product='" . (int)$idproduct . "' AND id_feature='3'";
Db::getInstance()->Execute($sql);
}
} else {
$sql = "INSERT INTO " . _DB_PREFIX_ . "feature_product (id_feature,id_product,id_feature_value) VALUES ('3','" . (int)$idproduct . "','" . (int)$feature_value . "')";
Db::getInstance()->Execute($sql);
}
}
In this instance, we use feature ID3. You can change the feature by looking for its ID in the Features panel (SC Catalog>Features)
Exit the editing window.
The new field is now present in the list of available fields and you can add it to your product grids.