I have a Magento store installed, and when a product is duplicated in the backend, Magento sets its status to Disabled by default. I don't want that to happen, the duplicated product should have its status copied from the original product as well.
In this post a partial solution was given. I see where I can find the config.xml and make the necessarry changes. However, where do I put such an observer class? Which file should I use/create and would that require any changes to the config.xml input?
Or does somebody have an overall solution for this issue? Thanks in advance!
Try this:
Create: app/code/local/MagePal/EnableDuplicateProductStatus/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<MagePal_EnableDuplicateProductStatus>
<version>1.0.1</version>
</MagePal_EnableDuplicateProductStatus>
</modules>
<global>
<models>
<enableduplicateproductstatus>
<class>MagePal_EnableDuplicateProductStatus_Model</class>
</enableduplicateproductstatus>
</models>
<events>
<catalog_model_product_duplicate>
<observers>
<enableduplicateproductstatus>
<type>singleton</type>
<class>enableduplicateproductstatus/observer</class>
<method>productDuplicate</method>
</enableduplicateproductstatus>
</observers>
</catalog_model_product_duplicate>
</events>
</global>
</config>
Create: app/code/local/MagePal/EnableDuplicateProductStatus/Model/Observer.php
class MagePal_EnableDuplicateProductStatus_Model_Observer
{
/**
* Prepare product for duplicate action.
*
* #param Varien_Event_Observer $observer
* #return object
*/
public function productDuplicate(Varien_Event_Observer $observer)
{
$newProduct = $observer->getEvent()->getNewProduct();
$newProduct->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
return $this;
}
}
Create: app/etc/modules/MagePal_EnableDuplicateProductStatus.xml
<?xml version="1.0"?>
<config>
<modules>
<MagePal_EnableDuplicateProductStatus>
<active>true</active>
<codePool>local</codePool>
</MagePal_EnableDuplicateProductStatus>
</modules>
</config>
Then clear cache and try duplicating a product.
read more # :
http://magento4u.wordpress.com/2009/06/08/create-new-module-helloworld-in-magento/
http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method
make a new product active by default in magento
I found error on this code and find out the solution below:
On app/code/local/MagePal/EnableDuplicateProductStatus/etc/config.xml change
<method> duplicateProduct </method>
TO
<method>productDuplicate</method>
Related
I've got a Magento store (1.9) with a very simple plugin. It changes the customer group when someone places an order in a website. But we also work with a POS.
In the POS the plugin is also trying to change the customer group but it gives an error.
So what I want to do is disable the plugin for our local IP (or User).
The code is the following or check Github:
observer.php
<?php
class RvdH_GroupChange_Model_Observer
{
public function changeGroup(Varien_Event_Observer $observer)
{
$order = $observer->getEvent()->getOrder();
$customer = Mage::getModel('customer/customer')->load($order->getCustomerId());
/*$event = $observer->getEvent(); //Fetches the current event"
$customer = $event->getCustomer();
$dbcustomer = Mage::getModel('customer/customer')->load($customer[entity_id]);*/
// ensure it's not guest checkout
if ($customer->getId()) {
$customer->setGroupId(5);
$customer->save();
}
}
}
config.xml
<?xml version="1.0"?>
<config>
<modules>
<RvdH_GroupChange>
<version>0.1.0</version>
</RvdH_GroupChange>
</modules>
<global>
<models>
<RvdH_GroupChange>
<class>RvdH_GroupChange_Model</class>
<resourceModel>module_mysql4</resourceModel>
</RvdH_GroupChange>
</models>
<events>
<sales_order_place_after>
<observers>
<RvdH_GroupChange>
<class>RvdH_GroupChange_Model_Observer</class>
<method>changeGroup</method>
</RvdH_GroupChange>
</observers>
</sales_order_place_after>
</events>
</global>
</config>
Solved.
I just had to change the <global> tag to <frontend> in the config.xml.
Now it only works in frontend and not global.
in product detail page, the product price is 50 dollar, I use JavaScript change the price to 80 dollar, but when add to cart, it is still 50 dollar in checkout page. how to let it still 80 dollar in checkout page?
You need use "sales_quote_add_item" magento event to update the product price in cart session.
You have to make a custom module for that purpose.
Create a file in app/etc/modules/Company_All.xml
<?xml version="1.0"?> <config> <modules>
<Company_Product>
<codePool>local</codePool>
<active>true</active>
</Company_Product> </modules> </config>
Create configuration file for our module file in app/code/local/Company/Product/etc/config.xml
<?xml version="1.0"?> <config> <global>
<models>
<product>
<class>Company_Product_Model</class>
</product>
</models>
<events>
<sales_quote_add_item><!--Event to override price after adding product to cart-->
<observers>
<company_product_price_observer><!--Any unique identifier name -->
<type>singleton</type>
<class>Company_Product_Model_Price_Observer</class><!--Our observer class name-->
<method>update_book_price</method><!--Method to be called from our observer class-->
</company_product_price_observer>
</observers>
</sales_quote_add_item>
</events> </global> </config>
Create our observer file in app/code/local/Company/Product/Model/Price/Observer.php
class Company_Product_Model_Price_Observer{
public function update_book_price(Varien_Event_Observer $observer) {
$quote_item = $observer->getQuoteItem();
//if(){ //your logic goes here
$customprice = 50;
//}
$quote_item->setOriginalCustomPrice($customprice);
$quote_item->save();
return $this;
}
}
G'Day all,
I'm trying to write my first magento event handler to capture "checkout_cart_save_after" but I can't get my Observer.php code to fire, after 2 days of looking at web sites on magento events I understand the whole process but the code is not running.. I'm perplexed as to why.
Here is the modules xml file in /var/magento/app/etc/modules/
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Yourmodule>
<codePool>local</codePool>
<active>true</active>
<version>1.0.0</version>
</Namespace_Yourmodule>
</modules>
</config>
As you can see I'm just using Namespace and Yourmodule as I'm writing a tutorial for others....
In my module's etc directory (/var/magento/app/code/local/Namespace/Yourmodule/Module) I have config.xml
<?xml version="1.0"?>
<global>
<events>
<checkout_cart_save_after>
<observers>
<yourmodule_after_observer>
<type>singleton</type>
<class>Namespace_Yourmodule_Model_Observer</class>
<method>checkout_cart_save_after</method>
</yourmodule_after_observer>
</observers>
</checkout_cart_save_after>
</events>
</global>
and in the Modules directory I have my Observer.php file:
class Namespace_Yourmodule_Model_Observer
{
/*----------------------------------------
*
* LogInfo()
*
* Basic logging of activity to disk for debugging
*/
public function LogInfo($msg)
{
$logfile="/logs/Namespace-module.log";
$fd=fopen($logfile,"a");
if($fd)
{
fwrite($fd,$msg."\n");
fclose($fd);
}
}
public function checkout_cart_save_before($observer)
{
LogInfo("save_before called");
}
public function checkout_cart_save_after($observer)
{
LogInfo("save_after called");
}
}
The result of trying to add and remove items from the cart is that no log file is crearted and when manually created, no data is written, system.log shows no errors, if I skew the XML it reports the errror, so the XML file is being read.
Any thoughts on what I have missed???
Sid
Update 12/2:
I have spent the weekend on this and solved the issue, thanks for the hints, it was a combination of minor things that I should have picked up and the format of the XML file now matches what has been suggested above... there is now a tutorial! I have fully documented the process with working code and config files.
See http://z900collector.wordpress.com/magento/magento-events/
Try updating your config.xml to
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Yourmodule>
<version>1.0</version>
</Namespace_Yourmodule>
</modules>
<global>
<!--helpers>
<yourmodule>
<class>Namespace_Yourmodule_Helper</class>
</yourmodule>
</helpers-->
<models>
<yourmodule>
<class>Namespace_Yourmodule_Model</class>
</yourmodule>
</models>
<events>
<checkout_cart_save_after>
<observers>
<yourmodule_after_observer>
<type>singleton</type>
<class>Namespace_Yourmodule_Model_Observer</class>
<method>checkout_cart_save_after</method>
</yourmodule_after_observer>
</observers>
</checkout_cart_save_after>
</events>
</global>
</config>
See Customize Magento using Event/Observer
I need to limit the number of products added to compare in Magento. Only wanted to have a maximum of 4 products to be compared.
I'm thinking of doing it in the .phtml (where item display looping is) but have no idea where I should edit to display the message "Compare list is full". Any idea?
Thanks!
I've hooked to the catalog_product_compare_add_product event.
Here is my solution:
Create module.
Directories:
app/code/local/Company //this can be any name
app/code/local/Company/Catalog
app/code/local/Company/Catalog/Helper
app/code/local/Company/Catalog/etc
Module config
Create a file: app/code/local/Company/Catalog/etc/config.xml
File contents:
<?xml version="1.0"?>
<config>
<modules>
<Company_Catalog>
<version>0.1</version>
</Company_Catalog>
</modules>
<frontend>
<events>
<catalog_product_compare_add_product>
<observers>
<company_catalog>
<type>singleton</type>
<class>Company_Catalog_Helper_Observer</class>
<method>limitProductCompare</method>
</company_catalog>
</observers>
</catalog_product_compare_add_product>
</events>
</frontend>
</config>
Helper
Create a file: app/code/local/Company/Catalog/Helper/Observer.php
File contents:
<?php
class Company_Catalog_Helper_Observer extends Mage_Catalog_Helper_Data {
const COMPARE_LIMIT = 4;
function limitProductCompare($event) {
if (Mage::helper('catalog/product_compare')->getItemCount()<self::COMPARE_LIMIT) return;
$session = Mage::getSingleton('catalog/session');
Mage::getSingleton('catalog/product_compare_list')->removeProduct($event->getProduct());
$session->getMessages()->clear();
$session->addNotice($this->__('You have reached the limit of products to compare. Remove one and try again.'));
}
}
Enable module
Create file: app/etc/modules/Company_Catalog.xml
File contents:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Company_Catalog>
<active>true</active>
<codePool>local</codePool>
</Company_Catalog>
</modules>
</config>
Profit!
Everything should work fine now. After adding, 5th product gets removed, and nice notice is displayed. Its not the perfect solution (since it removes a product after it was added), but it does the job well.
compare items are added in Mage_Catalog_Product_CompareController and you can see that there are events dispatched that you can hook your observer to or you can add your limits by extending Mage_Catalog_Model_Product_Compare_List and overriding addProduct() or addProducts() methods or even make this in collection classes
I am trying to add some data to a session variable when a product is added to the shopping cart...
I tried copying the "CartController.php" under "app\code\core\Mage\Checkout\controllers" to
"local\Mage\Checkout\controllers" but its not actually overriding the controller..
Could anyone give me some hint on this..?
Thanks a lot.
You can create an "Event Oberserver" to trigger an event.
Create the following folders :
app/code/local/[myMagento]/EventTest/etc
app/code/local/[myMagento]/EventTest/Model
create a file under /EventTest/Model directory like myMagento_EventTest.xml and put the following :
<?xml version="1.0"?>
<config>
<modules>
<myMagento_EventTest>
<active>true</active>
<codePool>local</codePool>
</myMagento_EventTest>
</modules>
</config>
create config.xml file under /EventTest/etc folder nad put the following :
<?xml version="1.0"?>
<config>
<modules>
<myMagento_EventTest>
<version>0.1.0</version>
</myMagento_EventTest>
</modules>
<frontend>
<events>
<checkout_cart_product_add_after>
<observers>
<myMagento_EventTest_Model_Observer>
<type>singleton</type>
<class>myMagento_EventTest_Model_Observer</class>
<method>MyTestMethod</method>
</myMagento_EventTest_Model_Observer>
</observers>
</checkout_cart_product_add_after>
</events>
</frontend>
</config>
create model class file observer.php under the /EventTest/Model directory and put following
<?php
class myMagento_EventTest_Model_Observer {
public function MyTestMethod($observer) {
$event = $observer->getEvent(); //Fetches the current event
$product = $event->getProduct();
$eventmsg = "Current Event Triggered : " . $event->getName() . "
Currently Added Product : " . $product->getName();
//Adds Custom message to shopping cart
echo Mage::getSingleton("checkout/session")->addSuccess($eventmsg);
//Your Custom Logic Here
//you can use print_r($product) here to get more details
}
}
I found this , this enables us to override the CartController.php addAction() funciton... This is exaclty i needed.
Hope this would help someone in need.
Balan