Create the checkout_cart_product_add_before observer in Magento
source link: https://markshust.com/2012/08/27/create-checkout_cart_product_add_before-observer-magento/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Create the checkout_cart_product_add_before observer in Magento
August 27, 2012 · 1 min read · Edit on GitHub
For some reason no one can explain, it appears that the event observer checkout_cart_product_add_before
never existed in Magento (even though checkout_cart_product_add_after
does). Say what?!??
Anyways, it’s fairly easy to go ahead and dispatch this event. Just add this to your module’s config.xml to rewrite the checkout/cart model:
<?xml version="1.0">
<config>
...
<global>
...
<models>
<checkout>
<rewrite>
<cart>Foo_Bar_Model_Checkout_Cart</cart>
</rewrite>
</checkout>
</models>
...
</global>
...
</config>
Then create your model that adds in the dispatch event to trigger the observers:
<?php
class Foo_Bar_Model_Checkout_Cart extends Mage_Checkout_Model_Cart
{
/**
* Create checkout_cart_product_add_before event observer
*
* @param int|Mage_Catalog_Model_Product $productInfo
* @param mixed $requestInfo
* @return Mage_Checkout_Model_Cart
*/
public function addProduct($productInfo, $requestInfo=null)
{
$product = $this->_getProduct($productInfo);
Mage::dispatchEvent('checkout_cart_product_add_before', array('product' => $product));
return parent::addProduct($productInfo, $requestInfo=null);
}
}
Now you can have fun triggering checkout_cart_product_add_before
event observers! Enjoy!
Are you a Magento geek?
Signup for my newsletter and I'll let you know about Magento-related blogs, courses & more.
Recommend
-
134
Clean Checkout for Magento 2 A drop-in replacement for the Magento 2 checkout. Features Modules The project is divided into several modules: Overall checkout improvements, such as; (
-
75
We are going to build a basic Hello World module Magento 2. It's step by step guide to learn Magento 2 development.
-
64
GitHub is where people build software. More than 28 million people use GitHub to discover, fork, and contribute to over 85 million projects.
-
24
5 Steps: Create a Size & Position Observer in React Have you ever thought about why we have to inspect every time we want to know the width or height of the component? For me, it is rea...
-
3
Structure before content == cart before the horse Some people seem to think they can see the future perfectly. I've always been bothered by people and projects which build up a lot of structure and places to fit things befo...
-
22
How To Create a Shopping Cart UI Using HTML & CSS?Step by step tutorialThe shopping cart page is designed to allow buyers to see all the products they have added to the shopping cart. It c...
-
4
News » Partner Content » Do you have an abandoned cart problem? Here’s how to improve your online check...
-
10
Create a product attribute data patch with Magento 2.3's declarative schemaFebruary 19, 2019 · 5 min read ·
-
3
Create Git patches for third-party Magento modulesAugust 30, 2019 · 7 min read · ...
-
3
Shopping React-Native Product Cart Management System May 17, 2023 1 min read
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK