how to update quantity of products in the cart via javascript?

Drupal Answers Asked by rick1 on November 30, 2020

I have the module that changes the quantity of products inside the card via ajax but I need to change it also outside, as on the picture, enter image description here

the code that changes the quantity after reload of the page are as follows

$variables = array(
        'order' => $order,
        'contents_view' => commerce_embed_view('commerce_cart_block', 'defaults', array($order->order_id), $_GET['q']),
      $count = commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types());

      $quantity = 0;

      foreach ($wrapper->commerce_line_items as $line_item) {
        if (!$line_item instanceof EntityMetadataWrapper) {
          $line_item = entity_metadata_wrapper('commerce_line_item', $line_item);
        $types = array('product');

        if (empty($types) || in_array($line_item->type->value(), $types)) {
          $quantity = $quantity + $line_item->quantity->value();


      $prod_count = t($quantity);  

      if ($prod_count > 0){

        $icon = '<div class="cart-icon"></div><span class="cart_popup_count">'. $count . '</span>';
        $content = '<div id="cart-popup" style="display:none;">' . theme('commerce_cart_block', $variables) . '<div class="popup-arrow"></div></div>';
        $content = '<div class="wrapper">' . $icon . $content . '</div>';  
      }elseif (variable_get('commerce_popup_cart_show_empty_cart', 0) == 1){
        $content = commerce_popup_cart_block_view_get_empty_cart($variables);

The problem is that I don’t know how to changes this numbers simultaneously inside and outside the card.

This is the code that changes quantity inside the card

  function _commerce_popup_cart_add_to_cart_ajax_callback($form, $form_state) {
  commerce_cart_add_to_cart_form_submit($form, $form_state); //Call the add to cart form to actually add the product
  $res = _commerce_popup_cart_ajax_cart_reload(); //Retrieve the new cart view

  $commands = array();
  $commands[] = ajax_command_replace("#block-commerce-popup-cart-commerce-popup-cart .cart-empty-block", "<div class='cart-contents'></div>");
  $commands[] = ajax_command_html('#block-commerce-popup-cart-commerce-popup-cart .cart-contents', $res);
  return array('#type' => 'ajax', '#commands' => $commands); 

function _commerce_popup_cart_ajax_cart_reload() {
  global $user;
  $view_name = 'commerce_cart_block'; // The name of the view we are going to load commerce_cart_block
  $args = array(commerce_cart_order_id($user->uid));  // Array of arguments we set for the view. Only one argument in our example. your actual view may require additional 

  //arguments which you may need to set   
  $displayId = 'default'; // The display id of for the view.
  // Call the views_embed_view function to returned themed view output
  $res = views_embed_view($view_name, $displayId, $args);
  return $res;

One Answer

  • Create new simple module like my_updater_card
  • add hook_menu
  • add php function to update the card
  • call this function from ajax

     * Implements hook_menu().
     * see hook_menu()
     * see menu_example
    function my_updater_card_menu()
        $items['my_updater/card/%'] = array(
            'page callback'         => 'my_updater',
            'page arguments'        => array(2),
            'access arguments'      => array('put your permission here'),
        return $items;
    function my_updater($newTotal){
        //check if this request come from ajax => if not => ignore it
        //here put your php code to update the card
        //then return results true/false to ajax caller


               url: 'my_updater/card/[new total here]',
               type: "GET",
               success: function (data) {
                 //do total refresh here 

Answered by Mohammad Alabed on November 30, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP