Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Executing code when specific item is purchased
 
DerekL
Jr. Member
 
Total Posts:  1
Joined:  2009-03-31
 

Thank you in advance for your help.

I am looking for a way to execute code when a specific item is purchased.  We want to be able to track purchases on a separate server.

For example:
If item #99 is a special pair of shoes, we want to notify the separate server that item #99 was purchased.

Can we attach code to a specific item? or would we run script at the end of checkout that searches through the items purchased?

Thanks,

Derek

 
Magento Community Magento Community
Magento Community
Magento Community
 
LindyKyaw
Sr. Member
 
Avatar
Total Posts:  138
Joined:  2008-01-02
Los Angeles
 

If you are using one page checkout, there is an event dispatch when the order is placed

Mage::dispatchEvent('checkout_type_onepage_save_order_after', array('order'=>$order'quote'=>$this->getQuote()));

You can do in your local module config.xml:

<frontend>
<
events>
        <
checkout_type_onepage_save_order_after>
                <
observers>
                    <
my_one_page_save_after_event>
                        <
type>singleton</type>
                        <class>
Mymodule/Myclass</class>
                        <
method>myMethod</method>
                    </
my_one_page_save_after_event>
                </
observers>
            </
checkout_type_onepage_save_order_after>
</
events>
</
frontend>

In your class, you can do

public function myMethod($observer)
{
     $order
$observer->getEvent()->getOrder();
     foreach (
$order->getAllItems() as $item{
        
if($item->sthSpecial()) {
             
//do your logic
        
}
    }
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
sandiegoarms
Jr. Member
 
Total Posts:  5
Joined:  2012-02-16
 

Instead of listening for event checkout_type_onepage_save_order_after, I think it would be better to listen for a more generic event such as:

sales_order_place_after
sales_order_save_after

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top