6

I'm currently building a web store with Drupal commerce and would like to get to price shown on a product page to asynchronously refresh when the user increases the amount in the quantity field, such as here.

How can I achieve this?

2

Commerce Cart Ajax

Features

  1. The “remove” button on cart view removes the item from the cart immediately, without a page refresh. The row is removed from the table.
  2. The “Qty” is updated when the "update" button is clicked. The totals are recalculated. No page refresh.
1

You can show the price in the add to cart form using this approach:

/**
* Implements hook_form_FORM_ID_alter
*/
function MODULE_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state){
  // Add read only price field to add to cart form
  if (isset($form_state['default_product']->commerce_price)){
    $price = commerce_product_calculate_sell_price($form_state['default_product']);
    $form['display_price'] = array(
      '#title' => t('Price'),
      '#type' => 'item',
      '#markup' => commerce_currency_format($price['amount'], $price['currency_code']),
      '#prefix' => '<div class="display-price">',
      '#suffix' => '</div>',
    );
  }
}

It may need adjusting to include the quantity multiplier, but should provide a good starting point there.

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.