Magento event paid status - php

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

Related

Get websiteId from observer event

Given that I have multiple websites in my Magento instance, how do I identify the website where a particular event happened? For example, observing the checkout_cart_add_product_complete event lets me catch all Add to Cart events. Let's say I wanted to get the website Id of the website where this Add to Cart event happened, how do you I do that?
public function addToCart(Varien_Event_Observer $observer) {
$product = $observer->getEvent()->getProduct();
$websiteId = $observer->getEvent()->get ??? ();
}
I know that I can get the websiteIds of the product that was added to the cart, by doing the following
$websiteIds = $observer->getEvent()->getProduct()->getWebsiteIds();
But that is not what I want, because if the product belongs to more than one website, it will give me all the websites and not the one where the Add to Cart event happened.
Thanks
Have you tried:
Mage::app()->getStore()->getId()
within your observer? That should give you the current store id..
You can directly get the StoreId from Mage, not from the observer object.
StoreId:
Mage::app()->getStore()->getStoreId();
WebsiteId:
Mage::app()->getStore()->getWebsiteId();

How to call function after payment in prestashop?

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) { }

Get customer details in backend admin panel order

What I have done:
I have few custom calculations to be done after placing an order for the customer in magento admin panel. I have hooked on to sales_order_save_after event inside tab inside my module's config.xml .
The Problem:
I need to get the customer_id of the actual customer for whom the order is been placed on the backend. How can this be done?
$_customer = Mage::getSingleton('customer/session')->getCustomer();
$customer_id=$_customer->getId();
The above will give me the customer_id in case of front end, I need a way to get the customer's id when ordering from backend.
In case, the event "adminhtml_sales_order_create_process_data", is what I need to hook on, do let me know. because I am also kind of confused about which event to hook on.
Help me out.
Please use this event sales_order_place_after.
And in your code, you might get the customer id like this:
public function hookSalesOrderPlaceAfter(Varien_Event_Observer $observer) {
$order = $observer->getEvent()->getOrder();
$customer_email = $order->getCustomerEmail();
$website_id = Mage::app()->getStore()->getWebsiteId();
$customer = Mage::getModel('customer/customer')->setWebsiteId($website_id)->loadByEmail($customer_email);
...
}
Hope it will help you.
This code below works for me (Thanks to Makwana Ketan shared this code)
$sessionquoteId = Mage::getSingleton('adminhtml/session_quote')->getQuote()->getId();
$sessionCustomerId = Mage::getModel('sales/quote')->loadByIdWithoutStore($sessionquoteId)->getCustomerId();

Magento - Difference between Quote and Order

I have a doubt about how quotes and orders are being called in payment method. What I know is that a Quote is a set of products or services offered. In magento Quote data is created just before clicking Place Order button of Onepage Checkout. After the Order is placed Order data is created in Magento. Invoice comes next to Order if Order is confirmed.
But I was wondering why the Class Mage_Payment_Model_Method_Abstract in validate Method checks Info class Instance if it is an instance of Mage_Sales_Model_Order_Payment take getOrder() else take getQuote()
I am not clear with this. Does the Validate() function is called two time i.e first time when Quote is created and second time when Order is Created OR does the Payment Method Class itself is called two times.
Please clarify my confusion.
/**
* Validate payment method information object
*
* #param Varien_Object $info
* #return Mage_Payment_Model_Abstract
*/
public function validate()
{
/**
* to validate paymene method is allowed for billing country or not
*/
$paymentInfo = $this->getInfoInstance();
if ($paymentInfo instanceof Mage_Sales_Model_Order_Payment) {
$billingCountry = $paymentInfo->getOrder()->getBillingAddress()->getCountryId();
} else {
$billingCountry = $paymentInfo->getQuote()->getBillingAddress()->getCountryId();
}
if (!$this->canUseForCountry($billingCountry)) {
Mage::throwException($this->_getHelper()->__('Selected payment type is not allowed for billing country.'));
}
return $this;
}
A quote in Magento is basically an order that hasn't been placed yet. It contains product items (shopping cart), addresses and payment/shipping methods. It is created as soon as you add an item to cart. During checkout, billing and shipping data is added to the quote. Finally, when the user clicks place order, the quote is converted to an order.
To answer your question about the payment validation: The payment method is included in the quote as well as the order and validated in both places. A payment method may be restricted to certain countries, so in the validate method, a payment method for a quote will validate the quote country, and a payment method for an order will validate the order country.

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