Solving Cart Addition Issues with Magento Observers
Understanding the Problem
You’ve created an observer to add extra items to the cart when specific products are added. The observer is triggered correctly (checkout_cart_add event), but the added product isn’t appearing in the actual cart.
Let’s break down the issue:
- You’re using a
hookToControllerActionPostDispatchmethod to listen for thecheckout_cart_addevent. - When this event is fired, you dispatch a new event (
add_to_cart_after) and pass the request as data. - In the
hookToAddToCartAfterobserver, you check if the added product has an extra function of ‘121’. - If it does, you try to add another product (ID 10934) to the cart using the same session and cart instance.
What’s Going Wrong
The core issue seems to be in how you’re managing the Magento session and cart objects within your observer context.
In a standalone script, everything works fine because you’re starting a fresh application context. However, when running inside an observer triggered by a controller action, the session management is handled differently due to the MVC architecture of Zend Framework (which Magento is built upon).
The observer is called asynchronously after the main request has been processed. This means the session and cart objects are in a different state than expected.
The Correct Approach
To fix this issue, you need to ensure that both the observer and your add-to-cart logic are operating within the same application context. You can achieve this by using Magento’s built-in checkout/session model, which is designed for cart operations.
Here’s how you should modify your observer:
class WP_Plugadapter_Model_Observer
{
public function hookToAddToCartAfter($observer)
{
$request = $observer->getEvent()->getRequest()->getParams();
$_product = Mage::getModel('catalog/product')->load($request['product']);
$extra_functions = $_product->getExtra_functions();
if (!empty($extra_functions)) {
$extra_functions = explode(',', $extra_functions);
if (array_search('121', $extra_functions)) {
// Use the checkout/session model for cart operations
$checkout_session = Mage::getSingleton('checkout/session');
// Start a new quote session
$quote = $checkout_session->createEmptyQuote();
// Add the product to the quote
$quote->addProduct(Mage::getModel('catalog/product')->load(10934), 1);
// Set the quote as the current quote for the customer
$checkout_session->replaceQuote($quote);
if (!$checkout_session->getQuote()->getHasError()) {
Mage::log("Product ADD TO CART is added to cart.");
} else {
Mage::log("BOEM");
}
}
}
}
}
In this updated code:
- I’ve replaced the standard
checkout/cartsingleton with thecheckout/sessionmodel. - Before adding the product, I create a new empty quote using
$quote = $checkout_session->createEmptyQuote();. - The
addProductmethod is called on the new quote object. - Finally, I replace the existing quote in the session with the new one containing the added product:
$checkout_session->replaceQuote($quote);.
Conclusion
When working with Magento observers and cart operations, it’s crucial to understand how sessions and quotes are managed within different execution contexts.
By using the checkout/session model and creating a new empty quote before adding products, you ensure that your observer operates in sync with the main application context. This approach respects the MVC architecture of Magento and avoids the discrepancies you were encountering earlier.