1

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.

<?php

/**
**Custom Module for hook_form_alter
*/

function MYMODULE_form_alter(&$form, &$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';           
    }   
}
?>

Any help would be much appreciated.

3
  • $form_id == uc_product_add_to_cart_form_1 Why don't you put the form ids in a string? Like this: $form_id == 'uc_product_add_to_cart_form_1' and $form_id == 'uc_cart_view_form'.
    – Sk8erPeter
    Apr 17 '12 at 10:24
  • @Sk8erPeter Nice finding.
    – niksmac
    Apr 17 '12 at 10:34
  • @Sk8erPeter hmm... What to say ?
    – niksmac
    Apr 22 '12 at 6:19
2

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: "Cart settings" on "/admin/store/settings/cart/edit/basic"
  • 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(&$form, &$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, &$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: Modifying "Add to cart" button's text - BEFORE

AFTER: Modifying "Add to cart" button's text - AFTER


Modifying "Shopping cart" page's buttons:

BEFORE: Modifying "Shopping cart" page's buttons - BEFORE

AFTER ("Continue shopping" as a "Text link"): Modifying "Shopping cart" page's buttons - AFTER ("Continue shopping" as a "Text link")

AFTER ("Continue shopping" as a "Button"): Modifying "Shopping cart" page's buttons - AFTER ("Continue shopping" as a "Button")

9
  • Great work Sk8erPeter
    – niksmac
    Apr 21 '12 at 8:11
  • @NikhilMohan: thank you! :) (if you found it useful, you can also upvote it, thanks :P)
    – Sk8erPeter
    Apr 21 '12 at 17:38
  • Apologies for the late reply. This was very helpful. but after trying the above code the 'update' and 'continue' buttons work but the back button shows the markup as title text - you can see here files.perfectday.gb.com/internal/stackoverflow/ubercart/… - any ideas how I can fix this?
    – piranah
    May 11 '12 at 16:48
  • Seems like my output is not a link but a button. I initally thought it may be my theme but it is rendered for me like this in garland and not like your screens. files.perfectday.gb.com/internal/stackoverflow/ubercart/…
    – piranah
    May 11 '12 at 17:11
  • 1
    @piranah: you're welcome! :)
    – Sk8erPeter
    May 13 '12 at 20:00
0

Try this

function MYMODULE_form_alter(&$form, &$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';           
    }   
}

Thanks Sk8erPeter

3
  • Thanks for the reply Nikhi. I tried with the above code but still no luck - all buttons just reload the cart page.
    – piranah
    Apr 17 '12 at 13:28
  • did you try something like $form['submit'] = array('#value' => 'Register',);
    – au_stan
    Apr 18 '12 at 13:47
  • @austin: that's not correct, because this way you can remove other necessary elements from the array. With the correct $form['submit']['#value'] = 'Register', you only override the #value key's value in the array.
    – Sk8erPeter
    Apr 21 '12 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.