Prestashop 1.6 get order reference in prestashop custom module - module

guys i want to make variable with order reference in my module with DisplayAdminOrder hook. And i try with this method , but nothing is happen are you have idea how to get variable with order reference
public function hookDisplayAdminOrder()
{
$orderid - Tools::getValue('id_order');
$order = new Order($orderid);
$ref = $order->reference;
echo $ref;
//$this->processChangePayment();
//return $this->display(__FILE__, 'display.tpl');
}

If the module placed in its hook, it should work. just use d() instead of echo to make sure it work correctly:
d($ref);

Related

Ho to call variables items on Joomla Component

I'm trying to create my first component in Joomla but I can't understand how it works really :D
I create a basic component with "Component Creator". (I saved a lot of time...).
Now, I have a table in my DB where I've put my data:
id = 1
name = Andrea
note = Ciao
Now, I want to call it from the page using Joomla Language.
On my models/component.php i wrote:
class Variabili extends JModelLegacy
{
function estraivariabili()
{
$db =& JFactory::getDBO();
$db->setQuery("SELECT * FROM #__table")->loadObjectList();
return $value;
}
}
and on my default.php I wrote
$model=$this->Variabili();
//call the method
$items=$model->estraivariabili();
//print
print_r($items);
But on the page I have this error:
0 Call to undefined method Calcolo_imposteViewCalcoloonline::Variabili()
Where is a mistake?
Please be gentle with me because I'm a beginner: D
Thanks in advance
Andrea
You have committed a few wrongs. I've rewritten your functions so that you can get it. Let's have a look-
The model, it looks almost okay. Just change it like-
class Variabili extends JModelLegacy
{
// Make the function public
public function estraivariabili()
{
$db = &JFactory::getDBO();
// Put the result into a variable first, then return it.
$value = $db->setQuery("SELECT * FROM #__table")->loadObjectList();
return $value;
}
}
And now call the model functions not from default.php, rather than write your code inside view.html.php file.
Inside the display function of view.html.php file first, get the model instance by using getModel() function.
$model = $this->getModel();
Now you can get the items by using this $model class instance.
$this->items = $model->estraivariabili();
This will bring you the data from the database table. And you can use the data at default.php file.
Just try at default.php file-
print_r($this->items);

Apply products list template in a custom module prestashop 1.7

I create a custom module in prestashop 1.7, and I would like to apply the layout of products list on my module and display products what I want. What I have to do ?
My initContent function is
class TestModuleDisplayModuleFrontController extends ModuleFrontController{
public function initContent()
{
parent::initContent();
$this->setTemplate('products.tpl');
}
}
Thanks you
Hi Varag and welcome to Stackoverflow.
Your answer is here waiting for you : https://devdocs.prestashop.com/1.7/modules/concepts/controllers/front-controllers/
For instance your function must look like that :
public function initContent()
{
// In the template, we need the vars paymentId & paymentStatus to be defined
$this->context->smarty->assign(
array(
'paymentId' => Tools::getValue('id'), // Retrieved from GET vars
));
// Will use the file modules/cheque/views/templates/front/validation.tpl
$this->setTemplate('module:cheque/views/templates/front/validation.tpl');
}
I think that you have to use array instead of object:
$listing['products'] = json_decode(json_encode($your_products), true); // convert object to array
$this->context->smarty->assign("listing", $listing);
$this->setTemplate('module:mymodule/views/templates/front/mytemplate.tpl');

Prestashop beforeRequest Middleware

I am trying to build a module for Prestashop 1.6 that would redirect the user if the targeted URL is present in a database.
What I'm going to do is the following:
public function checkRedirection ($url) {
$line = Db::getInstance()->executeS('SELECT * FROM ps_custom_redirection WHERE url = ' . pSQL($url));
if (!sizeof($line)) {
return null;
}
header('Location: ' . $line[0]['destination']);
http_response_code($line[0]['http_code']);
exit();
}
Now, I could run this function when the displayTop hook is fired. But I would rather launch this function at the beginning of the request's process.
Does Prestashop provide such a hook? If not, can I create one? Where should I write the code to fire it?
The fist hook executed is actionDispatcher – you can use it if you want.
You'll find this hook executed in /classes/Dispatcher.php. Search for the code Hook::exec('actionDispatcher', $params_hook_action_dispatcher);.
If you want to add this hook to your module, you need to use its name in the main module file like this:
public function install() {
return parent::install()
&& $this->registerHook('actionDispatcher');
}
public function hookActionDispatcher($params) {
// your code
Tools::redirect($url);
}
In Prestashop Tools::redirect($url); is used if redirecting.

Pretashop get new order details in a custom module

I am creating a custom module in Prestashop. In that module I want to get
the order details when a new order has been made on the store. So I want
to know which hook should I use and how to get new order details along with
customer details when a new order has been made. Any help and suggestions will be really appreciable.
Use a hook actionValidateOrder. Hook gives you params array which includes Order, Cart, Customer, Currency and OrderState objects associated with it.
With them you can now get customer and order details.
public function hookActionValidateOrder($params) {
$order = $params['order'];
$customer = $params['customer'];
$order_details = $order->getOrderDetailList();
}
Hook needs to be registered in module install.
public function install() {
return parent::install && $this->registerHook('actionValidateOrder');
}
You can use displayOrderConfirmation hook in your main controller to get details of any order.
Following code can help you:
public function hookDisplayOrderConfirmation($params = null)
{
$id_customer = $params['objOrder']->id_customer;
//Get all other details using the $params['objOrder'] order object
}

addJS function not working for admin in prestashop

I am trying to add javascript file in prestashop admin using backOfficeHeader hook using a module but nothing happened. My code is given below.
public function install()
{
if (!parent::install()
|| !$this->registerHook('backOfficeHeader'))
return false;
return parent::install() &&
$this->registerHook('backOfficeHeader');
}
public function hookBackOfficeHeader() {
$this->context->controller->addJS(_MODULE_DIR_.$this->module->name.'js/hs_custom.js');
}
If you are using PS 1.5 or 1.6 you should use hook "actionAdminControllerSetMedia".
Your module installer should check which prestashop version is used and then register the needed hook.
if (version_compare(substr(_PS_VERSION_, 0, 3), '1.5', '<'))
$this->registerHook('BackOfficeHeader');
else
$this->registerHook('actionAdminControllerSetMedia');
Then you need to addJS on each hook in its version format:
PS>=1.5
public function hookActionAdminControllerSetMedia($params) {
$this->context->controller->addJS($this->_path.'views/js/hs_custom.js');
}
PS<=1.4
public function hookBackOfficeHeader($params) {
Tools::addJS($this->_path.'views/js/hs_custom.js');
}
did u try to check addJS path? I think nothing more can be possible if other JS files working.
Try to use $this->_path.
$this->context->controller->addJS($this->_path.'views/js/hs_custom.js');
1) Output path and check if it is valid.
2) Reload page and check network. Page load your script or not?
3) Remember to reset module if u change something with hooks.
4) Check module hooks.
You did several mistakes.
This is the invalid access to the property: $this->module->name. Must be $this->name. I.e., the correct code to generate a path to JavaScript file is:
_MODULE_DIR_ . $this->name . '/js/hs_custom.js'
Or like this (shorted):
$this->_path . 'js/hs_custom.js'
You are also did the double installation of the module and of the hook.
You can use the hook BackOfficeHeader, but the hook ActionAdminControllerSetMedia is preferred.
So, the correct example to add a JS and a CSS files for a back-office (i.e. for AdminController) via a module class is:
public function hookActionAdminControllerSetMedia($params)
{
// Adds your's CSS file from a module's directory
$this->context->controller->addCSS($this->_path . 'views/css/example.css');
// Adds your's JavaScript file from a module's directory
$this->context->controller->addJS($this->_path . 'views/js/example.js');
}
Here is the detailed information, how to register JavaScript in a back-office (in admin pages).
I also met this problem, there is no error and warning, all grammar is right. But cannot find my js File.
I found the reason finally. In my case there is nothing in JS file and system passes this file which has no content always.
For me "this->_path" dosn't work. My solution is to use $_SERVER['DOCUMENT_ROOT']
public function hookActionAdminControllerSetMedia($params)
{
// add necessary javascript to products back office
if($this->context->controller->controller_name == 'AdminProducts' && Tools::getValue('id_product'))
{
$this->context->controller->addJS($_SERVER['DOCUMENT_ROOT']."/modules/apl/views/js/jquery.ui.touch-punch.min.js");
}
}