简体   繁体   中英

Magento 1.7 Observer Module

I have been struggling to get my observer module to work. I am using Magento 1.7.0.2 and everything appears to be correct for the XML. No errors, but I don't believe it's firing. I want it to do three things on the event sale_order_save_after as follows:

If Order status is 'Complete' then do the following:

1) Change custom attribute 'location' to 'SOLD'

2) Change visibility from Catalog/Search to Catalog.

3) Remove all products on order from all of their assigned categories to just one new category (which is ID:80)

And finally save/refresh cache. My php code is not complete, but I would at least like it to fire. The first step and second step should work, not sure how to handle changing the categories programmatically.

Here is what I have for code:

App/Code/Local/Pinnacle/Autoarchive/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Pinnacle_Autoarchive>
            <version>0.0.1</version>
        </Pinnacle_Autoarchive>
    </modules>
    <global>
        <models>
                <Pinnacle_Autoarchive>
                    <class>Pinnacle_Autoarchive_Model</class>
                </Pinnacle_Autoarchive>
        </models>
    </global>        
            <adminhtml>
                 <events>
                     <sales_order_save_after>
                         <observers>
                             <pinnacle_autoarchive>
                                <type>model</type>
                                <class>pinnacle_autoarchive/observer</class>
                                <method>salesOrderSaveAfter</method>
                             </pinnacle_autoarchive>
                         </observers>
                     </sales_order_save_after>
                </events>
       </adminhtml>
</config>     

App/Code/Local/Pinnacle/Autoarchive/Model/Observer.php

<?php
/*
 * Auto Archive all products on last order when status is complete
 */
class Pinnacle_Autoarchive_Model_Observer
{

public function salesOrderSaveAfter($observer)

{
   $order = $observer->getEvent()->getOrder();
        $orderStatus = $order->getStatus();


        if ($orderStatus == 'complete') {
                $items = $order->getAllItems();
                foreach ($items as $item) {
                    $productsToUpdate[] = $item->getProductId();
                }
                $theAttributeToUpdate = 'location';
                $theAttributeValue = 'SOLD';
                Mage::getSingleton('catalog/product_action')->updateAttributes($productsToUpdate, array($theAttributeToUpdate => $theAttributeValue), 0);
                }

        if ($orderStatus == 'complete') {
                $items = $order->getAllItems();
                foreach ($items as $item) {
                    $productsToUpdate[] = $item->getProductId();
                }
                $theAttributeToUpdate = 'visibility';
                $theAttributeValue = 'Catalog';
                Mage::getSingleton('catalog/product_action')->updateAttributes($productsToUpdate, array($theAttributeToUpdate => $theAttributeValue), 0);
                }

        //if ($orderStatus == 'complete') {
                //$items = $order->getAllItems();
                //foreach ($items as $item) {
                //    $productsToUpdate[] = $item->getProductId();
                //}

                //Mage::getSingleton('catalog/product_action')->$productsToUpdate->setCategoryIds(array(80));
                //}//            

}
?>

Any assistance would be appreciated, as I cannot seem to get this to work out. Thanks in advance for your help.

you can use this link

http://snipplr.com/view/56959/

And also make your etc/modules/config.xml

or you can compare your existing code with this one you can sure find your solution to add your code at after save method when any order place.

hope this will sure help you.

It looks like your config.xml is the problem. You have the observer defined inside of adminhtml instead of global .

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Pinnacle_Autoarchive>
            <version>0.0.1</version>
        </Pinnacle_Autoarchive>
    </modules>
    <global>
        <models>
                <Pinnacle_Autoarchive>
                    <class>Pinnacle_Autoarchive_Model</class>
                </Pinnacle_Autoarchive>
        </models>

        <events>
             <sales_order_save_after>
                   <observers>
                        <pinnacle_autoarchive>
                           <type>model</type>
                           <class>Pinnacle_Autoarchive_Model_Observer</class>
                           <method>salesOrderSaveAfter</method>
                         </pinnacle_autoarchive>
                    </observers>
              </sales_order_save_after>
         </events>

    </global>        
</config> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM