tags:

views:

6208

answers:

5

Magento shopping cart is built on the Zend Framework in PHP. This is the first time I've dealt with the Zend framework and I'm having the following difficulty...

I'm creating a custom module that will allow users to upload images whenever they purchase products.

I can overload the addAction() method whenever a user attempts to add a product to their cart. I can also create a custom module which presents the form to the user and accepts the file(s). However I'm not sure how to insert the code to run my module into my overloaded method:

<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class Company_SpecialCheckout_Checkout_CartController extends Mage_Checkout_CartController
{
    # Overloaded addAction
    public function addAction()
    {
        # when user tries to add to cart, request images from them
        # *********
        # *** what do i do in here to display a custom block ???? ###
        # *** and allow addAction to continue only if successfully validated form input ###
        # *********

        parent::addAction();
    }
}

I suspect my difficulties come from my lack of knowledge of the Zend MVC way of doing things. I've studied all the Magento documentation/wikis/forum threads from top to bottom.

A: 

I must admit upfront that I don't have production experience of Magento, but I have spent some time poking around their code.

The block structure is defined in XML, and so you may not need to actually extend the Cart Controller.

The Layout XML files can be found (on a default install) at app/design/frontend/default/default/layout. In here you will find checkout.xml which sets up the block structure for the checkout page.

Simon
I looked at that, but I need to implement a bit of logic before the product actually gets added to the cart (need some uploaded files from the user, plus some other DB fields). If the user fails to provide this data I don't want them to be able to checkout.
rwired
In that case, would it be possible to make use of the event system?The CartController fires a few events - checkout_cart_before_add would be of most use and it fires just before the product is added to the cart - giving you a chance to throw an error?
Simon
I thought of that too. I quite like the idea, but conceptually it's not very different from the overload method. The difficulty I'm having is I don't know the correct method calls to make my own module display *anything at all*. What is the "right" way to invoke my module where I put the comments?
rwired
Have you had a look at this - http://activecodeline.com/2008/10/12/writing-a-custom-module-in-magento-detailed-walktrough/ ?
Simon
That looks to be useful. I think he meant to write View.php for the file in app/code/local/ActiveCodeline/Example/Block/. This gives me some more ideas. Thanks.
rwired
+1  A: 

I thought I'd move to a new answer as I think I've managed to get it working.

Here's what I did

created the following files;

app/code/local/Company/SpecialCheckout/controllers/Checkout/CartController.php

app/code/local/Company/SpecialCheckout/etc/config.xml

app/etc/modules/Company_SpecialCheckout.xml

First the controller, which is exactly as you had;

    <?PHP
require_once 'Mage/Checkout/controllers/CartController.php';
class Company_SpecialCheckout_Checkout_CartController extends Mage_Checkout_CartController {

    public function indexAction()
    {
        die('test');
    }
}

Then the module configuration

<?xml version="1.0"?>
<config>
    <modules>
        <Company_SpecialCheckout>
            <version>0.1.0</version>
        </Company_SpecialCheckout>
    </modules>
    <global>
     <rewrite>
      <Company_SpecialCheckout_Checkout_Cart>
                <from><![CDATA[#^/checkout/cart#]]></from>
       <to>/SpecialCheckout/checkout_cart</to>
            </Company_SpecialCheckout_Checkout_Cart>
        </rewrite>
    </global>
    <frontend>
        <routers>
            <Company_SpecialCheckout>
                <use>standard</use>
                <args>
                    <module>Company_SpecialCheckout</module>
                    <frontName>SpecialCheckout</frontName>
                </args>
            </Company_SpecialCheckout>
        </routers>
    </frontend>
</config>

and then finally the config file in app/etc/modules to make sure the module is picked up.

<?xml version="1.0"?>
<config>
     <modules>
        <Company_SpecialCheckout>
            <active>true</active>
            <codePool>local</codePool>
        </Company_SpecialCheckout>
     </modules>
</config>

then when you go /checkout/cart you should see 'test'. This is based on details I found here.

Make sure you have the cacheing of config files disabled in the Magento admin.

Simon
Thanks Simon, appreciate the help. Unfortunately that Wiki was also my starting point, and I got just as far. I also have my custom module all working standalone, just trying to figure out how to link the two together!
rwired
Where you have die('test'); I have $this->_redirect('specialcheckout', array('_secure'=>true)); Problem is, I can't figure out the logic to get me back to the inherited parent::addAction(); if I get my extra fields filled out.
rwired
Ah right, yeah, sorry, I've got the wrong end of the stick over which bit you're having problems with.Is there no way you can move the logic in your custom module into the overloaded checkout, removing the need for an additional module?
Simon
Alternatively what about a session flag? so the redirect only takes place if the session flag hasn't been set, once your module has been satisfied the flag is set and get sent back to the checkout?
Simon
That's a great idea. I'm going to look into that now!
rwired
+1  A: 

hey this option is given in newer version of magento 1.3.1 to upload the file from frontend enjoy

A: 

It was beeing a nightmare for me, I created a Tutorial in my blog:

CONTROLLER / OVERRIDE / Frontend [...] #^/customer/account/# /mycustomer/account/ [...]

Check this out! How to magento declare and override controllers

Ignacio Pascual
A: 

For those who stuck on this i wrote the simplest way to solve this problem without overloading controllers. My variant based on onepage checkout take a look in magento wiki

Jeje