How to get payment method in an observer in Magento? - magento

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.

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!

i can get the payment method code using this
$observer->getEvent()->getInvoice()->getOrder()->getPayment()->getMethodInstance()->getCode()

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

Related

Fire an action when a link gets clicked to a page that has the same action (VoltRb)

At http://localhost:3000/books, I have an index page where I have a list of books.
When you click on one of the links, the action to which it is bound, book, gets fired:
However, when you click on one of the links from one of the book pages, the book action doesn't get fired:
Note that the URL does change when the links are clicked, from both the index page and the book pages, but the problem that I'm having is that book doesn't get activated when you click on a link to a book page from another book page. How can I fix a situation like this?
FYI, here is a repo where this problem can be reproduced.
The method book doesn't get called twice because your view is already setup. The change in the url only triggers an reactive update in your view.
What is it that you are trying to achieve?
As it turns out, Volt computations are a way to solve this problem. By using the params collection, you can attach computations to changes in the URL. I solved this issue and got book to run on changes in the URL like so: https://github.com/ylluminarious/volt_action_problems/commit/5fcefc16d7b14a980f68ce572a80f25540d21636.
Sorry for the late reply. This is a known issue thats on my to fix list. As GDP2 mentioned, you can use .watch! to handle it, or the probably better way to do it is to write your controllers in a more functional way so that the data being pulled from params is used in methods instead of setting instance variables.
So for example, if your using the params in a query, instead of doing something like:
attr_reader :query
def index
#item = store.items.where(name: params._name).first
end
You could do something like:
def query
store.items.where(name: params._name).first
end
This might seem less efficient, but there's a lot of caching and this is pretty much just as efficient.
Once I get time though, I'll make it retrigger the action method when accessed data changes. (Sorry, just haven't gotten to it.)

Check if product has tier pricing

I am not that knowledgable with the Magento API yet, so I don't know where to search.
I found in the docs getTierPriceHtml($product) but that doesn't really help me.
So, is there a function that I can call on my $product object (in a template) and check if the product has tier pricing? If there is no a direct function in existence, then a short workaround will also be helpful.
I tried if (getTierPriceHtml($product) != null).... but that didn't work and is quite an ugly approach anyway.
Try it with $product->getTierPrices(). If that returns an empty array it means that it doesn't have any tier prices at all.

Payment method sort order. Won't work

I'm trying to change my payment method sort order but it won't work.
I have 2 payment method modules, and i need select the output order, already changed on config.xml of each module.
But don't change the order. I've try to change the call of phtml, to getChildHtml('payment',true,true); but neither work too.
I'm out of ideas, someone can help me??
Thanks guys.

Get list of currently dispatched events

I know this question may seem weird but I'd like to get a list of currently dispatched events.
The thing is that I am a lazy man and I would like to check if the 'checkout_cart_add_product_complete' has been fired without creating an observer for it.
So the idea is to get an array of all dispatched events and do an in_array on it :)
I thought that Mage::getEvents()->getAllEvents() would throw some info but it just returns an empty array.
I also digged a bit in lib/Varien/Event files and folders but didn't manage to be successful at creating an observer programmatically. Yep, I know, why being simple while one can be complicated ? :)
So this main question (getting a list of dispatched events) hides another (for the pure knowledge) wich would be "how to create an observer programmatically".
What do you think?
Thanks a lot!
Take a look at dispatchEvent and you'll see that events are only loaded from the assorted config.xml files, via SimpleXML. I cannot see any way to intercept this except to override Mage_Core_Model_App.
Of course there cannot be an event-dispatched-event, that would create an infinite loop, so there is no way to observe all events.
If you need to see events for development my advice would be to set a breakpoint in dispatchEvent with your debugger, that way you get to see not only the event names but also the objects passed as parameters too. I've tried other ways before but this was most convenient for me.
I need to do the same and I think it's possible to trick magento by the function getEventConfig in Mage_Core_Model_Config. You could force him to add automatically a default observer.

Get customer object on an event

I’m trying to createan observer on the following event.:
‘sales_order_payment_pay’.
However according to magento doc
http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/magento_events
I don’t have so much parameters avalaible on this event..
Do you have any idea how I could retrieve the customer object (i would need info such as customer id and customer email)?
Thanks for your feedback and anyway I wish you a nice day,
Anselme
This event does expose the payment object, so you should be able to chain off of that to get the object you want:
public function yourObserverFunction($event) {
$payment = $event['payment'];
$customer = $payment->getOrder()->getCustomer();
// ... do something useful
}
Generally objects in Magento can be chained like this, and now your code doesn't rely on the event being triggered from a customer session (which is not a good assumption anyway).
Hope that helps!
Thanks,
Joe
Every event exposes different objects inside of the $observer object that is passed around. In Magento you can often get a lot of stuff by referring to any number of objects that are in the request or session. In this case there is a customer/session object (Mage_Customer_Model_Session) which has the customer attached.
if(Mage::getSingleton('customer/session')->isLoggedIn()){
Mage::getSingleton('customer/session')->getCustomer();
}

Resources