Hi.

I was wondering if there is a way to have an auto-update each time the quantity field changes at the shopping cart form?

Otherwise can we have a separate update button for each quantity-line item and not for the entire form??

Thank you.

Comments

rszrama’s picture

Status: Active » Fixed

It's technically possible using hook_form_alter() on the Add to Cart form, but it's not something I think we'll be putting in core. You would need to add #ajax settings to the quantity widget to work on change.

frixos12’s picture

Is there any particular reason for not putting this functionality in core?

More specifically, you think it's better from the customer's point of view to have an update button for the entire shopping cart form?

I am developing an e-shop and i would like to hear your opinion in order to decide the best method for updating a cart.

Thank you

rszrama’s picture

There's no particular reason to keep it out of core other than feature bloat; we'd hope to avoid that, so non-essential core features are typically punted to contribs. I'm not sure exactly how we'd integrate a feature like this into the default Cart View, but I know that it should be technically possible in contrib. I'd feel more comfortable moving it to core after seeing how much code it took to achieve and whether or not it resulted in any unintended side effects on that form.

frixos12’s picture

Hi and thank you for your post!

Can you give me some code hints of how to add #ajax settings inside hook_form_alter??

Thank you

rszrama’s picture

Nothing special - just look at how we use #ajax inside of Commerce and imagine adding it into the form via an alter instead of the original form. If you aren't sure what these things are, I'd recommend getting a Drupal 7 development book.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jonne.freebase’s picture

If anyone is wondering how to do this, I managed to make it work with the following code:

(function($){
  Drupal.behaviors.updateQuantity = {
    attach: function (context, settings) {
      $('#views-form-commerce-cart-form-default .form-type-textfield input', context).blur(function(){
        $('#edit-submit',context).mousedown();
      });
    }
  }
})(jQuery);

(put it in your theme's .js file)

If you use this in conjunction with the DC cart AJAX module, this code will reload the cart form after changing the value of the field. If you know a better way of accomplishing this, please let me know.

ThomasIsabelle’s picture

Hello jonne.freebase,

I followed your suggestion, but it isn't working for me.

1. Installed and enabled the DC cart AJAX module.
2. Set "Use AJAX " to Yes in "admin/structure/views/view/commerce_cart_form/edit" and saved.
3. Inserted your code into "profiles/commerce_kickstart/themes/omega_kickstart/js/omega_kickstart.js" (my site's theme for the Kickstart Commerce demo site.
4. And nothing. Still not auto-updating.

Any thoughts on why?

Thank you!

Tom

alexverb’s picture

Here's the way rszrama suggested:

function mymodule_form_commerce_cart_add_to_cart_form_alter(&$form, $form_state, $form_id) {
  // Add ajax to our add to cart form.
  if (isset($form['quantity'])) {
    $form['quantity']['#ajax'] = array(
      'callback' => 'commerce_cart_add_to_cart_form_attributes_refresh',
    );
  }
}

function mymodule_commerce_cart_attributes_refresh_alter(&$commands, $form, $form_state) {
  // Do our own quantity calculation and alter the replacement command.
  if (isset($form_state['line_item']->quantity)) {
    if (!empty($form_state['context'])) {
      $product = $form_state['default_product'];
      $product->display_context = $form_state['context'];
      $product->commerce_price['und'][0]['original']['amount'] = $form['quantity']['#value'] * $product->commerce_price['und'][0]['amount'];

      // First render the actual fields attached to the referenced product.
      foreach (field_info_instances('commerce_product', $product->type) as $product_field_name => $product_field) {

        if ($product_field_name == 'commerce_price') {
          // Rebuild the same array of classes used when the field was first rendered.
          $replacement_class = drupal_html_class(implode('-', array($form_state['context']['class_prefix'], 'product', $product_field_name)));

          $classes = array(
            'commerce-product-field',
            drupal_html_class('commerce-product-field-' . $product_field_name),
            drupal_html_class('field-' . $product_field_name),
            $replacement_class,
          );

          $element = field_view_field('commerce_product', $product, $product_field_name, $form_state['context']['view_mode']);

          // Add an extra class to distinguish empty product fields.
          if (empty($element)) {
            $classes[] = 'commerce-product-field-empty';
          }

          $element += array(
            '#prefix' => '<div class="' . implode(' ', $classes) . '">',
            '#suffix' => '</div>',
          );

          $commands[] = ajax_command_replace('.' . $replacement_class, drupal_render($element));
        }
      }
    }
  }
}

I do think that frixos12 is right that this feature should be a simple setting in the cart display settings. This alter probably takes way much effort than just checking a setting. +1 for voting it in core functionality.

One more caveat. This code doesn't take in consideration if a page refresh should happen. Then the price would be back at it's original value for quantity 1 with another quantity as value. Any suggestions for that are welcome...

alexverb’s picture

Now that I think about it, this all isn't such a good idea when you have other price calculations that have to be taken in consideration. Like for example, buying 10 items gives you 10 percent off... Do you think it's possible to run rules during ajax to calculate the price?

marilena6’s picture

Hello,
I'm building a directory site with ordering features and I have this issue:
I've created a Product Display which is loaded in a popup after a user clicks on the product. I've added a rule when the user clicks add to cart, a custom .php file is called so the popup is closed and the parent window which contains the cart block is reloaded

<script>
window.opener.location.reload();
window.close();

</script>

but I was wondering if this could be done with Ajax, so when the user closes the popup (if he adds the product) the cart will be automatically reloaded and not the whole parent page.

bellagio’s picture

Issue summary: View changes

Hello alexverb, #10 doesn't change price when quantity is changed with fresh install of kickstart profile and mymodule only. could you let us know if your code is working for you?