Magento 1.9观察者失效问题:如何测试观察者是否正常运行?
Let's break down why your Google redirect test isn't working, then fix the observer to reliably modify product prices when adding to cart (both before and after the action).
First: Why the Redirect Isn't Working
The checkout_cart_add_product_complete event fires after Magento has already initiated its own redirect logic (usually back to the cart or product page). Your header() call gets overridden by Magento's subsequent redirects. Additionally, if any output was sent to the browser before your observer runs (even whitespace in a PHP file), the header redirect will fail entirely.
Instead of using redirects for testing, rely on Magento's logging system—this is far more reliable for verifying observer triggers.
Step 1: Verify Your Module is Enabled
First, make sure your module is properly registered. Create app/etc/modules/Oroola_Oroolachildren.xml if it doesn't exist, with this content:
<?xml version="1.0"?> <config> <modules> <Oroola_Oroolachildren> <active>true</active> <codePool>local</codePool> </Oroola_Oroolachildren> </modules> </config>
After creating this, clear Magento's cache (System > Cache Management) to apply the change.
Step 2: Choose the Right Events for Price Modification
Magento has specific events for before/after adding to cart—pick the one that matches your needs:
- Before adding to cart: Use
checkout_cart_add_product_beforeto modify the product's price before it's added to the cart. - After adding to cart: Use
checkout_cart_product_add_afterto adjust the price of the cart item after it's been added (this is more flexible for cart-specific pricing).
Update your config.xml to use these events:
<?xml version="1.0"?> <config> <global> <events> <!-- Modify price BEFORE product is added to cart --> <checkout_cart_add_product_before> <observers> <oroola_oroolachildren_observer_before> <type>singleton</type> <class>oroola_oroolachildren/observer</class> <method>modifyPriceBeforeAdd</method> </oroola_oroolachildren_observer_before> </observers> </checkout_cart_add_product_before> <!-- Modify price AFTER product is added to cart --> <checkout_cart_product_add_after> <observers> <oroola_oroolachildren_observer_after> <type>singleton</type> <class>oroola_oroolachildren/observer</class> <method>modifyPriceAfterAdd</method> </oroola_oroolachildren_observer_after> </observers> </checkout_cart_product_add_after> </events> </global> </config>
Step 3: Update Your Observer Class
Replace your Observer.php code with this optimized version, which handles both before/after events and uses proper Magento practices:
<?php class Oroola_Oroolachildren_Model_Observer extends Varien_Event_Observer { /** * Modify product price BEFORE it's added to the cart */ public function modifyPriceBeforeAdd(Varien_Event_Observer $observer) { $product = $observer->getEvent()->getProduct(); if (!$product) { return; } // Calculate new price (adjust logic as needed) $newPrice = $product->getPrice() + rand(10, 100); // Override the product's price $product->setPrice($newPrice); // Log the change for verification Mage::log(sprintf( "Modified product %s (ID: %d) price to %f BEFORE adding to cart", $product->getName(), $product->getId(), $newPrice ), null, 'price_updates.log'); } /** * Modify cart item price AFTER it's added to the cart */ public function modifyPriceAfterAdd(Varien_Event_Observer $observer) { $item = $observer->getEvent()->getQuoteItem(); if (!$item) { return; } $product = $item->getProduct(); // Calculate new price (adjust logic as needed) $newPrice = $product->getPrice() + rand(10, 100); // Set custom price for the cart item $item->setCustomPrice($newPrice); $item->setOriginalCustomPrice($newPrice); // Enable super mode to bypass price validation $item->getProduct()->setIsSuperMode(true); // Log the change for verification Mage::log(sprintf( "Updated cart item %s (ID: %d) price to %f AFTER adding to cart", $product->getName(), $product->getId(), $newPrice ), null, 'price_updates.log'); } }
Step 4: Verify the Implementation
- Clear Magento's cache again to apply all config changes.
- Add a product to your cart.
- Check the log file at
var/log/price_updates.log(create thevar/logdirectory if it doesn't exist) to confirm the observer triggered and prices were modified. - View your cart to see the updated prices.
Key Notes to Remember
- Avoid redundant product loads: Use
$item->getProduct()instead of reloading the product withMage::getModel('catalog/product')->load()—this improves performance. - Cache is critical: Magento caches config files, so always clear cache after modifying
config.xml. - Class/file naming: Ensure your observer class name matches the file path and case (e.g.,
Oroola_Oroolachildren_Model_Observermaps toapp/code/local/Oroola/Oroolachildren/Model/Observer.php).
内容的提问来源于stack exchange,提问作者Prashant Patil

