We will show all the list of our carrier in our product page. to do this
First we will create a function in root_folder/controllers/front/ProductController.php , note that you can add this script on any
controller that you desired.
find this line of code and add the function
public function initContent()
{
parent::initContent();
.......
//add custom function here
$this->setTemplate(_PS_THEME_DIR_.'product.tpl');
}
add this function inside initContent()
function sinelectnacarrier(){
}
first define a null var inside our function
$namecar = '';
Prepare our sql statement, there are many data that will show when we query our carrier because prestashop
is not deleting the records of carrier in the database when we delete it in backoffice, the solution to this is to add an where clause.
$db = Db::getInstance();
$sql = 'SELECT * FROM '._DB_PREFIX_.'carrier WHERE '._DB_PREFIX_.'carrier.deleted = 0';
We will loop our data and append it to our var $namecar
if ($results = Db::getInstance()->ExecuteS($sql))
foreach ($results as $row){
//append data to variable $namecar
$namecar.= "id_carrier : " . $row['id_carrier'] . ' - Name :' . $row['name'] . "";
}
we will return an array with the corresponding carrier id and its name.
return $namecar;
And finally we will assign the return value of our function to a tpl file so that we can use it later on our tpl file
$this->context->smarty->assign("listOfCarrier", sinelectnacarrier());
Adding it all up
function sinelectnacarrier(){
$namecar = '';
$db = Db::getInstance();
//there are many data that will show here, we will remove the deleted carrier
$sql = 'SELECT * FROM '._DB_PREFIX_.'carrier WHERE '._DB_PREFIX_.'carrier.deleted = 0';
if ($results = Db::getInstance()->ExecuteS($sql))
foreach ($results as $row){
//append data to variable $namecar
$namecar.= "id_carrier : " . $row['id_carrier'] . ' - Name :' . $row['name'];
}
//return an array with the corresponding carrier id and its name
return $namecar;
}
// assign it to the tpl var
$this->context->smarty->assign("listOfCarrier", sinelectnacarrier());
To call our assigned value in our tpl file, open root_folder/themes/default-bootstrap/product.tpl
paste this code to the part where you want the list of carrier to be shown.
{$listOfCarrier}
Im getting a blank page when modificaton on ProductController.php were made
ReplyDeleteps 1.6.14
ReplyDelete