Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

dispatchEvent around cart checkout. 
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

I’m finding myself adding lots of little statements into the Checkout CartController.  I haven’t yet been able to figure out how to override a controller, so it would be nice to have dispatchEvents before and after adding the product to the cart.

Mage::dispatchEvent('checkout_cart_pre_add', array('product'=>$product'request'=>$this->getRequest(),'qty'=>$qty));

            
$cart->addProduct($product$qty);
            
$cart->addProductsByIds($additionalIds);

        
Mage::dispatchEvent('checkout_cart_post_add', array('product'=>$product'request'=>$this->getRequest(),'qty'=>$qty));

            
$cart->save();
            
$this->_backToCart();

I think this would add a lot of flexibility for people making their own customizations to Magento w/o having to override any controllers.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Done, just to keep the naming conventions, changed event names:

$eventArgs = array(
                
'product' => $product,
                
'qty' => $qty,
                
'additional_ids' => $additionalIds,
                
'request' => $this->getRequest(),
            );

            
Mage::dispatchEvent('checkout_cart_add_before'$eventArgs);

            
$cart->addProduct($product$qty)
                ->
addProductsByIds($additionalIds);

            
Mage::dispatchEvent('checkout_cart_add_after'$eventArgs);

            
$cart->save();
What are you adding in there?
 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 
Moshe - 31 December 2007 09:20 AM

Done, just to keep the naming conventions, changed event names:

Great, thanks.

Moshe - 31 December 2007 09:20 AM

What are you adding in there?

User submitted sizes…

I have been seeing a great need from people to modify the products somehow, adding every single thing you want to sell as a simple product doesn’t seem to work for some cases, especially manufacturers who make their own stuff.  We can make any size of most of our products.  One filter in particular, is available from 6 to 74 inches wide, and 6 to 48 inches tall, at any 1/8th inch interval.  This is about 182,000 combinations, and that doesn’t include the ability to add “Velcro (r)” pieces to the front, back, or any 4 sides of the filters, that’s 182,000 * 6 * 5 * 4 * 3 * 2 *1, right? 131 million combinations for just 1 product?

So, what I’ve been doing is using some product attributes just as place holders.  There’s an attribute called “line_item_details”, that allows user input to be shoved into it, then passed along to the order.  The only problems I had were the fact that the sales_quote auto-loads the products in it by product ID and overwrites the customized attributes with fresh blank ones from the DB, and having to change the way the cart compares two products, right now it’s only an if the product id’s match.

What I want to do with the events is make some methods that can read the input from the request and then format/shove the data into the product just before it goes into the cart.  This is more desirable than simply modifying the cart with its own attribute, or just saving a customer session variable since it has prefect affinity with the product in question and requires very little code changes.  Right now, those small code changes to right into Checkout/controllers/CartController.php, but, I feel that it is a perfect candidate for events, since we’re not actually *changing* the behavior of that controller.

What I would like to see as well is some sort of $salesQuote.isSameAs($product);, so I can override it with my own product/cart logic.  getItemByProduct decides if the $product that I’m currently adding is the same as anything already in my cart.  But once you start modifying products as they get into the cart, it doesn’t work they way you’d like it to.  I know that this is completely not the way that MC is designed to be used, but I think that a lot of people who feel the need to customize MC want to go in this sort of direction.  I think modifying product attributes is the way that makes the most sense immediately.

 
Magento Community Magento Community
Magento Community
Magento Community
 
majacore
Member
 
Avatar
Total Posts:  53
Joined:  2008-12-29
 
Moshe - 31 December 2007 09:20 AM

Done, just to keep the naming conventions, changed event names:

$eventArgs = array(
                
'product' => $product,
                
'qty' => $qty,
                
'additional_ids' => $additionalIds,
                
'request' => $this->getRequest(),
            );

            
Mage::dispatchEvent('checkout_cart_add_before'$eventArgs);

            
$cart->addProduct($product$qty)
                ->
addProductsByIds($additionalIds);

            
Mage::dispatchEvent('checkout_cart_add_after'$eventArgs);

            
$cart->save();
What are you adding in there?

thank you so much for this, really helped me out!

But how would one go about also adding custom options into the cart ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
borje
Member
 
Total Posts:  40
Joined:  2010-10-21
 

I know this is a really old thread, but I’m facing the same problems right now that Mark_Kimsal faced with consumer edited products in the cart (with countless combinations). He never got an answer what the preferred way of achieving what he wanted, nor an answer to his suggestion to have a “$salesQuote->isSameAs($product);” So I’m asking anyone out there that might be able to help me along to gain a bit of clarity.

EDIT:
This is a solution I came up with. Don’t know if it’s the correct way of doing things, but it seems to work as far as I can tell. I made my own CartController extending the core CartController. Xml for extending the controller not included in code example.

<?php
require_once Mage::getModuleDir('controllers''Mage_Checkout').DS.'CartController.php';

class 
MyCompany_Checkout_CartController extends Mage_Checkout_CartController
{
    
protected $_product;
    
    protected function 
_initProduct()
    
{
        
if(!isset($this->_product))
        
{
            $this
->_product parent::_initProduct();

            
$requestArray $this->getRequest()->getParams();
            
$this->_product->addCustomOption('myoption'$requestArray['myoption']);
        
}
        
        
return $this->_product;
    
}
}

This also means you have to make your own addtocart.phtml where you add the option as a hidden input field:

<?php $_product $this->getProduct() ?>

<?php 
if($_product->isSaleable()): ?>
<form action="/checkout/cart/add/" method="post">
    <
input type="hidden" name="product" value="<?= $_product->getId(); ?>" />
    <
input type="hidden" name="myoption" value="option_value" />
    ...
</
form>
<?php endif; ?>

I hope this is helpful for somone.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top