You are here

basic_cart.install in Basic cart 7.3

Basic cart install file

File

basic_cart.install
View source
<?php

/**
 * @file
 * Basic cart install file
 */

/**
 * Default currency.
 */
define('BASIC_CART_DEFAULT_CURRENCY', 'USD');

/**
 * Default VAT state (enabled/disabled). 
 */
define('BASIC_CART_VAT_STATE', FALSE);

/**
 * Default VAT value. Zero by default.
 */
define('BASIC_CART_VAT_VALUE', '');

/**
 * Implements hook_install().
 */
function basic_cart_install() {
  $t = get_t();

  // Setting up the default currency.
  variable_set('basic_cart_currency', BASIC_CART_DEFAULT_CURRENCY);

  // Setting up the default messages.
  variable_set('basic_cart_admin_subject', $t('New order placed'));
  $default_admin_message = $t('Hi,
An order was placed by [node:title] ([node:field_email]
Ordered products:
[basic_cart_order:products]
Total price: [node:field_total_price]');
  variable_set('basic_cart_admin_message', $default_admin_message);
  variable_set('basic_cart_send_user_message', 1);
  variable_set('basic_cart_user_subject', $t('New order placed'));
  $default_user_message = $t('Hi,
You just placed an order on the website @sitename. Here are the order details:
[basic_cart_order:products]
Total price: [node:field_total_price]
We will contact you as soon as possible.', array(
    '@sitename' => variable_get('site_name'),
  ));
  variable_set('basic_cart_user_message', $default_user_message);
  $default_thank_you_title = $t("Thank you.");
  variable_set('basic_cart_thank_you_title', $default_thank_you_title);
  $default_thank_you_message = $t("Thank you for placing an order on our website.\nWe will contact you as soon as possible.");
  variable_set('basic_cart_thank_you_message', $default_thank_you_message);

  // Redirect url after add to cart.
  variable_set('basic_cart_redirect_user_after_add_to_cart', '<none>');
  foreach (basic_cart_get_fields() as $field_name => $field_) {

    // Check to see if the field field already exists.
    $field = field_info_field($field_name);

    // If the field does not exist then create it.
    if (empty($field)) {
      $field = array(
        'field_name' => $field_name,
        'type' => $field_['type'],
        'entity_types' => array(
          'node',
        ),
      );
      field_create_field($field);
    }
  }

  // Setting the VAT default state and value.
  variable_set('basic_cart_vat_state', BASIC_CART_VAT_STATE);
  variable_set('basic_cart_vat_value', BASIC_CART_VAT_VALUE);
}

/**
 * Implements hook_uninstall().
 */
function basic_cart_uninstall() {

  // Currency.
  variable_del('basic_cart_currency');

  // Delete the price field.
  $types = variable_get('basic_cart_content_types');
  if (is_array($types)) {
    drupal_load('module', 'basic_cart');

    // Foreach selected content type, delete the price field.
    foreach ($types as $type => $checked) {
      if (!empty($checked)) {
        foreach (basic_cart_get_fields() as $field_name => $field_) {

          // Price instance.
          $instance = array(
            'field_name' => $field_name,
            'entity_type' => 'node',
            'bundle' => $type,
          );
          field_delete_instance($instance);
        }
      }
    }
  }

  // Delete the content types from the variables table.
  variable_del('basic_cart_content_types');

  // Admin confirmation mail.
  variable_del('basic_cart_admin_subject');
  variable_del('basic_cart_admin_message');

  // User confirmation email.
  variable_del('basic_cart_user_subject');
  variable_del('basic_cart_user_message');
  variable_del('basic_cart_send_user_message');

  // Thank you messages.
  variable_del('basic_cart_thank_you_title');
  variable_del('basic_cart_thank_you_message');

  // Redirect user after add to cart action.
  variable_del('basic_cart_redirect_user_after_add_to_cart');

  // VAT default state and value.
  variable_del('basic_cart_vat_state');
  variable_del('basic_cart_vat_value');
}

/**
 * Adding Basic Cart new features (price, currency).
 */
function basic_cart_update_7200() {

  // Setting up the default currency.
  variable_set('basic_cart_currency', BASIC_CART_DEFAULT_CURRENCY);

  // Check to see if the price field already exists.
  $field = field_info_field('price');

  // If the price field does not exist then create it.
  if (empty($field)) {
    $field = array(
      'field_name' => 'price',
      'type' => 'number_decimal',
      'entity_types' => array(
        'node',
      ),
    );
    field_create_field($field);
  }

  // Getting the content types for which we have basic cart active.
  $node_types = variable_get('basic_cart_content_types');
  if (is_array($node_types)) {

    // Setting up the price field for the selected content types.
    foreach ($node_types as $type => $checked) {

      // If a node type is checked, then create the price field.
      if ($checked) {

        // Foreach checked content type, we must assign the price field to the content type.
        $instance = field_info_instance('node', 'price', $type);
        if (empty($instance)) {
          $instance = array(
            'field_name' => 'price',
            'label' => t('Price'),
            'description' => t('Please enter the price for this item.'),
            'entity_type' => 'node',
            'bundle' => $type,
          );

          // It doesn't exist. Create it.
          field_create_instance($instance);
        }
      }
      else {
        $instance = field_info_instance('node', 'price', $type);
        if (!empty($instance)) {
          field_delete_instance($instance);
        }
      }
    }
  }
}

/**
 * Updating the price field to number_decimal instead of text. 
 */
function basic_cart_update_7201() {
  $product_types = basic_cart_product_types();
  if (!empty($product_types)) {
    $backup_products = array();

    // Taking each content type.
    foreach ($product_types as $product_type => $is_active) {
      if (!empty($is_active)) {

        // Taking each product and storing it's initial value.
        $query = db_select('node', 'n')
          ->fields('n', array(
          'nid',
        ));
        $products = $query
          ->condition('type', $product_type, '=')
          ->execute()
          ->fetchAll();
        if (!empty($products) && is_array($products)) {
          foreach ($products as $i => $node) {
            $node = node_load($node->nid);
            $backup_products[$product_type][$node->nid] = $node;
          }
        }
      }
    }

    // Now deleting the price field.
    field_delete_field('price');

    // Using field_purge_batch() to actually delete the field.
    field_purge_batch(10);

    // Creating the new field with the right type property.
    $field = array(
      'field_name' => 'price',
      'type' => 'number_decimal',
      'entity_types' => array(
        'node',
      ),
    );
    field_create_field($field);

    // Restoring the content type instance and the node's initial value.
    if (!empty($backup_products)) {
      foreach ($backup_products as $product_type => $nodes) {

        // Restoring the field instance.
        $instance = array(
          'field_name' => 'price',
          'label' => t('Price'),
          'description' => t('Please enter the price for this item.'),
          'entity_type' => 'node',
          'bundle' => $product_type,
        );
        field_create_instance($instance);

        // Restoring the node's initial value.
        foreach ($nodes as $nid => $node) {
          node_save($node);
        }
      }
    }
  }

  // Redirect url after add to cart.
  variable_set('basic_cart_redirect_user_after_add_to_cart', '<none>');

  // Setting the VAT default state and value.
  variable_set('basic_cart_vat_state', BASIC_CART_VAT_STATE);
  variable_set('basic_cart_vat_value', BASIC_CART_VAT_VALUE);
}

/**
 * Adding the latest fields, in case they aren't already there.
 */
function basic_cart_update_7202() {

  // Adding the add to cart and/or price field.
  foreach (basic_cart_get_fields() as $field_name => $field_) {

    // Check to see if the field field already exists.
    $field = field_info_field($field_name);

    // If the field does not exist then create it.
    if (empty($field)) {
      $field = array(
        'field_name' => $field_name,
        'type' => $field_['type'],
        'entity_types' => array(
          'node',
        ),
      );
      field_create_field($field);
    }
  }

  // Getting the available content types.
  $content_types = basic_cart_product_types();

  // Creating the field instances.
  if (!empty($content_types) && is_array($content_types)) {
    foreach ($content_types as $type => $checked) {

      // If a node type is checked, then create the price field.
      if ($checked) {

        // Save content_type as a product.
        $product_types[$type] = $type;
        foreach (basic_cart_get_fields() as $field_name => $field_) {

          // Foreach checked content type, we must assign the price field to the content type.
          $instance = field_info_instance('node', $field_name, $type);
          if (empty($instance)) {
            $instance = array(
              'field_name' => $field_name,
              'label' => $field_['title'],
              'description' => $field_['description'],
              'entity_type' => 'node',
              'bundle' => $type,
            );

            // It doesn't exist. Create it.
            field_create_instance($instance);
          }
        }
      }
      else {
        foreach (basic_cart_get_fields() as $field_name => $field_) {
          $instance = field_info_instance('node', $field_name, $type);
          if (!empty($instance)) {
            field_delete_instance($instance);
          }
        }
      }
    }
  }
}

Functions

Namesort descending Description
basic_cart_install Implements hook_install().
basic_cart_uninstall Implements hook_uninstall().
basic_cart_update_7200 Adding Basic Cart new features (price, currency).
basic_cart_update_7201 Updating the price field to number_decimal instead of text.
basic_cart_update_7202 Adding the latest fields, in case they aren't already there.

Constants

Namesort descending Description
BASIC_CART_DEFAULT_CURRENCY Default currency.
BASIC_CART_VAT_STATE Default VAT state (enabled/disabled).
BASIC_CART_VAT_VALUE Default VAT value. Zero by default.