I'm new to drupal. What I want is the price to be updated for an ubercart product when I change the quantity the same way it happens with the attributes on the product page.

So far I have a custom module to edit the type of the edit quantity form when I add a product in my cart. I turned it to a select box and tried to append the class ajax-processed but nothing happens.

  • well I gave up on this for now but it still bugs me so I'll keep the question. So far I have managed to update the sell price based on the quantity by adding buttons (via hook_form_alter) with ajax_fom_callbacks invoking commands to update the price. Yet when I click on a priced attribute the price gets updated again thus not taking the quantity into account as it happens by default.
    – D3v
    Sep 26, 2017 at 12:02

1 Answer 1


Well after some tail chasing I managed to do it. It probably is a bad solution since I had to modify uc_product_module itself. Here's what I did:

1) Created a custom module that adds two buttons in hook_form_alter- and + that will modify the quantity:

    $form['decrease'] = array(
        '#name' => 'decrease',
        '#id' => 'decrease-product-qty',
        '#type' => 'button',
        '#value' => t('-'),
        '#prefix' => '<div class="form-item form-item-qty-btn form-type-uc-quantity form-group">',
        '#ajax' => array(
            'wrapper' => 'wrapper',
            'callback' => 'decreaseProductQuantity_form_ajax_callback'

 * AJAX callback function for addToCartFormEditQuantity_form().
function decreaseProductQuantity_form_ajax_callback($form, $form_state)
    $nid = $form_state['values']['nid'];
    $node = node_load($nid);

//Get current quantity
$quantity = (int)$form['qty']['#value'];
// Adjust
if ($quantity > 1) {

$form_state['storage']['quantity'] = $quantity;

// Calculate updated price to be displayed
$displayPrice = uc_currency_format($node->price*$quantity);
// Price textfield id and class
$fieldId = "#total-price-uc-product-" . $nid;
$fieldClass = '.uc-price';

// Update field via command
$commands = array();
$commands[] = ajax_command_invoke('#edit-qty', 'val', array($quantity));
$commands[] = ajax_command_html($fieldClass, $displayPrice);

// Send the command to the page.
$page = array(
    '#type' => 'ajax',
    '#commands' => $commands,
return $page; 

2) Saved quantity in $form_state['storage']['quantity'] = $quantity; as seen above. In order to keep the value form being erased add use $form['after_build']:

//Inside hook_form_alter
$form['#after_build'][] = 'addToCartFormEditQuantity_after_build';

function addToCartFormEditQuantity_after_build(&$element, &$form_state)
    $form_state['storage']['quantity'] = (int)$element['qty']['#value'];
    return $element;

3) Final step modified ubercart/uc_product/uc_product.module function uc_product_view_ajax_commands:

 * @return
 *   An array of Ajax commands.
function uc_product_view_ajax_commands($form_state, $keys) {
  $commands = array();
  if (variable_get('uc_product_update_node_view', FALSE) && !empty($form_state['storage']['variant'])) {
    $node_div = '.uc-product-' . $form_state['storage']['variant']->nid;
    $build = node_view($form_state['storage']['variant']);
    foreach ($keys as $key) {
      if (isset($build[$key])) {
         //Modify display price to take qantity into consideration
        if ($key === "display_price") {
          $quantity = (int)$form_state['storage']['quantity'];
          $totalPrice = (float)$build[$key]['#value'];
          $totalPrice *= $quantity;

          $build[$key]['#value'] = $totalPrice;
        $id = $node_div . '.' . str_replace('_', '-', $key);
        $commands[] = ajax_command_replace($id, drupal_render($build[$key]));
  return $commands;

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.