How to call function after payment in prestashop? - php

I am building a custom module in prestashop and I need to execute something after payment accepted and after the emails have been sent. In mymodule.php I have the following hooks:
public function hookActionValidateOrder($params) {
$order = $params['order'];
$customer = $params['customer'];
$valuesToinsert="";
$attrValue=array();
etc...
}
Which is executed normally. I tried actionOrderStatusPostUpdate, actionPaymentConfirmation but none of these seems to be called. I dont know whether it is relevant but I am using opc module and the product is free of charge.

This hook is call when an order is placed after a client confirm his cart. The function that triggers this hook is validateOrder from PaymentModule class. It is call by payment modules when client click en confirm button in checkout. Every payment module should call this function in some moment. But, if you don't have a payment module in your specific process due to free product this hook could maybe be never called.
Anyway, you can subscribe to actionObjectOrderAddAfter hook or similar to get notified when a new order is placed:
public function hookActionObjectOrderAddAfter($params)
{
//$params['object'] contains specific object, in this case your Order object
}
If you need information about order status you could subscribe to hook actionOrderHistoryAddAfter too. Hook actionOrderStatusUpdate is only trigger inside changeIdOrderState function. If for some reason order status change with no call to this function you will miss notification.
Good luck

As you have mentioned in your question that the order you are trying is free, in this case any hook that is called on payment, will never call.
Hence the hooks (i.e. actionOrderStatusPostUpdate, actionPaymentConfirmation, hookActionObjectOrderAddAfter etc.) will never be called as they are called from the PaymentModule.php class and it is not called at all in case of a free order.
Unfortunately, there are no hooks that are called when a free order is placed. In case you want to take any action on a Free order then you can only do that by overriding the FreeOrder class or _checkFreeOrder() function in ParentOrderController.php

Old post, but wanted to drop a comment to help others. This function calls after an order is submitted and they get the confirmation page, whether a payment was submitted with it or not:
public function hookDisplayOrderConfirmation($params) { }

Related

What is the cleanest way to override a Wordpress plugins' hook instantiated with $this

I am trying to modify a Wordpress plugins' method without changing the plugin core code itself. I need to set some status flag in a different part of the website.
The method is defined in the plugin class like so:
add_action('plugins_action_name', [$this, 'local_method_name']);
The plugin method does not offer any actions or filters to hook into.
If this wasn't Wordpress but plain PHP I would write my own class, which extends the plugins' original class but has it's own method by the same name - but in Wordpress this gets me nowhere since I just move the problem from one plugin file to the next.
Is there a way to remove the plugins action altogether? And then redefine my own version of it?
Since the add_action has been called with $this - how would I remove it in some functions.php file?
Thanks
UPDATE
Indeed I want to learn how to handle different scenarios. Not expecting a one size fits all solution, but I do need to know how to avoid core changes, as each core-change complicates my deploy/update process exponentially.
Sure I will add some more details. My example plugin is WooCommerce Germanized Pro which comes with a WC_GZDP_Download_Handler::download() method which I needed to modify, in order to set some sort of "Warehouse downloaded the invoice" flag for each order. Which is later used to highlight new orders, filter orders that have been handed over to fulfilment etc.
There seems to be a Invoice Helper class which does in fact have a singleton pattern.
my modification in the download class is trivial:
public static function download( $pdf, $force = false ) {
... /* original code above, now my modifications */
if ($check_some_codition) {
$invoice = $pdf->get_invoice();
update_post_meta($invoice->get_id(), 'some-flag', 'some-value');
}
/* then resume original function (return the file to the browser) */
self::out( $filename, $file, $force );
}

Payment module return URI

I am creating a payment module for our payment gateway...
So far I have 1) set up the back-end and 2) used the hookPayment() method to display a hidden form at part 5 of the checkout process (http://prestashop.dev/order). This will then redirect off to my gateway with all the required information. Good job.
The next part is the part I am struggling with. I don't understand what the return URIs are for payment-confirmation - just to give the customer some information on the status of the order (and maybe also update the back-office?).
For now, I have just a very simple method;
public function hookPaymentReturn()
{
// if (!$this->active) {
// return null;
// }
return $this->display(__FILE__, 'views/templates/front/confirmation.tpl');
}
in my main module file. I just want to get to this on the browser... I'll start worrying about POST'ed values after this. But for now I just don't know the URI. What will it be?? Do I need to register the route somehow?
Most payment modules confirmation.tpl get hooked into the order-confirmation.tpl of your theme which in turn gets called by the OrderConfirmationController.php.
This can be accessed by:
[YOUR_BASE_URL]/index.php?controller=order-confirmation&[some_stuff].
The [some_stuff] part is important though since the controller performs a series of validations and will redirect you somewhere else if these are not met

Magento event paid status

Does anyone know how to implement a custom "module" that is triggered when an order is paid or complete?
And how am I able to call the order data from that observer?
I am also working with the "Serial Codes" plugin, and I want to send
an email to the person who bought this product, containing the serial
code.
Is there anybody who is able to help me out?
You can write an observer for the sales_order_save_before event. In the observer method you are able to get the order by $observer->getEvent()->getOrder(). Then you can check for the order status/state and add your code when the order is completed. This is the safest way, with the small downside, that the Observer function will always be triggered when the order is saved. Example Code:
public function onCompleteOrder(Varien_Event_Observer $observer)
{
/** #var $order Mage_Sales_Model_Order */
$order = $observer->getEvent()->getOrder();
if ($order->getState() == Mage_Sales_Model_Order::STATE_COMPLETE) {
// do something
}
return $this;
}
By the way: A Magento order is usually becoming completed when
An invoice has been created AND
a shipment has been created

Call new php function when order is complete in Magento

I have a magento site for ecommerce. When an order is placed, I need to call another function I've created in a new php file and pass the order skus, quantities and shipping address to. I'm extremely comfortable with php, but Magento is an entirely new beast for me.
Does anyone know how to call a function when an order is placed? Even just the name of the event would be helpful.
I haven't used it personally, but sales_order_place_after sounds like it might be what you're looking for. It's used in this way in this Inchoo article, which also involves doing some things as soon as an order is placed.
Here's a page on the Magento wiki about setting up an event observer, which really is just a little XML to tell Magento to run some code when that event is dispatched, and the code you want to run.
you can try sales_order_place_before and sales_order_place_after
if you are interested in the events fired, a common approach is to temporary add
Mage::log($name); in the Mage.php (app/Mage.php) like this
public static function dispatchEvent($name, array $data = array())
{
Mage::log($name);
Varien_Profiler::start('DISPATCH EVENT:'.$name);
$result = self::app()->dispatchEvent($name, $data);
Varien_Profiler::stop('DISPATCH EVENT:'.$name);
return $result;
}
this will log any event fired during a page view or action to the var/log/system.log, if you enabled logging in the backend System->Configuration>Developer->Log Settings

Magento event for Paypal IPN marking order as Processing

I'm working on an extension for Magento (1.4 CE) that needs to be triggered once an order has been paid for. I'm having trouble finding an event to hook in to that will trigger when Paypal IPN (Paypal Standard) has done its thing.
I've tried using the sales_order_invoice_save_after and sales_order_invoice_register events, but neither of these seem to be triggered by a Paypal IPN response.
I'm now trying to use the sales_order_save_after event to detect when the order enters the "processing" status, like so:
class Lightbulb_Blastramp_Model_Observer {
public function sendOrderToBlastramp(Varien_Event_Observer $observer) {
Mage::log('Start' . "\n\n", null, 'blastramp.log');
$order = $observer->getEvent()->getOrder(); // get order data
// make sure the order is in the processing state
if ($order->getState() != Mage_Sales_Model_Order::STATE_PROCESSING) {
Mage::log('Not processing, return.' . "\n\n", null, 'blastramp.log');
return $this;
}
// order has reached "processing" state, do stuff...
}
}
From the log files I can see that my code is triggered when the order is initially created with the "payment pending" state, but does not get triggered when it moves to the "processing" state. Is there some event I can hook in to that will trigger when an order hits the "processing" stage as set by Paypal IPN?
Cheers
After struggling with this for a while, I eventually had success with this by overriding the _isCaptureFinal($amount) method in Mage_Sales_Model_Order_Payment.
eg.
class Lightbulb_Blastramp_Model_Order_Payment extends Mage_Sales_Model_Order_Payment {
public function _isCaptureFinal($amount) {
// do things here
return parent::_isCaptureFinal($amount);
}
}
This is thanks to the answer to another question: https://stackoverflow.com/a/5024475/602734
Hopefully this helps someone!
In case anyone stumbled upon this question like i did, an observer that does this as was originally requested;
checkout_onepage_controller_success_action
This returns just the order id, so;
$order_id = $observer->getData('order_ids');
$order = Mage::getModel('sales/order')->load($order_id);
and you see that the order status is 'processing' and the payment is aproved (or not).

Categories