How to get payment method in an observer in Magento?

丶灬走出姿态 提交于 2019-12-11 05:58:36

问题


I have an observer that handles the event: sales_payment_invoice_pay (or something like that).

What I'm trying to do is to send an invoice if the payment method is PayPal.

Everything is ok in version 1.4~ by doing $observer->getEvent()->getOrder()->getPayment->getMethodInstance().

In version 1.5+ however I can't seem to find any solutions. I also tried with getData() but without any results.

Any help is appreciated. thanks

Calling me super desperate for an answer would be an understatement.


回答1:


It looks like the only data passed in to the sales_order_invoice_pay event is $this, which will be the sales/order_invoice model. I found this by searching through the Magneto core code, it's fired off in Invoice.php like so:

Mage::dispatchEvent('sales_order_invoice_pay', array($this->_eventObject=>$this));

Looking at a similar event (sales_order_invoice_register) which has an observer in the core (of Enterprise, at least - increaseOrderGiftCardInvoicedAmount() in GiftCardAccount) you can access the Invoice object like this in your Observer method:

$invoice = $observer->getEvent()->getInvoice();

The invoice is all you will be able to get though, since it's the only thing passed to the Observers by dispatchEvent(). You cannot directly access the order, like you are trying to do.

Looking at the Invoice model, however, it appears to have a nice getOrder method, which should do the trick. I haven't tested it, but try this:

$observer->getEvent()->getInvoice()->getOrder()->getPayment->getMethodInstance();

Cheers and good luck!




回答2:


i can get the payment method code using this

$observer->getEvent()->getInvoice()->getOrder()->getPayment()->getMethodInstance()->getCode()



回答3:


Using the debug() function of varien object is a good choice for such things.. like this: http://pastebin.com/wtdJdeLq - I can't get the code thingie to work on this site, not gonna bother trying anymore. Check the pastebin link.

There you have the order_id, load the order like you normally would, getModel('...') (is pretty fast), and get the payment method.




回答4:


user this code:$order->getPayment()->getMethodInstance()->getCode() ;



来源:https://stackoverflow.com/questions/6193054/how-to-get-payment-method-in-an-observer-in-magento

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!