how to update order table after checkout - prestashop

i'm developing carrier module for my project.i have to update a new column after checkout.
inside my module ,i want to call some hook immediately after order placed to get order id .
i have used below hooks.but no luck.
public function hookActionOrderStatusUpdate($params){
$order_id=$params['newOrderStatus']->id;
}
public function hookActionOrderStatusPostUpdate($params){
$order_id=$params['newOrderStatus']->id;
}
public function hookDisplayOrderConfirmation($params){
$order_id=$params['newOrderStatus']->id;
}
how to get order id into my module,when user on order confirmation page?

Did you register one of this hooks before ?
It must work as a charm inside module class with :
$this->registerHook('displayOrderConfirmation');
to get the order id it's simple :
public function hookDisplayOrderConfirmation($params){
$order_id = $params['order']->id;
}

Related

Prestashop 1.6 get order reference in prestashop custom 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);

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
}

Yii Rest api url manager

I am not able to find the logic behind the url manager. Can any body tell me about the given url calling in Rest pattern. I have too many get methods in my Controller with single parameter.
This is my Original url : localhost/project/api/event/getevent/event_id/1
api is Module
event is Controller
getevent is Controller action name
event_id is parameter 1
I want to convert this to Rest pattern localhst/project/api/event/1
//Updated code..
all are related to same Model..............
//Search by Event id
public function actionByEventId(){
$model->byEventId();
}
//Retrieve user's events by User id
public function actionByUserId()
{
$model->userEvents();
}
//Search for event by code
public function actionByEventcode()
{
$model->byEventCode();
}
Add this rule:
'api/<controller:\w+>/<action:\w+>/<id:\d+>' => 'api/<controller>/by<action>'
And add the $id your actions:
public function actionByEventId($id) {
}
public function actionByUserId($id) {
}
Now if you call localhst/project/api/event/eventid/1 Yii will call the actionByEventId in your event-Controller with $id as 1

how to call the function and display the result from tpl file in prestashop

I need to display product favorite count and some other functionalites in product detail page (product.tpl page).
But i am new to prestashop.so i am unable to find the where i declared function and i dont how to call the function form tpl file.
Here i write product favorite count code
public function favcount($id_product)
{
$sql = 'SELECT count(*) FROM `ps_favorite_product` WHERE `id_product`='.(int)$id_product.;
$result = Db::getInstance()->getRow($sql);
return $result['count'];
}
in which place i can insert the above code and how to call from product.tpl file
any one help ?
The best way is to do it in the ProductController. First you need to override it. For this create (or use the existant) /override/controllers/front/ProductController.php. You can use your function in the initContent() method:
/**
* #see ProductController::initContent()
*/
public function initContent() {
parent::initContent();
// you can place your favcount method inside the class and use it
$favCount = $this->favcount($this->product->id);
// then assign the variable to the template
$this->context->smarty->assign('favcount', $favCount);
}
Then in your template you can call the variable with: {$favcount}.