3 replies [Last post]
Francois's picture
Offline
Joined: 08/19/2008
Juice: 387
Was this information Helpful?

I'm trying to override the theming of the "Add to Cart" form on product nodes. I've isolated the function (uc_product_add_to_cart_form), and made a small change to test whether I've got the right one; which worked. Now I've copied the function to my template.php, renamed it to theme_uc_product_add_to_cart_form and it doesn't work?

I've emptied my cache, still no change. Am I doing something wrong?

--------
'Twas the end of the world, and you didn't even know it.

sipy's picture
Offline
Joined: 05/04/2010
Juice: 42
example_form_alter

I tried to use a bit different approach for this but basically trying to do exactly same thing.

I have own custom module and there I have implemented function called: my example _form_alter "example" being my module name.
then I have switch - case where I check the form ID and this works for most of the form, but not uc_product_add_to_cart_from as formID has nodeID added to it.

So If i use FormID uc_product_add_to_cart_from_2 where 2 is my products NodeID, it works, but that is not really generic solution Smiling

So not sure what would be best way to theme this form as standard doesn't seem to work.

sipy's picture
Offline
Joined: 05/04/2010
Juice: 42
My solution

Ok, my solution was use 'if (strpos($form_id, 'uc_product_add_to_cart_form_') === 0)' in my example _form_alter -function.

strae's picture
Offline
Joined: 02/08/2010
Juice: 13
Re: Add to Cart Theme Override

Ubercats seem to add the node id (and even a counter, in case you have many form of the same product in the same page, for example if you use a lot of views..could happen).

I dont know about theme function, but as sipy suggested i guess the simpliest way is to use a small custom module, that will look like (lets say the module is named 'foo'):

/*
* Implementation of hook_form_alter
* */
function foo_form_alter(&$form, &$form_state, $form_id){
    switch($form_id){
        case 'blog_node_form':
            /*
             * Here you can alter a Blog node form.. its just an example.
             * */
        break;
        case stristr($form_id, 'uc_product_add_to_cart_form'): // As said, the form id for this kind of form is dynamic.
            /*
             * Ubercart do some fancy stuff before this function has been invoked, there
             * is no sense to edit the form here.
             * Instead, we add a function callback to #after_build, that will be fired just before building the form.
             * (this callback is not indended to be uset outside this module, so for convention I add a
             * underscore in the begin of the name)
             * */
            $form['#after_build'][] = '_foo_uc_product_add_to_cart_form';
        break;
    }
}

function _foo_uc_product_add_to_cart_form($form, &$form_state){
    /*
     * Do whatever you want here, you ca edit/remove/add form elements...
     * just REMEMBER to return the $form, or you'll get a WSOD
     * */
    return $form;   
}