Jump to content

how to get query result in variable using smarty in tpl file prestashop 1.6


Recommended Posts

hello 

 I want to work smarty in (template) .tpl file like in jquery code we can assign smarty variable to jquery or javascript variable like 

 

var temp = '{Configuration::get("Test")}'; 

same way I want database query result as follows i.e

var temp = '{Db::getInstance()->getRow('Select * from '._DB_PREFIX_.'test ')}";

alert ($temp);

 

its work fine with first one get value of test into temp but with second one when im print it in alert it will print alert only  "Array", so how can i get result of Db class

Link to comment
Share on other sites

You should not use classes in templates that way, my advice is to create a php file and use it to fetch a tpl file where you will have your javascript :) Also, you cannot alert an array, try with console.log()

Link to comment
Share on other sites

You should not use classes in templates that way, my advice is to create a php file and use it to fetch a tpl file where you will have your javascript :) Also, you cannot alert an array, try with console.log()

but im not able to override "AdminOrdersController.php" file with "renderForm()" that create template named "form.tpl" , if i do so then it will show twice search on order tab

Link to comment
Share on other sites

when you override that, copy the whole method, and instead of using parent::renderForm() use AdminController::renderForm()

great man now it shows only single search but when im pass smarty variable it will give error "Undefined index: Test" & "Trying to get property of non-object"

 

i have override AdminOrdersConrtoller.php as follows and place it in {root}/override/controllers/admin folder 

 

I have written following line of code 

<?php
class AdminOrdersController extends AdminOrdersControllerCore
{
public function renderForm()
{
if (Context::getContext()->shop->getContext() != Shop::CONTEXT_SHOP && Shop::isFeatureActive())
$this->errors[] = $this->l('You have to select a shop before creating new orders.');
 
$id_cart = (int)Tools::getValue('id_cart');
$cart = new Cart((int)$id_cart);
if ($id_cart && !Validate::isLoadedObject($cart))
$this->errors[] = $this->l('This cart does not exists');
if ($id_cart && Validate::isLoadedObject($cart) && !$cart->id_customer)
$this->errors[] = $this->l('The cart must have a customer');
if (count($this->errors))
return false;
 
AdminController::renderForm();
unset($this->toolbar_btn['save']);
$this->addJqueryPlugin(array('autocomplete', 'fancybox', 'typewatch'));
 
$defaults_order_state = array('cheque' => (int)Configuration::get('PS_OS_CHEQUE'),
'bankwire' => (int)Configuration::get('PS_OS_BANKWIRE'),
'cashondelivery' => (int)Configuration::get('PS_OS_PREPARATION'),
'other' => (int)Configuration::get('PS_OS_PAYMENT'));
$payment_modules = array();
foreach (PaymentModule::getInstalledPaymentModules() as $p_module)
$payment_modules[] = Module::getInstanceById((int)$p_module['id_module']);
 
$this->context->smarty->assign(array(
'recyclable_pack' => (int)Configuration::get('PS_RECYCLABLE_PACK'),
'gift_wrapping' => (int)Configuration::get('PS_GIFT_WRAPPING'),
'cart' => $cart,
'currencies' => Currency::getCurrenciesByIdShop(Context::getContext()->shop->id),
'langs' => Language::getLanguages(true, Context::getContext()->shop->id),
'payment_modules' => $payment_modules,
'order_states' => OrderState::getOrderStates((int)Context::getContext()->language->id),
'defaults_order_state' => $defaults_order_state,
'show_toolbar' => $this->show_toolbar,
'toolbar_btn' => $this->toolbar_btn,
'toolbar_scroll' => $this->toolbar_scroll,
'title' => array($this->l('Orders'), $this->l('Create order'))
));
$this->context->smarty->assign(array(
'Test' => Configuration::get('MYMODULE_STATUS'),
));
$this->content .= $this->createTemplate('form.tpl')->fetch();
}
}
?>

 

Link to comment
Share on other sites

×
×
  • Create New...