Magento module observer not firing - php

Can you see why the Mage::log's in my observer are not firing? Very frustrated. I'm expecting them to log after I add an item to cart, but they are not. Can you see issue?
I have logging enabled. The module is showing up on the list under config>advanced. I have cache disabled but am clearing for good measure.
config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Caitlinhavener_Dynamicprice>
<version>0.1.0</version>
</Caitlinhavener_Dynamicprice>
</modules>
<global>
<models>
<chdispatcher>
<class>Caitlinhavener_Dynamicprice_Model</class>
</chdispatcher>
</models>
<frontend>
<events>
<checkout_cart_product_add_after>
<observers>
<modify_to_custom_price>
<class>Caitlinhavener_Dynamicprice_Model_Observer</class>
<method>modifyPrice</method>
</modify_to_custom_price>
</observers>
</checkout_cart_product_add_after>
</events>
</frontend>
</global>
</config>
Observer.php:
<?php
Mage::log('Im here')
or exit("unable to log");
class Caitlinhavener_Dynamicprice_Model_Observer
{
public function modifyPrice(Varien_Event_Observer $obs)
{
// Get the quote item
$item = $obs->getQuoteItem();
Mage::log('Get Quote Item '. var_dump($_item->debug());
// Ensure we have the parent item, if it has one
$item = ( $item->getParentItem() ? $item->getParentItem() : $item );
Mage::log('Get parent item ' . var_dump($_item->debug());
// Load the custom price
//$price = "your custom price logic";
$price = Mage::registry('dynamic_tier_price');
Mage::log('Price is ' . $price);
// Set the custom price
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
// Enable super mode on the product.
$item->getProduct()->setIsSuperMode(true);
Mage::log('Item after super mode ' . var_dump($_item->debug());
}
}
?>

Your config.xml is incorrect (remove <frontend> tag)
<config>
..
<global>
<models>
<chdispatcher>
<class>Caitlinhavener_Dynamicprice_Model</class>
</chdispatcher>
</models>
<events>
...
</events>
In magento you have 3 different event scope (see Magento: Event Observer Scope)
In your config.xml
<config>
<frontend>
<events>
...
</events>
<frontend>
<adminhtml>
<events>
...
</events>
<adminhtml>
<global>
<events>
...
</events>
<global>
</config>

Related

try to override model but not working in magento

Try to override model of Mage_Paypal_Model_Express_Checkout but not working.
I try to override start() in "Mage_Paypal_Model_Express_Checkout" in magento.
The bellow code used but not working.
File : code/local/Live/Paypal/Model/Express/Checkout.php
class Live_Paypal_Model_Express_Checkout extends
Mage_Paypal_Model_Express_Checkout
{
public function start($returnUrl, $cancelUrl, $button = null)
{
echo "fasdfasdf";exit;
}
}
File : code/local/Live/Paypal/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Live_Paypal>
<version>1.0.0</version>
</Live_Paypal>
</modules>
<global>
<models>
<paypal>
<rewrite>
<express_checkout>Live_Paypal_Model_Express_Checkout</express_checkout>
</rewrite>
</paypal>
</models>
</global>
</config>
File : app/etc/modules/Live_Paypal.xml
<?xml version="1.0"?>
<config>
<modules>
<Live_Paypal>
<active>true</active>
<codePool>local</codePool>
</Live_Paypal>
</modules>
</config>

Magento 1.7 Observer Method

I believe I have a config issue with the code below, module is activated but observer is not being fired on event. Can anyone spot the issue?
app/etc/modules/James_CoreProductCheck.xml
<?xml version="1.0"?>
<config>
<modules>
<James_CoreProductCheck>
<active>true</active>
<codePool>local</codePool>
</James_CoreProductCheck>
</modules>
</config>
app/code/local/James/CoreProductCheck/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<James_CoreProductCheck>
<version>0.0.1</version>
</James_CoreProductCheck>
</modules>
<global>
<models>
<james_coreproductcheck>
<class>James_CoreProductCheck_Model</class>
</james_coreproductcheck>
</models>
<events>
<checkout_cart_product_add_after>
<observers>
<james_coreproduct_check_model_observer>
<type>singleton</type>
<class>James_CoreProductCheck_Model_Observer</class>
<method>check</method>
</james_coreproduct_check_model_observer>
</observers>
</checkout_cart_product_add_after>
</events>
</global>
</config>
app/code/local/James/CoreProductCheck/Model/Observer.php
class James_CoreProductCheck_Model_Observer {
public function check(Varien_Event_Observer $observer) {
Mage::log('Yet another product added', null, 'product-updates.log');
}
}
try this one.
<events>
<checkout_cart_product_add_after>
<observers>
<sales_quote_add_item>
<class>James_CoreProductCheck_Model_Observer</class>
<method>check</method>
</sales_quote_add_item>
</observers>
</checkout_cart_product_add_after>
</events>
Make sure you clear cache. Thanks :)
Try Different Module name instead of CoreProductCheck.
so your code will look like following and it will work perfectly.
app/code/local/James/Prodcheck/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<James_Prodcheck>
<version>1.0.0</version>
</James_Prodcheck>
</modules>
<global>
<events>
<checkout_cart_product_add_after>
<observers>
<checkout_cart_product_add_after>
<type>singleton</type>
<class>prodcheck/observer</class>
<method>check</method>
<args></args>
</checkout_cart_product_add_after>
</observers>
</checkout_cart_product_add_after>
</events>
</global>
</config>
and observer file app/code/local/James/Prodcheck/Model/Observer.php
class James_Prodcheck_Model_Observer
{
public function check(Varien_Event_Observer $observer)
{
//Your code stuff.
}
}

Magento observer is not viewing product details

I have created custom checkout_cart_save_before observer in magento by create the following files
app/etc/modules/Cart_Override.xml
<?xml version="1.0"?>
<config>
<modules>
<Cart_Override>
<codePool>local</codePool>
<active>true</active>
<depends>
<Mage_Contacts />
</depends>
</Cart_Override>
</modules>
</config>
app/code/local/Cart/Override/etc/config.xml
<?xml version="1.0"?>
<config>
<global>
<models>
<cartoverride>
<class>Cart_Override_Model</class>
</cartoverride>
</models>
<events>
<checkout_cart_save_before>
<observers>
<cart_override_qty_observer>
<type>singleton</type>
<class>Cart_Override_Model_Qtyc_Observer</class>
<method>checkout_cart_save_before</method>
</cart_override_qty_observer>
</observers>
</checkout_cart_save_before>
</events>
</global>
</config>
and app/code/local/Cart/Override/Model/Qtyc/Observer.php
class Cart_Override_Model_Qtyc_Observer extends Varien_Event_Observer
{
public function checkout_cart_save_before($observer)
{
$action = Mage::app()->getFrontController()->getAction();
$product = $observer->getProduct();
echo "<pre>";
print_r($product);
echo "</pre>";
die();
}
}
The observer is working fine, when i click the add to cart button it is going to checkout_cart_save_before function. But i could not get the product values from the observer using the following code inside the checkout_cart_save_before function
$product = $observer->getProduct();
I have to add something for get the product details from the observer parameter?....any guess??
Have a look at Mage_Checkout_Model_Cart::save() where the event is dispatched:
Mage::dispatchEvent('checkout_cart_save_before', array('cart'=>$this));
You can access the cart data with $cart = $observer->getEvent()->getCart(); and the quote items with $cart->getItems().

Magento showConfig module is not working

I start study magento from Magento for Developers guide. So I create module with their names. But it is showing following error when i trying http://works.dev/magento/?showConfig=true.
XML Parsing Error: XML or text declaration not at start of entity
Location: http://works.dev/magento/?showConfig=true
Line Number 1, Column 2: <?xml version="1.0"?>
-^
(works.dev is my localhost domain. magento path is working fine.)
This is my configuration files.
app/code/local/Magentotutorial/Configviewer/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Magentotutorial_Configviewer>
<version>0.1.0</version>
</Magentotutorial_Configviewer>
</modules>
<global>
<events>
<controller_front_init_routers>
<observers>
<Magentotutorial_configviewer_model_observer>
<type>singleton</type>
<class>Magentotutorial_Configviewer_Model_Observer</class>
<method>checkForConfigRequest</method>
</Magentotutorial_configviewer_model_observer>
</observers>
</controller_front_init_routers>
</events>
</global>
</config>
app/etc/modules/Magentotutorial_Configviewer.xml
<?xml version="1.0"?>
<config>
<modules>
<Magentotutorial_Configviewer>
<active>true</active>
<codePool>local</codePool>
</Magentotutorial_Configviewer>
</modules>
</config>
app/code/local/Magentotutorial/Configviewer/Model/Observer.php
<?php
class Magentotutorial_Configviewer_Model_Observer {
const FLAG_SHOW_CONFIG = 'showConfig';
const FLAG_SHOW_CONFIG_FORMAT = 'showConfigFormat';
private $request;
public function checkForConfigRequest($observer) {
$this->request = $observer->getEvent()->getData('front')->getRequest();
if($this->request->{self::FLAG_SHOW_CONFIG} === 'true'){
$this->setHeader();
$this->outputConfig();
}
}
private function setHeader() {
$format = isset($this->request->{self::FLAG_SHOW_CONFIG_FORMAT}) ?
$this->request->{self::FLAG_SHOW_CONFIG_FORMAT} : 'xml';
switch($format){
case 'text':
header("Content-Type: text/plain");
break;
default:
header("Content-Type: text/xml");
}
}
private function outputConfig() {
die(Mage::app()->getConfig()->getNode()->asXML());
}
}
I did clear cache and check logs. I cant find problem from those.
According #Jürgen Thelen it was space issue. It had worked after remove space before the
Ausuraya,can you please change config.xml
<global>
<models>
<configviewer>
<class>Magentotutorial_Configviewer_Model</class>
</configviewer>
</models>
<events>
<controller_front_init_routers>
<observers>
<Magentotutorial_configviewer_model_observer>
<type>singleton</type>
<class>configviewer/observer</class>
<method>checkForConfigRequest</method>
</Magentotutorial_configviewer_model_observer>
</observers>
</controller_front_init_routers>
</events>
</global>

Magento: can't capture dispatched event

In order to develop an automated order status changer based on payment method and event type, I created the following module:
/GT/OrderFlo/etc/config.xml
<!--?xml version="1.0"?-->
<config>
<modules>
<gt_orderflow>
<version>1.0</version>
</gt_orderflow>
</modules>
<global>
<models>
<orderflow>
<class>GT_OrderFlow_Model</class>
</orderhook>
</models>
<events>
<sales_order_place_after>
<observers>
<order_payment_pending_autostatus>
<type>singleton</type>
<class>orderflow/observer</class>
<method>implementOrderStatus</method>
</order_payment_pending_autostatus>
</observers>
</sales_order_place_after>
<sales_order_shipment_save_after>
<observers>
<order_invoice_pending_autostatus>
<type>singleton</type>
<class>orderflow/observer</class>
<method>implementOrderStatus</method>
</order_invoice_pending_autostatus>
</observers>
</sales_order_shipment_save_after>
<sales_order_invoice_save_commit_after>
<observers>
<order_complete_autostatus>
<type>singleton</type>
<class>orderflow/observer</class>
<method>implementOrderStatus</method>
</order_complete_autostatus>
</observers>
</sales_order_invoice_save_commit_after>
</events>
</global>
</config>
/GT/OrderFlow/Model/Observer.php
class GT_OrderFlow_Model_Observer
{
public function implementOrderStatus($event)
{
$order = $event->getOrder();
$payment_method = $this->_getPaymentMethod($order);
$this->_log('In implementOrderStatus with payment method: '.$payment_method);
Mage::log('In implementOrderStatus with payment method: '.$payment_method);
$next_status = "";
return $this;
}
private function _getPaymentMethod($order)
{
return $order->getPayment()->getMethodInstance()->getCode();
}
private function _log($message)
{
return Mage::log($message, null, 'gt_orderflow.log');
}
}
The code was replicated from http://www.atwix.com/magento/auto-invoice-and-custom-order-status-upon-checkout/.
But how can I get it to fire the observer after sales_order_place_after event?
Is your module loaded at all? You will need a file in /app/etc/modules. On a related note, I see that you refer to your module as <gt_orderflow> but the class prefix is GT_OrderFlow. On a case sensitive filesystem, this is important and will not work this way. The nodes under config/modules are mapped to the module path.
I think you have a typo
<models>
<orderflow>
-----^
<class>GT_OrderFlow_Model</class>
</orderhook>
-----^
</models>

Categories