You are here

function template_preprocess_dc_ajax_shopping_cart in Commerce Ajax Add to Cart 7

Same name and namespace in other branches
  1. 7.2 dc_ajax_add_cart.module \template_preprocess_dc_ajax_shopping_cart()

Add more variables to shopping cart block.

Default template: ajax-shopping-cart.tpl.php.

Parameters

array $variables: An associative array containing:

  • order: Order object of the current user.
  • line_items: Line items wrapper.
  • quantity: Number of items in the cart.
  • total: Array containing the total amount and the default currency you are in the site.

File

./dc_ajax_add_cart.module, line 450
Ajax add to cart module.

Code

function template_preprocess_dc_ajax_shopping_cart(&$variables) {

  // Check whether order object exists.
  if ($variables['order']) {
    $header = array();
    $row = array();
    $image_remove_cart_path = drupal_get_path('module', 'dc_ajax_add_cart') . '/images/remove-from-cart.png';
    $image_remove_cart = theme('image', array(
      'path' => $image_remove_cart_path,
      'alt' => t('Remove from cart'),
      'title' => t('Remove from cart'),
    ));

    // Default currency array.
    $currency = commerce_currency_load(commerce_default_currency());

    // Output arrays.
    $line_item_list = array();
    $products = array();
    $product_prices = array();
    $shipping = array();
    $product_price_total = commerce_currency_format($variables['total']['amount'], $currency['code']);

    // Create the line item list and products array.
    foreach ($variables['line_items'] as $line_item_wrapper) {
      $line_item = $line_item_wrapper
        ->value();

      // If the line item is a product.
      if (property_exists($line_item, 'commerce_product')) {
        $product = commerce_product_load($line_item->commerce_product[LANGUAGE_NONE][0]['product_id']);
        $products[$product->product_id] = $product;
        $product_prices[$product->product_id] = commerce_currency_format($line_item->commerce_unit_price[LANGUAGE_NONE][0]['amount'], $currency['code']);
      }
      elseif (property_exists($line_item, 'commerce_shipping_service')) {
        $shipping['service'] = $line_item->line_item_label;
        $shipping['price'] = commerce_currency_format($line_item->commerce_unit_price[LANGUAGE_NONE][0]['amount'], $currency['code']);
      }
      $line_item_list[$line_item->line_item_id] = $line_item;
    }

    // Create checkout url.
    if (variable_get(DC_AJAX_ADD_CART_CHECKOUT_REDIRECT, 'cart_page') == 'cart_page') {
      $variables['checkout_link'] = array(
        'text' => t('Checkout'),
        'path' => 'cart',
        'attributes' => array(
          'class' => array(
            'dc-ajax-add-cart-checkout-link',
            'dc-ajax-add-cart-checkout-cart',
          ),
        ),
        'query' => array(),
        'fragment' => '',
        'html' => FALSE,
      );
    }
    else {
      $variables['checkout_link'] = array(
        'text' => t('Checkout'),
        'path' => 'checkout',
        'attributes' => array(
          'class' => array(
            'dc-ajax-add-cart-checkout-link',
            'dc-ajax-add-cart-checkout-checkout',
          ),
        ),
        'query' => array(),
        'fragment' => '',
        'html' => FALSE,
      );
    }

    // Shopping cart.
    if (variable_get(DC_AJAX_ADD_CART_SHOW_LABELS, 'label') == 'label') {
      $variables['products_list']['header'][] = array(
        'data' => t('Quantity'),
        'header_classes' => 'quantity-label',
      );
      $variables['products_list']['header'][] = array(
        'data' => t('Items'),
        'header_classes' => 'item-label',
      );
      $variables['products_list']['header'][] = array(
        'data' => t('Price'),
        'header_classes' => 'price-label',
      );
      $variables['products_list']['header'][] = array(
        'data' => '',
      );
    }
    foreach ($line_item_list as $line_item) {
      if (property_exists($line_item, 'commerce_product')) {
        $product = commerce_product_load($line_item->commerce_product[LANGUAGE_NONE][0]['product_id']);
        if (variable_get('dc_ajax_add_cart_update_quantity', 0) == 1) {
          $updateable_quantity = drupal_get_form('dc_ajax_add_cart_update_quantity_' . $line_item->line_item_id, $line_item);
          $quantity = drupal_render($updateable_quantity);
        }
        else {
          $quantity = intval($line_item->quantity);
        }
        $variables['products_list']['row'][$line_item->line_item_id]['quantity'] = array(
          'data' => $quantity,
          'field_classes' => 'quantity',
        );
        $variables['products_list']['row'][$line_item->line_item_id]['title'] = array(
          'data' => $product->title,
          'field_classes' => 'name',
        );
        $variables['products_list']['row'][$line_item->line_item_id]['price'] = array(
          'data' => $product_prices[$product->product_id],
          'field_classes' => 'price',
        );
        $variables['products_list']['row'][$line_item->line_item_id]['remove'] = array(
          'data' => l(variable_get(DC_AJAX_ADD_CART_REMOVE_CART, 'link') == 'link' ? t('Remove from cart') : $image_remove_cart, 'remove-product/nojs/' . $line_item->line_item_id, array(
            'attributes' => array(
              'class' => array(
                'use-ajax',
              ),
            ),
            'html' => TRUE,
          )),
          'field_classes' => 'remove-from-cart',
        );
        $variables['products_list']['row_classes'][$line_item->line_item_id] = 'ajax-cart-row';
      }
    }

    // Create new theme variables.
    $variables['line_item_list'] = $line_item_list;
    $variables['products'] = $products;
    $variables['product_prices'] = $product_prices;
    $variables['product_price_total'] = $product_price_total;
    $variables['checkout_url'] = l($variables['checkout_link']['text'], $variables['checkout_link']['path'], array(
      'attributes' => $variables['checkout_link']['attributes'],
      'query' => $variables['checkout_link']['query'],
      'fragment' => $variables['checkout_link']['fragment'],
      'html' => $variables['checkout_link']['html'],
    ));
    $variables['products_list']['classes'] = 'ajax-shopping-cart-table';
    if (!empty($shipping)) {
      $variables['shipping'] = $shipping;
    }
  }

  // Create new theme variables related to configuration.
  $variables['configuration']['show_labels'] = variable_get(DC_AJAX_ADD_CART_SHOW_LABELS, 'label');
  $variables['configuration']['remove_cart'] = variable_get(DC_AJAX_ADD_CART_REMOVE_CART, 'link');
  $variables['configuration']['empty_cart_message'] = check_plain(variable_get(DC_AJAX_ADD_CART_EMPTY_CART_MESSAGE, t('Shopping cart is empty')));
}