我尝试在购物车中添加额外的产品。我为此创建了一个观察者。
<?php
class WP_Plugadapter_Model_Observer
{
public function hookToControllerActionPostDispatch($observer)
{
if($observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add')
{
Mage::dispatchEvent("add_to_cart_after", array('request' => $observer->getControllerAction()->getRequest()));
}
}
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)){
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct(10934, 1);
$cart->save();
if (!$cart->getQuote()->getHasError()){
Mage::log("Product ADD TO CART is added to cart.");
}else{
Mage::log("BOEM");
}
}
}
}
}
当我检查我的系统日志时,我看到以下日志消息。产品添加到购物车已添加到购物车。
我不知道我做错了什么。当独立加载脚本时,它工作正常。例如:
<?php
include_once '../app/Mage.php';
Mage::app();
umask(0);
$session = Mage::getSingleton('core/session', array('name'=>'frontend'));
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct(10934, 1);
$cart->save();
是否有可能在观察者中您可以以不同的方式进行操作?