如何用ajax改变产品数量?

时间:2016-07-26 18:12:41

标签: php drupal drupal-7 drupal-commerce

我正在使用Drupal 7和Commerce模块。我的购物车上显示了一个计数器:

shopping cart with 57 written at the right

这是hook_block_info的代码

function commerce_popup_cart_block_info() {
  $blocks = array();  
  // Define the basic shopping cart block and hide it on the checkout   pages.
  $blocks['commerce_popup_cart'] = array(
    'info' => t('Popup Shopping cart'),
    'cache' => DRUPAL_NO_CACHE,
    'visibility' => 0,    
  );  
  return $blocks;
}

但是,当我添加新产品时,虽然新商品确实出现在购物车中,但此商品上方的商品数量并未发生变化。我有以下代码负责卡内的更改:

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);
}

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;
} 

这是保存图像的块的代码:

function commerce_popup_cart_block_view($delta='') {
  $block = array();

  switch($delta) {    
    case 'commerce_popup_cart':
      global $user;
      // Default to an empty cart block message.
      $content = '';

      // First check to ensure there are products in the shopping cart.
      if ($order = commerce_cart_order_load($user->uid)) {
        $wrapper = entity_metadata_wrapper('commerce_order', $order);

          // Build the variables array to send to the cart block template.
          $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">'. $prod_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);
          }
      }elseif (variable_get('commerce_popup_cart_show_empty_cart', 0) == 1){        
            $content = commerce_popup_cart_block_view_get_empty_cart($variables = array());          
      }

      // If the superfish module is not installed then add hoverintent script
      if (!module_exists('superfish')){
        drupal_add_js(drupal_get_path('module','commerce_popup_cart') . '/js/jquery.hoverIntent.minified.js');  
      }

      return array('subject' => t('Shopping cart'), 'content' => $content);
      break;
  } 
  return $block;
}

0 个答案:

没有答案