magento event observer (magento1.7) - module

i am trying to implement an simple observer in my module (app/code/local/Foo/Bar).
My objective is to set the product names to '[product name] is cool' when a product is loaded in the frontend.
These are my files:
app/code/local/Foo/Bar/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<global>
<models>
<foo_bar>
<class>Foo_Bar_Model</class>
</foo_bar>
</models>
</global>
<frontend>
<events>
<catalog_product_load_after>
<observers>
<foo_bar>
<type>model</type>
<class>foo_bar/observer</class>
<method>catalogProductLoadAfter</method>
</foo_bar>
</observers>
</catalog_product_load_after>
</events>
</frontend>
</config>
app/code/local/Foo/Bar/Model/Observer.php
<?php
class Foo_Bar_Model_Observer{
public function catalogProductLoadAfter(Varien_Event_Observer $observer)
{
$product = $observer ->getProduct();
$product ->setName($product.getName().' '.'is cool');
}
}
?>
And i have also configured the module in app/etc/modules/Foo_Bar.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Foo_Bar>
<active>true</active>
<codePool>local</codePool>
</Foo_Bar>
</modules>
</config>
But it doenst work, does anyone have any suggestion?

You only need to module name for the model:
<foo_bar>
<type>model</type>
<class>bar/observer</class>
<method>catalogProductLoadAfter</method>
</foo_bar>
you don't need to specify Foo_Bar
also have you an error in your PHP
$product ->setName($product.getName().' '.'is cool');
should be
$product->setName($product->getName().' '.'is cool');

Related

install sql not work in table Mage_Sales_Model_Resource_Setup

My script runs perfect but dont save the new attribute in database.... why? thanks.
config.xml
<?xml version="1.0"?>
<config>
<modules>
<Itep_Incomm>
<version>0.1.0</version>
</Itep_Incomm>
</modules>
<global>
<models>
<incomm>
<class>Itep_Incomm_Model</class>
<resourceModel>incomm_mysql4</resourceModel>
</incomm>
</models>
<catalog>
<product>
<type>
<incomm_virtual translate="label" module="incomm">
<label>Incomm Virtual</label>
<model>incomm/product_virtual</model>
<price_model>incomm/product_price</price_model>
<is_qty>1</is_qty>
</incomm_virtual>
</type>
</product>
</catalog>
<resources>
<incomm_setup>
<setup>
<module>Itep_Incomm</module>
<class>Itep_Incomm_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</incomm_setup>
<incomm_write>
<connection>
<use>core_write</use>
</connection>
</incomm_write>
<incomm_read>
<connection>
<use>core_read</use>
</connection>
</incomm_read>
</resources>
</global>
</config>
Script install in folder sql/incomm_setup/mysql4-install-0.1.0.php
<?php
$installer = $this;
$installer->startSetup();
//incomm_request_active_code att order
$installer->addAttribute('order_item','incomm_request_active_code',
array(
'type' => 'text',
'grid' => false
)
);
$installer->endSetup();
And the model/resource/eav/mysql4/setup.php
<?php
class Itep_Incomm_Model_Resource_Eav_Mysql4_Setup extends Mage_Sales_Model_Resource_Setup
{
}
When i run the script, and go to search in database, i cant find the new attribute in table Mage_Sales_Model_Resource_Setup, but the script shows in core_resouce, why my code is not saving? thanks

override core controller in custom module in Magento

HI i have to extend the core controller in my own module for this i am referencing the below link
http://inchoo.net/tools-frameworks/how-to-extend-magento-core-controller/
below is my module structure
/var/www/magento1.9/app/etc/modules
<?xml version="1.0"?>
<!--we need to enable this module as any other if-->
<!--you wish to do it as standalone module extension-->
<config>
<modules>
<Inchoo_Coreextended>
<active>true</active>
<codepool>local</codepool>
</Inchoo_Coreextended>
</modules>
</config>
/var/www/magento1.9/app/code/local/Inchoo/Coreextended/controllers/Frontend/Customer/AccountController.php
<?php
require_once Mage::getModuleDir('controllers', 'Mage_Customer').DS.'AccountController.php';
//we need to add this one since Magento wont recognize it automatically
class Inchoo_Coreextended_Frontend_Customer_AccountController extends Mage_Customer_AccountController
{//here, you extended the core controller with our
public function indexAction()
{
parent::indexAction();
//you can always use default functionality
}
public function myactionAction()
{
//my code
//you can write your own methods / actions
}
public function mymethod()
{
//my code
//you can write your own methods
}
public function loginAction()
{
echo "hello";
//finally you can write your code that will rewrite the whole core method
//and you can call for your own methods, as you have full control over core controller
}
}
/var/www/magento1.9/app/code/local/Inchoo/Coreextended/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Inchoo_Coreextended>
<version>0.1.0</version>
</Inchoo_Coreextended>
</modules>
<frontend>
<routers>
<customer>
<args>
<modules>
<Inchoo_Coreextended before="Mage_Customer_AccountController">Inchoo_Coreextended_Frontend_Customer</Inchoo_Coreextended>
</modules>
</args>
</customer>
</routers>
</frontend>
</config>
but when i am accessing the http://localhost/magento1.9/index.php/customer/account/login/ it shows core login action and it is not switching from my module Can you please suggest where i am doing mistake .
I have fixed this by making changes in following file:
app/etc/modules/Inchoo_Coreextended.xml .
Before:
<config>
<modules>
<Inchoo_Coreextended>
<active>true</active>
<codepool>local</codepool>
</Inchoo_Coreextended>
</modules>
</config>
After:
<config>
<modules>
<Inchoo_Coreextended>
<active>true</active>
<codePool>local</codePool>
</Inchoo_Coreextended>
</modules>
</config>
codepool should be codePool

Override magento model files getting Fatal error: Call to undefined method

I need to edit the below file
/var/www/magento1.9/app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Urlkey.php
i dont want to edit the core file
for which i have done this so far
<?xml version="1.0"?>
<config>
<modules>
<Inchoo_Coreextended>
<version>0.1.0</version>
</Inchoo_Coreextended>
</modules>
<global>
<models>
<catalog>
<rewrite>
<product>Inchoo_Coreextended_Model_Product_Attribute_Backend_Urlkey</product>
</rewrite>
</catalog>
</models>
</global>
</config>
i have created the folowing directory from where i like to extend the file
/var/www/magento1.9/app/code/local/Inchoo/Coreextended/Model/Product/Attribute/Backend/Urlkey.php
and file is as
class Inchoo_Coreextended_Model_Product_Attribute_Backend_Urlkey extends Mage_Catalog_Model_Attribute_Backend_Urlkey_Abstract
{
}
but when I am going to catalog pages i get following error
Fatal error: Call to undefined method Inchoo_Coreextended_Model_Product_Attribute_Backend_Urlkey::setStoreId() in /var/www/magento1.9/app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php on line 66
please suggest where i am doing mistake
change your config.xml code
from
<global>
<models>
<catalog>
<rewrite>
<product>Inchoo_Coreextended_Model_Product_Attribute_Backend_Urlkey</product>
</rewrite>
</catalog>
</models>
</global>
to
<global>
<models>
<catalog>
<rewrite>
<product_attribute_backend_urlkey>Inchoo_Coreextended_Model_Product_Attribute_Backend_Urlkey</product_attribute_backend_urlkey>
</rewrite>
</catalog>
</models>
</global>

setup installer script in magento module

Hi i am developing a little magento module.
below is my directory structure of module
app/code/local/Xyz/Total
/var/www/magext/app/code/local/Xyz/Total/Block/Prototal.php
/var/www/magext/app/code/local/Xyz/Total/controllers/IndexController.php
/var/www/magext/app/code/local/Xyz/Total/etc/config.xml
/var/www/magext/app/code/local/Xyz/Total/Model/Price/Observer.php
below is my config file code
<?xml version="1.0"?>
<config>
<global>
<blocks>
<total>
<class>Xyz_Total_Block</class>
</total>
</blocks>
<models>
<xyztotal>
<class>Xyz_Total_Model</class>
</xyztotal>
</models>
<events>
<sales_order_place_after>
<observers>
<xyz_total_price_observer>
<type>singleton</type>
<class>Xyz_Total_Model_Price_Observer</class>
<method>apply_discount_percent</method>
</xyz_total_price_observer>
</observers>
</sales_order_place_after>
</events>
</global>
<frontend>
<routers>
<total>
<use>standard</use>
<args>
<module>Xyz_Total</module>
<frontName>newcatalog</frontName>
</args>
</total>
</routers>
<layout>
<updates>
<total>
<file>total.xml</file>
</total>
</updates>
</layout>
</frontend>
</config>
now what i need to install a product attribute through my module
.But i cant understand understand where should i put my installer
script and how to modify config file.
please suggest.
I saw your config.xml i am not able to find xml vesrion.
So you need to give xml version like below example:
<config>
<modules>
<Xyz_Total>
<version>0.1.0</version> <!-- Version of module -->
</Xyz_Total>
</modules>
</config>
Now create the file app/code/local/Xyz/Total/sql/total_setup/mysql4-install-0.1.0.ph‌​p
After that reinstall the module And check it.
You also have to add below code in config.xml under config tag.
<global>
<resources> <!-- These are resource setting giving access to module, read/write permission on database -->
<total_setup>
<setup>
<module>Xyz_Total</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</total_setup>
<total_write>
<connection>
<use>core_write</use>
</connection>
</total_write>
<total_read>
<connection>
<use>core_read</use>
</connection>
</total_read>
</resources>
</global>
After that reinstall module.
Take a look # Adding custom product attributes in Magento using setup script
<modules>
<Xyz_Total>
<version>0.1.0</version>
</Xyz_Total>
</modules>
<global>
....
<models>
<xyztotal>
<class>Xyz_Total_Model</class>
<resourceModel>total_setup</resourceModel>
</xyztotal>
</models>
<resources>
<catalog_setup>
<setup>
<module>Mage_Catalog</module>
<class>Mage_Catalog_Model_Resource_Setup</class><!-- that line !-->
</setup>
</catalog_setup>
</resources>
...
In
app/code/local/Xyz/Total/sql/total_setup/mysql4-install-0.1.0.ph‌​p
$installer->addAttribute('catalog_product', 'offer_type', array(
'backend' => '',
...
));

magento 1.7 add attributegroup

I need to add an attributegroup called Videos in the database using an install script.
these are my files:
app/code/local/Company/ProductVideo/etc/config.xml
<resources>
<attributegroup_setup>
<setup>
<module>Company_ProductVideo</module>
<class>Company_ProductVideo_Model_Entity_Setup</class>
</setup>
</attributegroup_setup>
</resources>
</global>
app/code/local/Company/ProductVideo/Model/Entity/Setup.php
<?php
class Company_ProductVideo_Model_Entity_Setup extends Mage_Eav_Model_Entity_Setup{
}
?>
app/code/local/Company/sql/attributegroup_setup/msql4-install-1.7.0.php
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttributeGroup('catalog_product', 'Default','Videos', 40);
$installer->endSetup();
?>
My module is also registered in app/code/etc/modules/Company_ProductVideo.xml
<config>
<modules>
<Company_ProductVideo>
<active>true</active>
<codePool>local</codePool>
</Company_ProductVideo>
</modules>
</config>
But this is not working, any help is welcome!!
I have already solved it, the problem was the name of the folder under the sql folder.
the convention is to name it like this: mysql4-install-'version'.