[英]Magento 1.7 Observer Module
我一直在努力使我的觀察者模塊正常工作。 我正在使用Magento 1.7.0.2,對於XML一切似乎都是正確的。 沒有錯誤,但我不認為這是在射擊。 我希望它在事件sale_order_save_after上做以下三件事:
如果訂單狀態為“完成”,請執行以下操作:
1)將自定義屬性“位置”更改為“已售”
2)將可見性從目錄/搜索更改為目錄。
3)將訂購的所有產品從其所有分配的類別中刪除到一個新的類別(ID:80)中
最后保存/刷新緩存。 我的php代碼不完整,但我至少希望將其觸發。 第一步和第二步應該起作用,不確定如何以編程方式更改類別。
這是我的代碼:
App /代碼/本地/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 /代碼/本地/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));
//}//
}
?>
任何幫助將不勝感激,因為我似乎無法解決這個問題。 在此先感謝您的幫助。
您可以使用此鏈接
http://snipplr.com/view/56959/
並且也使您的etc / modules / config.xml
或者您可以將您現有的代碼與此代碼進行比較,您可以確定找到解決方案以在任意下單后的保存方法后添加代碼。
希望對您有所幫助。
看來您的config.xml
是問題所在。 您在adminhtml
而不是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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.