To create a support ticket, start a search

Code index


Select options

To use this tool, the cell type of your field must be 'multiple choice'.
This section allows you to set the select box options when the user click to edit the cell.

To set the options, you need to return a PHP array:

return array(1=>'Yes' , 0=>'No');


You can use a more complex code to get the options from a table:

$sql = 'SELECT id_logistician,name_logistician FROM ps_logistician ORDER BY name_logistician';
$res = Db::getInstance()->ExecuteS($sql);
$tmparray = array(0 => 'NA');
foreach ($res AS $row) {
    $tmparray[$row['id_logistician']] = $row['name_logistician'];
}
return $tmparray;

 


SQL Select

Here you need to return the name of the field that will be displayed in your grids.
This will be used in our main query to get products/orders information.

You can use these variables:

$view - name of the current grid view
$cols - array of columns of the grid
$id_lang - language used in the interface

Example:

return ' ,myTableAlias.myTableFieldName AS myFieldName';


You can use a more complex code to get the feature value of the product:

return ' , (SELECT fvl.value FROM `ps_feature_product` fp 
                LEFT JOIN `ps_feature_value_lang` fvl 
                ON (fp.id_feature_value=fvl.id_feature_value and fvl.id_lang=2)
                WHERE fp.id_feature = 3 
                AND fp.id_product = p.id_product) as myFeature ';


An other example to add the ordered products name on an orders grid (full example available on our support platform):

return ' , (SELECT GROUP_CONCAT(product_name SEPARATOR '') 
                FROM `'._DB_PREFIX_.'order_detail` od 
                WHERE od.id_order = o.id_order) as orderProductsNames ';

 


SQL Left Join

You should not use aliases corresponding to standard Prestashop tables for fields coming from external tables (do not use "p.", "a.", etc.)

You can use these variables:

$view - name of the current grid view
$cols - array of columns of the grid
$id_lang - language used in the interface

Examples:

return " LEFT JOIN "._DB_PREFIX_."myTable myTableAlias ON (myTableAlias.id_product= p.id_product) ";


Warning: if you use several fields from the same external table (myTable) you need to set this information only once for all the fields.

 

Grid JS onBeforeUpdate

Work in progress...

 

Grid JS onEditCell

This event is triggered when a cell is edited by the user in the interface.

// onEditCell(stage,rId,cInd,nValue,oValue)
// rId is the row ID
// cInd is the column INDEX
// nValue is the new value
// oValue is the old value

if (nValue != oValue) {
    idxDeliveryInfo = cat_grid.getColIndexById('DeliveryInfo');
    if (cInd == idxDeliveryInfo) {
// here we truncate the data entered by the user
        cat_grid.cells(rId, idxDeliveryInfo).setValue(cat_grid . cells(rId, idxDeliveryInfo).getValue().substr(0, 100));
    }
}

Grid JS onAfterUpdate

Work in progress...

 

PHP definition

When you add a field to the combinations grid, you need to declare it in this section.

$combArray[$combinaison['id_product_attribute']]['myFieldName'] = $combinaison['myFieldName'];

PHP onBeforeUpdateSQL

Work in progress...

 

PHP onAfterUpdateSQL

This event is triggered when the user's browser sends the data to the server.
In this section we need to store the new value in the database.

You can use these variables:

$id_product
$id_lang

Example:

$sql = "SELECT * FROM "._DB_PREFIX_."myTable WHERE id_product=".(int)$id_product;
$res = Db::getInstance()->ExecuteS($sql);
$myFieldName = Tools::getValue('myFieldName', -1);
if (count($res)) {
    if (in_array($myFieldName, array('North', 'East'))) // we check if the new value is in the expected range of values
    {
        $sql = "UPDATE "._DB_PREFIX_."myTable SET myFieldName=".psql($myFieldName)." WHERE id_product=".(int)$id_product;
        Db::getInstance()->Execute($sql);
    } else {
        $sql = "DELETE FROM "._DB_PREFIX_."myTable WHERE id_product=".(int)$id_product;
        Db::getInstance()->Execute($sql);
    }
} else {
    $sql = "INSERT INTO "._DB_PREFIX_."myTable (id_product,myFieldName) VALUES (".(int)$id_product.",'".psql($myFieldName)."')";
    Db::getInstance()->Execute($sql);
}