Ubercart - how to alter button texts via hook_form_alter()? I am attempting to change the language of the buttons on the product and cart forms.

All is working well on product form but when the same is implemented on the cart page it stops submitting properly. Button values change but they stop working.

&lt;?php

/**
**Custom Module for hook_form_alter
*/

function MYMODULE_form_alter(&amp;$form, &amp;$form_state, $form_id){

    if ($form_id == uc_product_add_to_cart_form_1){
            $form['submit']['#value'] = 'Register';     
    }

    if ($form_id == uc_cart_view_form){
        //dsm($form);   
        $form['continue_shopping']['#value'] = 'Back';
        $form['update']['#value'] = 'Update';
        $form['checkout']['#value'] = 'Continue';           
    }   
}
?&gt;


Any help would be much appreciated.
There is a thread about this problem on drupal.org, and there is even a patch to solve this problem:
http://drupal.org/node/1170364

A quick explanation why the button text altering breaks up normal behavior (which is a bit "funny"):
Let's take a look at uc_cart_view_form_submit()'s code, mainly the part below the comment "// Specify the appropriate redirect based on the button used to submit". You can see that there's a switch block where the text of the clicked button is inspected ($form_state['clicked_button']['#value']), which is a very bad practice: the logic depends on what text this button is set to display. This means if you simply change the text of the buttons programatically, the appropriate redirections won't work anymore! To be honest, I don't really understand why this part of Ubercart was developed this way.

I just solved it with a custom module without patching the Ubercart core, and it works after some tests, you can find the code below.  

About changing the "Continue shopping" text:


the "Continue shopping"  text can be easily changed on the /admin/store/settings/cart/edit/basic page, and as it's a multilingual variable, THIS is the appropriate way of changing it - you can do this by typing another name in the "Custom continue shopping link text:" textfield, and saving the changes. You can see a screenshot of it here: 
BUT I also show you how to change it programatically (you can see it below in the code - you have to set the $set_continue_shopping_type_programatically to TRUE to try it - but I would suggest using the first method)
I also have to mention that the "Continue shopping" is a link (not a button) in Ubercart 6.x-2.7 by default - so if you would like to change its value programatically, and it is set to be displayed as a "Text link", its value has to be a link markup (not a regular string, because this way it won't work as expected) - you can use Drupal's l() function to create an appropriate link. But you can also set it to be displayed as a button, this way you have to use a plain text to modify its original text (and it turned out from your comment that you've already set it to be displayed as a button). 


Here is the tested and working code:

// ...

// you should change MYMODULE to your module's name

/**
 * Implementation of hook_form_alter()
 * @see http://api.drupal.org/api/drupal/developer%21hooks%21core.php/function/hook_form_alter/6
 */
function MYMODULE_form_alter(&amp;$form, &amp;$form_state, $form_id)
{

    // for testing, you can output the appropriate $form_id variable
    // to detect the form id(s) of the actual form(s) printed to the page
    // e.g. by using Devel module: http://drupal.org/project/devel
    /*
    if(function_exists('dsm'){
        dsm('Actual $form_id:');
        dsm($form_id);  
    }
    */

    // You can just simply toggle overriding the default cart button values 
    // with setting the variable below to "TRUE" or "FALSE" (without the quotes)
    // (this can be good for testing!)
    $modify_default_ubercart_buttons = TRUE;

    switch ($form_id) {
        // CHANGE IT TO THE APPROPRIATE $form_id!!!
        case 'uc_product_add_to_cart_form_1':
            // dsm($form);           
            if($modify_default_ubercart_buttons){
                $form['submit']['#value'] = 'Register';
            }
            break;

        case 'uc_cart_view_form':
            // dsm($form);

            if ($modify_default_ubercart_buttons) {
                $form['#submit'][] = 'testModule_form_alter_uc_cart_view_form_submit';

                // 1.) changing the "Update cart" button's text

                $form['update']['#value'] = 'Update';
                // if you modify the "#name" attribute, you also have to modify it in testModule_form_alter_uc_cart_view_form_submit()
                $form['update']['#name']  = 'update-cart';

                // 2.) changing the "Checkout" button's text

                $form['checkout']['#value'] = 'Continue';
                // if you modify the "#name" attribute, you also have to modify it in testModule_form_alter_uc_cart_view_form_submit()
                $form['checkout']['#name']  = 'cart-checkout';

                // IMPORTANT!!!
                // You should rather set the "Continue shopping element display" on the "/admin/store/settings/cart/edit/basic" page!
                // This is a multilingual variable, so setting it on the appropriate admin page is the best solution.
                // The code below is just a sample for setting it programatically!
                $set_continue_shopping_type_programatically = FALSE;

                if ($set_continue_shopping_type_programatically) {
                    $uc_continue_shopping_type = variable_get(uc_continue_shopping_type, 'link');

                    $continue_shopping_link_text = 'Back';
                    // $continue_shopping_link_path   = uc_cart_continue_shopping_url();
                    if ($uc_continue_shopping_type == 'link') {
                        $continue_shopping_link_markup       = l($continue_shopping_link_text, $continue_shopping_link_path);
                        $form['continue_shopping']['#value'] = $continue_shopping_link_markup;
                    } else {
                        $form['continue_shopping']['#value'] = $continue_shopping_link_text;
                        // the default "#name" attribute equals to "op" by default
                        // we change it to the desired button text to make it clear
                        $form['continue_shopping']['#name']  = $continue_shopping_link_text;
                    }
                }
            }

            // ...

            break;
    }
}
/**
 * Ubercart submit function needed because of button text altering
 * @see http://drupal.org/node/1170364
 */
function MYMODULE_form_alter_uc_cart_view_form_submit($form, &amp;$form_state)
{

    // Specify the appropriate redirect based on the button used to submit.
    switch ($form_state['clicked_button']['#name']) {
        // Update cart button.
        case 'update-cart':
            // No redirect.  Just display a message and preserve the last URL.
            drupal_set_message(t('Your cart has been updated.'));
            break;

        // Checkout button.
        case 'cart-checkout':
            $form_state['redirect'] = variable_get('uc_checkout_enabled', TRUE) ? 'cart/checkout' : 'cart';
            break;
    }
}

// ...




I also posted some screenshots about the button text altering:

Modifying "Add to cart" button's text:  

BEFORE:


AFTER:




Modifying "Shopping cart" page's buttons:

BEFORE:


AFTER ("Continue shopping" as a "Text link"):


AFTER ("Continue shopping" as a "Button"):