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

Overriding controller - still doesn’t work
 
Rafal
Member
 
Total Posts:  42
Joined:  2007-11-22
Munich
 

Hello,

I try to override CartController.
I found information about it on forum ... so I made following:

1. In my module’s config.xml (in global tag):

<rewrite>
  <
mycartrewrite>
    <
from><![CDATA[#^/checkout/cart/add$#]]></from>
    
<to>/mymodule/cart</to>
  </
mycartrewrite>
</
rewrite>

<config>
...
  <
frontend>
    <
routers>
      <
mymodule>
        <use>
standard</use>
        <
args>
          <
module>Mage_MyModule</module>
          <
frontName>mymodule</frontName>
        </
args>
      </
mymodule>
    </
routers>
  </
frontend>

2.

insert into core_url_rewrite (store_idid_pathrequest_pathtarget_path
values (0'mycartrewrite/13''checkout/cart''mymodule/cart');

3. I placed in Mage_MyModule_CartController:

require_once 'Mage/Checkout/controllers/CartController.php';

4. And of course I creted in MyModule/controllers/CartController.php:

class Mage_MyModule_CartController extends Mage_Checkout_CartController
{
  
public function addAction()
  
{
    
// my code
  
}
}

When I press on Product view page: Add to cart then I receive on browser screen:
Fatal error: Call to a member function setContinueShoppingUrl() on a non-object in /var/www/app/code/core/Mage/Checkout/controllers/CartController.php on line 77

Line 77 it’s:
$this->getLayout()->getBlock(’checkout.cart’)->setContinueShoppingUrl($continueShoppingUrl);

But when I add in checkout.xml:

<mymodule_cart_index>
  <
update handle="checkout_cart_index"/>
</
mymodule_cart_index>

then I don’t get any error but it doesn’t call my controller.

Can anyone give me some hint ??

Magento version: 07.15480
PHP: PHP Version 5.2.0-8+etch7
Apache: Apache/2.2.3
Mysql: 5.0.32

with best regards,
Rafal

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

Please update Mage_Checkout_CartController::indexAction :

public function indexAction()
    
{
        $cart 
$this->_getCart();
        
$cart->init();
        
$cart->save();

        
$this->loadLayout();
        
$this->_initLayoutMessages('checkout/session');

        
$this->renderLayout();
    
}
Add method Mage_Checkout_Block_Cart::getContinueShoppingUrl
public function getContinueShoppingUrl()
    
{
        $continueShoppingUrl 
Mage::getSingleton('checkout/session')->getContinueShoppingUrl(true);
        if (!
$continueShoppingUrl{
            $continueShoppingUrl 
Mage::getUrl();
        
}
        
return $continueShoppingUrl;
    
}
I’m not sure what were the customizations, you have probably changed layout XML files too and checkout.cart block doesn’t exist anymore, but the above implementation is better and will be included in next revision.
 
Magento Community Magento Community
Magento Community
Magento Community
 
Rafal
Member
 
Total Posts:  42
Joined:  2007-11-22
Munich
 

When I make it then after pressing ‘Add to Cart’ I don’t see Cart i.e. I see template but without content.
Unfortunately it still doesn’t call my controller :-(

I want only to override addAction - it has to make additional processing for me.

I didn’t make any changes in layout. Checkout.cart block still exists.

with best regards,
Rafal

 
Magento Community Magento Community
Magento Community
Magento Community
 
diri
Member
 
Total Posts:  48
Joined:  2008-01-13
 
Moshe - 05 February 2008 06:23 AM

Please update Mage_Checkout_CartController::indexAction :
... but the above implementation is better and will be included in next revision.

Don’t get me wrong, please, but:

Is there any trunck available to watch what will happen?

I’m interested in developing add-ons and bridges. I start with version x.y.a. So far, so good.

In x.y.b logic changes, functions change and more of this kind. This makes it impossible to work with Magento - even development of a “trivial” import feature for several thousand products is impossible. Not to mention different prices (step) and BOM.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Rafal
Member
 
Total Posts:  42
Joined:  2007-11-22
Munich
 

I’m interested in developing add-ons and bridges. I start with version x.y.a. So far, so good.

In x.y.b logic changes, functions change and more of this kind. This makes it impossible to work with Magento - even development of a “trivial” import feature for several thousand products is impossible. Not to mention different prices (step) and BOM.

They warned that as it’s beta they make a lot of changes. When it will be 1.0 then we will get “stable” api and functionality.

with best regards,
Rafal

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

@Rafal: is your customization something that could go into checkout_cart_before_add or checkout_cart_after_add events? (look in addAction method)

If yes, in your module add to configuration:

<config>
..
  <
frontend>
...
    <
events>
      <
checkout_cart_after_add>
        <
observers>
          <
my_custom_observer>
            <
type>model</type>
            <class>
mymodule/observer</class>
            <
method>cartAfterAdd</method>
          </
my_custom_observer>
        </
observers>
      </
checkout_cart_after_add>
    </
events>
  </
frontend>
</
config>

Then you can create Mage_MyModule_Model_Observer:

<?php

class Mage_MyModule_Model_Observer
{
  
function addCartAfter($observer)
  
{
    $event 
$observer->getEvent();
// now you can reference any arguments that were supplied during event dispatch
// look in $eventArgs in addAction
   
$product $event->getProduct();
   
$qty $event->getQty();
   
$request $event->getRequest();
// .. your custom processing here
  
}
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
Rafal
Member
 
Total Posts:  42
Joined:  2007-11-22
Munich
 

When I add what you wrote me i.e. add observer to config.xml of my module and create Mage_MyModule_Model_Observer then I see that addCartAfter is called.
But ... my addAction isn’t :-(

My Mage_MyModule_CartController:

<?php

require_once 'Mage/Checkout/controllers/CartController.php'

class 
Mage_MyModule_CartController extends Mage_Checkout_CartController
{
  
public function addAction()
  
{
    Mage
::log"My controller was called");
  
}
}

I know that Mage::log is “deactivated” but I made it working.

When I add in checkout.xml:

<mymodule_cart_index>
  <
update handle="checkout_cart_index"/>
</
mymodule_cart_index>

and I press Add to Cart then I see Shopping cart and product is added in the basket.
If I remove these 3 lines from checkout.xml then after pressing ‘Add to Cart’ the product lands in the cart and I see an empty page i.e. template 2columns-right without content.
But if my controller would be called then the product wouldn’t land in the cart.

I have to make custom processing in addAction because I’m changing attributes of a product before it lands in the cart.
I have configurable product but user doesn’t select property on the website but the property should be selected in addAction. I could make it addCartAfter but it wouldn’t be nice solution. I hope that magento won’t give some error if a configurable product (without selected property) will be added into basket.

with best regards,
Rafal

 
Magento Community Magento Community
Magento Community
Magento Community
 
diri
Member
 
Total Posts:  48
Joined:  2008-01-13
 
Rafal - 06 February 2008 02:47 AM

They warned that as it’s beta they make a lot of changes. When it will be 1.0 then we will get “stable” api and functionality.

I’m aware of this. OTOH I watch multiple real opensource projects. Advantage of opensource is chance to not to see ready product only, one can watch development as well. This can speed up and improve basic product as well as add-ons. Add-ons are needed very often because nobody can see any need of every possible user in our global environment.

For me hidden development is no recommendation in relation to opensource.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Rafal
Member
 
Total Posts:  42
Joined:  2007-11-22
Munich
 

I’m aware of this. OTOH I watch multiple real opensource projects. Advantage of opensource is chance to not to see ready product only, one can watch development as well. This can speed up and improve basic product as well as add-ons. Add-ons are needed very often because nobody can see any need of every possible user in our global environment.

For me hidden development is no recommendation in relation to opensource.

But it’s not hidden development !
You have access to SVN and source of releases. You can write own modules if you want.
For example I’m extending magento now (by creating own module). I have worked already with 4 releases of magento - and it was always compatible - I haven’t have to adapt anything for next release so far.

with best regards,
Rafal

 
Magento Community Magento Community
Magento Community
Magento Community
 
diri
Member
 
Total Posts:  48
Joined:  2008-01-13
 
Rafal - 06 February 2008 11:57 PM



For me hidden development is no recommendation in relation to opensource.

But it’s not hidden development !
You have access to SVN and source of releases. You can write own modules if you want.

Let’s sort out one issue first:
Being able to download source code of last published version makes SVN access a fake when there is no access to more recent files (read: development branch, trunck, whatever you want to call it).

Related to modules:
I have been hit by logic changes with last beta published and stopped further work at this project at very same moment.

It’s like working with closed source products in relation of publishing recent code to have an idea of what happens while being a supporting programmer who wants to have a GA product as well when Magento reaches GA or very short time later.

 
Magento Community Magento Community
Magento Community
Magento Community
 
zburnham
Member
 
Avatar
Total Posts:  69
Joined:  2007-11-07
Westborough, MA
 
Moshe - 06 February 2008 04:50 AM

@Rafal: is your customization something that could go into checkout_cart_before_add or checkout_cart_after_add events? (look in addAction method)

If yes, in your module add to configuration:

<config>
..
  <
frontend>
...
    <
events>
      <
checkout_cart_after_add>
        <
observers>
          <
my_custom_observer>
            <
type>model</type>
            <class>
mymodule/observer</class>
            <
method>cartAfterAdd</method>
          </
my_custom_observer>
        </
observers>
      </
checkout_cart_after_add>
    </
events>
  </
frontend>
</
config>

Then you can create Mage_MyModule_Model_Observer:
<?php

class Mage_MyModule_Model_Observer
{
  
function addCartAfter($observer)
  
{
    $event 
$observer->getEvent();
// now you can reference any arguments that were supplied during event dispatch
// look in $eventArgs in addAction
   
$product $event->getProduct();
   
$qty $event->getQty();
   
$request $event->getRequest();
// .. your custom processing here
  
}
}

Please correct me if I’m wrong, but shouldn’t it look like this:

<my_custom_observer>
  <
type>model</type>
  <class>
mymodule/observer</class>
  <
method>addCartAfter</method>
</
my_custom_observer>

Am I missing something?  (I hope not, I’m just beginning to grok this stuff smile

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