I have a multiselect attribute in my categories named location
How do I display the selected / saved values on the frontend?
Thanks
you have to add the attribute to the colleciton via event:
"catalog_category_flat_loadnodes_before"
$observer->getSelect()->columns(
array( 'location' )
);
Try this:
$category->getResource()
->getAttribute('location')
->getSource()
->getOptionText($category->getData('location'))
register an ovserver in magento via xml:
<events>
<catalog_category_flat_loadnodes_before>
<observers>
<category_add_attribute>
<type>model</type>
<class>myModule/observer_catalog_category</class>
<method>addMenuAttributes</method>
</category_add_attribute>
</observers>
</catalog_category_flat_loadnodes_before>
</events>
and then in your Class
class MyModule_Namespace_Model_Observer_Catalog_Category
{
public function addMenuAttributes( Varien_Event_Observer $observer )
{
$observer->getSelect()->columns(
array( 'custom_attribute_name' )
);
}
}
add the customAttribute
Related
I am Using Observer Event in my custom module's Config.xml
<controller_action_layout_render_before>
<observers>
<Test_Check_Model_Observer>
<class>Test_Check_Helper_Data</class>
<method>checkValidi</method>
</Test_Check_Model_Observer>
</observers> </controller_action_layout_render_before>
Now in Test/Check/Helper/Data checkValidi Method I am inserting a block in Content.
>
class Test_Check_Helper_Data extends Mage_Core_Helper_Abstract {
> public function checkValidi($observer) {
> $layout = Mage::app()->getLayout();
> $content = $layout->getBlock('content');
> $block = 'hello! I am Working';
> $content->insert($block);
> }
But In Frontend When I am Filling Checkout billing and other information it gives me an Error Call to a member function insert() on a non-object in right side bar of Your Checkout Progress ,Please Give me any Solution for that Thanks
If you look at layout of checkout module checkout.xml you can see that some handles do not have content block. Eg, checkout_onepage_progress_billing or checkout_onepage_progress_shipping
So, you will get non-object error with your code. I think you should check $block variable before calling method.
$layout = Mage::app()->getLayout();
$content = $layout->getBlock('content');
if ($content) {
$block = 'hello! I am Working';
$content->insert($block);
}
It is not good idea to call helper from observer.
Observer always call a model file ,it did not class
i have modify and code config.xml code is
<global>
<models>
<testcheck>
<class>Test_Check_Model</class>
</testcheck>
</models>
</global>
<events>
<controller_action_layout_render_before>
<observers>
<test_check_codel_observer>
<type>singleton</type>
<class>testcheck/observer</class>
<method>your_function_name</method>
</test_check_codel_observer>
</observers>
</controller_action_layout_render_before>
</events>
And then Create Observer Observer.php file Under app/code/YourcodePoll/Test/Check/Model/
and below
code in Observer.php
is
<?php class Test_Check_Model_Observer
{
public function your_function_name($observer){
$block = $this->getLayout()
->createBlock('core/text', 'example-block')
->setText('<h1>This is a text block</h1>');
$observer->getEvent()->getLayout()->getBlock('content')->append($block);
//$observer->getEvent()->getLayout()->getUpdate();
}
}
I am trying to use addHandle(), but using the following generates error:
public function HandleMe($observer)
$update = $observer->getEvent()->getLayout()->getUpdate();
$update->addHandle('handlename');
raises a "Fatal error: Call to a member function getUpdate()"
You have to load core/layout before you update the layout, So try follow below code,
public function addCustomHandles($observer) {
$update = Mage::getSingleton('core/layout')->getUpdate();
//Your code here..
}
Or refer below link,
Link 1
Link 2
try using following approach :
First add this to your config.xml in yourcustommodule:
<config>
<frontend>
<events>
<controller_action_layout_load_before>
<observers>
<yourcustomtheme_observer>
<class>yourcustomtheme/observer</class>
<method>addHandles</method>
</yourcustomtheme_observer>
</observers>
</controller_action_layout_load_before>
</events>
</frontend>
</config>
And then add following method to your observer
class YourPackage_YourCustomTheme_Model_Observer extends CLS_Core_Model_Abstract
{
public function addHandles($observer) {
$category = Mage::registry('current_category');
if ($category instanceof Mage_Catalog_Model_Category) {
$update = Mage::getSingleton('core/layout')->getUpdate();
$fertilility = (count($category->getChildrenCategories()->getData())) ? 'parent' : 'nochildren';
$update->addHandle('catalog_category_' . $fertilility);
}
return $this;
}
}
PS : this is only for reference, so that you can check whether you are correctly using observer and handles or not.
I'm new in Magento and I need to send an email to a specific admin when another admin creates a new product in Magento.
Please, how can I implement it? Thanks!
Add an event to your modules config.xml in global-tag or maybe better adminhtml-tag
...
<events>
<catalog_product_save_after>
<observers>
<yourmodule>
<type>singleton</type>
<class>yourmodule/observer</class>
<method>sendTransactionalMail</method>
</yourmodule>
</observers>
</catalog_product_save_after>
</events>
In your observer class yourmodule/observer which is in fact Your_Module_Model_Observer
just send the transactional mail:
public function sendTransactionalMail(){
$senderName = 'NAME';
$senderEmail = 'your#email.com';
$sender = array('name' => $senderName,'email' => $senderEmail);
$storeId = Mage::app()->getStore()->getId();
$vars = array('subject' => 'Product Save Notification');
$transactionalEmail= Mage::getModel('core/email_template')->setDesignConfig(array('area'=>'frontend', 'store'=>$storeId));
//create a new template via adminhtml and grab the id
$emailTemplateId = 17;
$transactionalEmail->sendTransactional($emailTemplateId, $sender, $recepientEmail, "Admin", $vars);
}
I would create an observer for the catalog_product_new_action event or maybe catalog_product_prepare_save event and then send the transactional.
Create observer tutorial:
http://www.pierrefay.com/event-observers-magento-tutorial-howto-105
Send transactional tutorial:
http://www.excellencemagentoblog.com/magento-sending-custom-emails
I want to change some values of some products while adding them to the cart in Magento CE 1.7
Im trying with the Observer checkout_cart_add_product_complete for this. Change the price (CustomPrice) works well if I try to change the product name or product images, its not saved.
Is there a way to change this attributes already on adding to cart?
public function checkout_cart_add_product_complete(Varien_Event_Observer $observer) {
[...]
// Set new Price
$lastAddedItem->setOriginalCustomPrice($originalProduct->getPrice());
$lastAddedItem->setCustomPrice($originalProduct->getPrice());
// Set Product-Name
$lastAddedItem->setName($originalProduct->getName());
// Set Product-Images
$lastAddedItem->setImage($originalProduct->getImage());
$lastAddedItem->setSmallImage($originalProduct->getSmallImage());
$lastAddedItem->setThumbnail($originalProduct->getThumbnail());
// Save updated Item and Cart
//$lastAddedItem->save();
Mage::getSingleton('checkout/cart')->save();
// Recalc Totals and save
$quote->setTotalsCollectedFlag(false);
$quote->collectTotals();
$quote->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
}
The function Mage_Sales_Model_Quote_Item::setProduct resets some of the basic information each time a product is saved (updated). Luckily there is an event "sales_quote_item_set_product" you can latch onto.
config.xml
<config>
...
<global>
<events>
<sales_quote_item_set_product>
<observers>
<samples>
<type>singleton</type>
<class>samples/observer</class>
<method>salesQuoteItemSetProduct</method>
</samples>
</observers>
</sales_quote_item_set_product>
</events>
<global>
...
</config>
Observer.php
class Mynamespace_Samples_Model_Observer
{
public function salesQuoteItemSetProduct(Varien_Event_Observer $observer)
{
/* #var $item Mage_Sales_Model_Quote_Item */
$item = $observer->getQuoteItem();
$item->setName('Ians custom product name');
return $this;
}
}
I'm creating a custom extension and I would like to add a custom option when a certain item is purchased.
For example, when the product "Name Tag" is purchased, the extension would detect that the specific product has been ordered and assign a custom option of "Year" to it. The user does not see this, but the attribute is added and displayed in the admin when viewing the order.
Are there any specific listeners our there to accomplish this?
EDIT Based on Ben's suggestions in the comments, I'm editing the comment to reflect progress I've made with the answers located here: https://stackoverflow.com/a/9496266/268165
I now have an extension running under Namespace_addYear and the following code running:
Namespace/addYear/etc/config.xml:
<?xml version="1.0" encoding="utf-8"?>
<config>
<modules>
<Namespace_addYear>
<version>1.0.0</version>
</Namespace_addYear>
</modules>
<global>
</global>
<frontend>
<events>
<catalog_product_load_after>
<observers>
<Namespace_addYear>
<type>model</type>
<class>addYear/observer</class>
<method>catalogProductLoadAfter</method>
</Namespace_addYear>
</observers>
</catalog_product_load_after>
</events>
</frontend>
</config>
Namespace/addYear/Model/Observer.php:
class Namespace_addYear_Model_Observer
public function catalogProductLoadAfter(Varien_Event_Observer $observer)
{
// set the additional options on the product
$action = Mage::app()->getFrontController()->getAction();
if ($action->getFullActionName() == 'checkout_cart_add')
{
// assuming you are posting your custom form values in an array called extra_options...
if ($options = $action->getRequest()->getParam('extra_options'))
{
$product = $observer->getProduct();
// add to the additional options array
$additionalOptions = array();
if ($additionalOption = $product->getCustomOption('additional_options'))
{
$additionalOptions = (array) unserialize($additionalOption->getValue());
}
foreach ($options as $key => $value)
{
$additionalOptions[] = array(
'label' => $key,
'value' => $value,
);
}
// add the additional options array with the option code additional_options
$observer->getProduct()
->addCustomOption('additional_options', serialize($additionalOptions));
}
}
}
}
The Observer.php was taken from the answer linked above. So far, I cannot get the store to do anything other than display a 404 page when I remove the tags in config.xml.
Is there anything someone can immediately see that is wrong?