You are here

basic_cart.module in Basic cart 7.3

File

basic_cart.module
View source
<?php

/**
 * @file
 * Basic cart module file.
 */
require_once dirname(__FILE__) . '/basic_cart.theme.inc';

/**
 * Implements hook_permission().
 */
function basic_cart_permission() {
  return array(
    'administer basic cart' => array(
      'title' => t('Administer basic cart'),
      'description' => t('Perform administration tasks for basic cart.'),
    ),
    'view basic cart orders' => array(
      'title' => t('View basic cart orders'),
      'description' => t('View basic cart orders.'),
    ),
    'use basic cart' => array(
      'title' => t('Use basic cart'),
      'description' => t('Use basic cart (add to cart, remove from cart, checkout).'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function basic_cart_menu() {
  $items = array();
  $items['admin/config/basic_cart'] = array(
    'title' => 'Basic cart',
    'description' => 'Basic cart content type selection.',
    'position' => 'left',
    'weight' => -10,
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array(
      'administer basic cart',
    ),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
  );
  $items['admin/config/basic_cart/settings'] = array(
    'title' => 'Basic cart',
    'description' => 'Basic cart content type selection.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'basic_cart_admin_content_type',
    ),
    'access arguments' => array(
      'administer basic cart',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'basic_cart.admin.inc',
  );
  $items['admin/config/basic_cart/settings/settings'] = array(
    'title' => 'Settings',
    'description' => 'Basic cart content type selection.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'basic_cart_admin_content_type',
    ),
    'access arguments' => array(
      'administer basic cart',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'file' => 'basic_cart.admin.inc',
    'weight' => 5,
  );
  $items['cart'] = array(
    'title' => 'Your shopping cart',
    'description' => 'The shopping cart page',
    'page callback' => 'basic_cart_cart',
    'access arguments' => array(
      'use basic cart',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'basic_cart.cart.inc',
  );
  $items['cart/add'] = array(
    'title' => 'Add to cart',
    'description' => 'Add to cart url.',
    'page callback' => 'basic_cart_add_to_cart',
    'access arguments' => array(
      'use basic cart',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'basic_cart.cart.inc',
  );
  $items['cart/remove'] = array(
    'title' => 'Remove from cart',
    'description' => 'Remove from cart url.',
    'page callback' => 'basic_cart_remove_from_cart',
    'access arguments' => array(
      'use basic cart',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'basic_cart.cart.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function basic_cart_theme() {
  return array(
    'basic_cart_cart_total_price' => array(
      'variables' => array(
        'price' => NULL,
      ),
    ),
    'basic_cart_price' => array(
      'function' => 'basic_cart_price',
      'variables' => array(
        'price' => NULL,
      ),
    ),
    'basic_cart_add_to_cart' => array(
      'function' => 'basic_cart_add_to_cart_button',
      'variables' => array(
        'path' => NULL,
        'css_class' => NULL,
      ),
    ),
    'basic_cart_cart_flat' => array(
      'template' => 'basic_cart_cart_flat',
      'variables' => array(
        'cart' => NULL,
        'price' => NULL,
        'vat' => NULL,
      ),
    ),
    'basic_cart_cart_render_block' => array(
      'template' => 'basic_cart_cart_render_block',
      'variables' => array(
        'cart' => NULL,
        'price' => NULL,
        'vat' => NULL,
      ),
    ),
    'basic_cart_render_cart_element' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * Implements hook_block_info().
 */
function basic_cart_block_info() {
  $blocks['shopping_cart'] = array(
    'info' => t('Shopping cart'),
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function basic_cart_block_view($delta = '') {

  // Check if the current user has access.
  switch ($delta) {
    case 'shopping_cart':
      if (user_access('use basic cart')) {

        // Price format.
        module_load_include('inc', 'basic_cart', 'basic_cart.cart');
        $price = basic_cart_get_total_price();
        $total = basic_cart_price_format($price->total);
        $options = array(
          'cart' => basic_cart_get_cart(),
          'price' => $total,
        );

        // Checking the VAT.
        $vat_is_enabled = (int) variable_get('basic_cart_vat_state');
        if (!empty($vat_is_enabled) && $vat_is_enabled) {
          $options['vat'] = basic_cart_price_format($price->vat);
        }
        $block['subject'] = t('Your cart');
        $block['content'] = theme('basic_cart_cart_render_block', $options);
        return $block;
      }
      break;
  }
}

/**
 * Implements hook_node_presave().
 * 
 * This hook is called before the node is saved in the db.
 * 
 * Used for adding the right url to the add to cart button, only when a node ID 
 * is available. This is the case only when we update products. When we insert new products
 * the hook_node_insert function is used instead.
 */

/*function basic_cart_node_presave($node) {
  
  // If we don't have a node ID, there's no point to this.
  if (empty($node->nid)) {
    return;
  }
    
  // Check if the current user has access to basic cart.
  if (user_access('use basic cart')) {
    // Getting node types.
    $node_types = basic_cart_product_types();
    if (!is_array($node_types) || empty($node_types)) {
      return;
    }
    // The node is of type product.
    if (!empty($node_types[$node->type]) &&  in_array($node->type, $node_types)) {
      // Setting the right add to cart link.
      $node->add_to_cart[LANGUAGE_NONE][0]['value'] = 'cart/add/' . $node->nid;
    }
  }
}*/

/**
 * Implements hook_node_insert().
 * 
 * This hook is called after the node was saved in the db.
 * 
 * Used for adding the right url to the add to cart button, only when a node ID 
 * is NOT available. This is the case only when we insert new products.
 */

/*function basic_cart_node_insert($node) {
  // Check if the current user has access to basic cart.
  if (user_access('use basic cart')) {
    // Getting node types.
    $node_types = basic_cart_product_types();
    if (!is_array($node_types) || empty($node_types)) {
      return;
    }
    // The node is of type product.
    if (!empty($node_types[$node->type]) &&  in_array($node->type, $node_types)) {
      // Setting the right add to cart link. Using field_attach_update for this operation as we don't have the node id yet.
      $node->add_to_cart[LANGUAGE_NONE][0]['value'] = 'cart/add/' . $node->nid;
      field_attach_update('node', $node);
    }
  }
}*/

/**
 * Implements hook_field_formatter_info().
 */
function basic_cart_field_formatter_info() {
  $info = array(
    'basic_cart_add_to_cart' => array(
      'label' => t('Add to cart button'),
      'field types' => array(
        'text',
      ),
      'description' => t('Adds an add to cart button.'),
      'settings' => array(
        'css_class' => 'button basic-cart-add-to-cart-link',
      ),
    ),
    'basic_cart_add_to_cart_quantity' => array(
      'label' => t('Add to cart button with quantity'),
      'field types' => array(
        'text',
      ),
      'description' => t('Adds an add to cart button with quantity.'),
    ),
    'basic_cart_price' => array(
      'label' => t('Basic cart price format'),
      'field types' => array(
        'number_decimal',
      ),
      'description' => t('Uses the price format defined on the configuration page.'),
    ),
  );
  return $info;
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function basic_cart_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {

  //This gets the view_mode where our settings are stored
  $display = $instance['display'][$view_mode];

  //This gets the actual settings
  $settings = $display['settings'];

  //Initialize the element variable
  $element = array();
  switch ($display['type']) {
    case 'basic_cart_add_to_cart':
      $element['css_class'] = array(
        '#type' => 'textfield',
        '#title' => t('CSS class'),
        '#description' => t('This class will be added to the button\'s HTML code.'),
        '#default_value' => $settings['css_class'],
      );
      break;
  }
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function basic_cart_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = '';
  switch ($display['type']) {
    case 'basic_cart_add_to_cart':
      $summary = t('Classes for add to cart button: "@css_class"', array(
        '@css_class' => $settings['css_class'],
      ));
      break;
  }
  return $summary;
}

/**
* Implements hook_field_formatter_view().
*/
function basic_cart_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  switch ($display['type']) {
    case 'basic_cart_add_to_cart':
      if (!empty($items) && is_array($items)) {
        foreach ($items as $delta => $item) {
          $css_class = !empty($settings['css_class']) ? $settings['css_class'] : '';
          $element[$delta] = array(
            '#markup' => theme('basic_cart_add_to_cart', array(
              'path' => 'cart/add/' . $entity->nid,
              'css_class' => $css_class,
            )),
          );
        }
      }
      else {
        $css_class = !empty($settings['css_class']) ? $settings['css_class'] : '';
        $element[] = array(
          '#markup' => theme('basic_cart_add_to_cart', array(
            'path' => 'cart/add/' . $entity->nid,
            'css_class' => $css_class,
          )),
        );
      }
      break;
    case 'basic_cart_add_to_cart_quantity':
      $element[] = drupal_get_form('basic_cart_add_to_cart_quantity_form', $entity->nid);
      break;
    case 'basic_cart_price':
      foreach ($items as $delta => $item) {
        $value = basic_cart_price_format($item['value']);
        $element[$delta] = array(
          '#markup' => theme('basic_cart_price', array(
            'price' => $value,
          )),
        );
      }
      break;
  }
  return $element;
}

/**
 * Add to cart with quantity form.
 */
function basic_cart_add_to_cart_quantity_form($form, $form_state, $nid = NULL) {
  $form['nid'] = array(
    '#type' => 'value',
    '#value' => $nid,
  );
  $form['quantity'] = array(
    '#type' => 'textfield',
    '#title' => t('Quantity'),
    '#default_value' => '1',
    '#size' => 1,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add to cart'),
  );
  return $form;
}

/**
 * Submit callback for Add to cart with quantity form.
 */
function basic_cart_add_to_cart_quantity_form_submit($form, $form_state) {
  dsm($form_state);
  basic_cart_add_to_cart($form_state['values']['nid'], $form_state['values']['quantity']);
}

/**
 * Implements template_preprocess_field().
 * 
 * Stupid fix for having drupal take into consideration the value returned by 
 * basic_cart_field_formatter_view() for formatting the "Add to cart" button.
 * 
 * Without this fix, the button doesn't appear, since there is no value for it.
 */
function basic_cart_preprocess_field(&$variables, $hook) {
  $field = $variables['element'];
  if ($field['#field_name'] == 'add_to_cart') {
    $variables['items'][] = $field[0];
  }
}

/**
 * Implements hook_form_alter().
 * 
 * Used for removing the add to cart text field from the node/add or node/edit form.
 */
function basic_cart_form_alter(&$form, &$form_state, $form_id) {
  $types = basic_cart_product_types();
  if (!empty($types) && is_array($types)) {
    $types_ = array();
    foreach ($types as $value) {
      $types_[] = $value . '_node_form';
    }

    // Check if we are on a product content type.
    if (in_array($form_id, $types_)) {

      // We are - hiding the add to cart field.
      if (isset($form['add_to_cart'])) {
        unset($form['add_to_cart']);
      }
    }
  }
}

/**
 * Helper function. Returns true if given node (node object or string node type) is a product.
 */
function basic_cart_is_product($node) {
  $type = is_object($node) ? $node->type : $node;

  // Getting node types.
  $node_types = basic_cart_product_types();
  if (empty($node_types) || !is_array($node_types)) {
    return FALSE;
  }
  return in_array($type, $node_types);
}

/**
 * Helper function. Returns an array of basic_cart product (content) types.
 */
function basic_cart_product_types() {
  static $types;
  if (empty($types)) {
    $types = variable_get('basic_cart_content_types', array());
  }
  return $types;
}

/**
 * Implementation hook_views_api()
 */
function basic_cart_views_api() {
  return array(
    'api' => '2.0',
    'path' => drupal_get_path('module', 'basic_cart') . '/views',
  );
}

/**
 * Returns the fields we need to create.
 * 
 * @return mixed
 *   Key / Value pair of field name => field type. 
 */
function basic_cart_get_fields() {
  return array(
    'price' => array(
      'type' => 'number_decimal',
      'title' => t('Price'),
      'description' => t('Please enter this item\'s price.'),
    ),
    'add_to_cart' => array(
      'type' => 'text',
      'title' => 'Add to cart',
      'description' => '',
    ),
  );
}

Functions

Namesort descending Description
basic_cart_add_to_cart_quantity_form Add to cart with quantity form.
basic_cart_add_to_cart_quantity_form_submit Submit callback for Add to cart with quantity form.
basic_cart_block_info Implements hook_block_info().
basic_cart_block_view Implements hook_block_view().
basic_cart_field_formatter_info Implements hook_field_formatter_info().
basic_cart_field_formatter_settings_form Implements hook_field_formatter_settings_form().
basic_cart_field_formatter_settings_summary Implements hook_field_formatter_settings_summary().
basic_cart_field_formatter_view Implements hook_field_formatter_view().
basic_cart_form_alter Implements hook_form_alter().
basic_cart_get_fields Returns the fields we need to create.
basic_cart_is_product Helper function. Returns true if given node (node object or string node type) is a product.
basic_cart_menu Implements hook_menu().
basic_cart_permission Implements hook_permission().
basic_cart_preprocess_field Implements template_preprocess_field().
basic_cart_product_types Helper function. Returns an array of basic_cart product (content) types.
basic_cart_theme Implements hook_theme().
basic_cart_views_api Implementation hook_views_api()