Magento observer is not viewing product details - php

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().

Related

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 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>

Calling a Magento event Observer AFTER payment capture

Im trying to call an Observer after the order has been created, and AFTER payment has been captured.
So far I've tried;
checkout_submit_all_after,
sales_order_payment_place_end,
sales_order_place_after,
sales_order_payment_pay,
sales_order_payment_capture,
sales_order_payment_transaction_save_after
Just to name the main ones.
I've also logged all Event Dispaches inside dispatchEvent() but found nothing that stands out and is only fired when i need it.
the issue i'm having is that the status of the order is always ether 'Payment Pending' or something that predated this; meaning that i don't know whether the order will fail or succeed.
My aim, is to fire a function only on successful orders.
thanks.
after much more testing i found the following Observer to do the trick;
checkout_onepage_controller_success_action
This returns just the order id, so;
$order_id = $observer->getData('order_ids');
$order = Mage::getModel('sales/order')->load($order_id);
and you see that the order status is 'processing' and the payment is aproved (or not).
1 ) here is custom config.xml for call observer file
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Modulename>
<version>0.1.0</version>
</Namespace_Modulename>
</modules>
<frontend>
<events>
<checkout_submit_all_after>
<observers>
<Namespace_Modulename_Customevent>
<type>singleton</type>
<class>Namespace_Modulename_Model_Observer</class>
<method>customFunction</method>
</Namespace_Modulename_Customevent>
</observers>
</checkout_submit_all_after>
</events>
</frontend>
</config>
2 ) create observer.php file inside your module/Model directory and paste this code
<?php
class Namespace_Modulename_Model_Observer
{
public function customFunction(Varien_Event_Observer $observer)
{
$order = $observer->getEvent()->getOrder();
//here you can add your custom code
}
}
please try this.. sure it will help you !
I have also tried all event but didn't get success. Then I moved to override Mage OnePageController and called my custom function. Below is the code to override onestep checkout.
app\etc\modules\Namespace_Module.xml
<Namespace_Checkout>
<active>true</active>
<codePool>local</codePool>
</Namespace_Checkout>
app\code\local\Namespace\Checkout\etc\config.xml
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Checkout>
<version>0.1.0</version>
</Namespace_Checkout>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<Namespace_Checkout before="Mage_OneStepCheckout">Namespace_Checkout</Namespace_Checkout>
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>
app\code\local\Namespace\Checkout\controllers\OnepageController.php
<?php
require_once 'Mage/Checkout/controllers/OnepageController.php';
class Namespace_Checkout_OnepageController extends Mage_Checkout_OnepageController{
public function successAction(){
$session = $this->getOnepage()->getCheckout();
if (!$session->getLastSuccessQuoteId()) {
$this->_redirect('checkout/cart');
return;
}
$lastQuoteId = $session->getLastQuoteId();
$lastOrderId = $session->getLastOrderId();
$lastRecurringProfiles = $session->getLastRecurringProfileIds();
if (!$lastQuoteId || (!$lastOrderId && empty($lastRecurringProfiles))) {
$this->_redirect('checkout/cart');
return;
}
$this->customFunction(); // Custom function to call
$session->clear();
$this->loadLayout();
$this->_initLayoutMessages('checkout/session');
Mage::dispatchEvent('checkout_onepage_controller_success_action', array('order_ids' => array($lastOrderId)));
$this->renderLayout();
}
function customFunction(){
// This function is calling before clearing order session
//Here you can put all your custom code
}
}
?>
In the above controller, I have added customFunction() wher you can put your custom code.
Hope it will help you!

Magento module observer not firing

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>

magento sales_order_place_after observer not working

I have created an event observer that will send email on order completion.
I am sending email to email_address1 when a product is ordered from the category1 and sending email to email_address2 when a product is ordered from the cateogory2. Therefore I have created an observer event for this.
But when I click on the "Place Order" button nothing happens. What is the problem?
in magento/app/etc/modules/Custom_Email.xml:
<?xml version="1.0"?>
<config>
<modules>
<Custom_Email>
<codePool>local</codePool>
<active>true</active>
</Custom_Email>
</modules>
</config>
in magento/app/code/local/Custom/Email/etc/config.xml:
<?xml version="1.0"?>
<config>
<global>
<models>
<customemail>
<class>Custom_Email_Model</class>
</customemail>
</models>
<events>
<sales_order_place_after>
<observers>
<custom_email_order_observer>
<type>singleton</type>
<class>customemail/order_observer</class>
<method>sendOrder</method>
</custom_email_order_observer>
</observers>
</sales_order_place_after>
</events>
</global>
</config>
in magento/app/code/local/Custom/Email/Model/Order/Observer.php:
<?php
class Custom_Email_Model_Order_Observer
{
public function __contruct()
{
}
/**
* Exports new orders to an xml file
* #param Varien_Event_Observer $observer
* #return Feed_Sales_Model_Order_Observer
*/
public function sendOrder($observer){
$order = $observer->getEvent()->getOrder();
$cat_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
echo $cat_id;
//Implement logic here
...
$emailTemplate = Mage::getModel('core/email_template')
->loadDefault('rehab');
$emailTemplateVariables = array();
$emailTemplateVariables['order'] = $order;
$emailTemplate->setSenderName('Your shops name');
$emailTemplate->setSenderEmail('addres#from.com');
$emailTemplate->setTemplateSubject('Subject');
$emailTemplate->send('to#addres.com','Name', $emailTemplateVariables);
echo 'email sent';
}
}
?>
So any one know where is the problem?
The layer does not exist in the order context, nor is the category from which the product was selected available as a property of quote or order items. It's a known deficiency in Magento which unfortunately limits segmentation.

Categories