You are here

commerce_gc_product.install in Commerce GC 7

Installs default giftcard product fields.

File

modules/commerce_gc_product/commerce_gc_product.install
View source
<?php

/**
 * @file
 * Installs default giftcard product fields.
 */

/**
 * Install default fields.
 */
function _commerce_gc_product_install_helper() {
  field_info_cache_clear();
  $fields = field_info_fields();
  $instances = field_info_instances();

  /*
   * Product giftcard value
   */
  if (empty($fields['commerce_gc_value'])) {

    // Create max uses field.
    $field = array(
      'field_name' => 'commerce_gc_value',
      'type' => 'commerce_price',
      'locked' => TRUE,
      'cardinality' => '1',
    );
    field_create_field($field);
  }
  if (empty($instances['commerce_product']['giftcard']['commerce_gc_value'])) {
    $instance = array(
      'field_name' => 'commerce_gc_value',
      'entity_type' => 'commerce_product',
      'bundle' => 'giftcard',
      'label' => t('Giftcard value'),
      'required' => TRUE,
    );
    field_create_instance($instance);
  }

  /*
   * Free product line items: Giftcard coupon reference
   */
  if (module_exists('commerce_discount')) {
    if (empty($fields['commerce_giftcards'])) {

      // Create giftcard reference field
      $field = array(
        'settings' => array(
          'target_type' => 'commerce_coupon',
          'handler' => 'base',
          'handler_settings' => array(
            'target_bundles' => array(
              'product_display' => 'giftcard_coupon',
            ),
          ),
        ),
        'field_name' => 'commerce_giftcards',
        'type' => 'entityreference',
        'locked' => TRUE,
        'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      );
      field_create_field($field);
    }
    if (empty($instances['commerce_line_item']['product_discount']['commerce_giftcards'])) {
      $instance = array(
        'label' => t('Purchased giftcards'),
        'widget' => array(
          'type' => 'entityreference_autocomplete',
          'weight' => '9',
          'settings' => array(
            'match_operator' => 'CONTAINS',
            'size' => 60,
            'path' => '',
          ),
        ),
        'field_name' => 'commerce_giftcards',
        'entity_type' => 'commerce_line_item',
        'bundle' => 'product_discount',
        'default_value' => NULL,
      );
      field_create_instance($instance);
    }
  }
}

/*
 * Implements hook_install().
 */
function commerce_gc_product_install() {

  // Add the giftcard product product type
  // Create the basic product type.
  drupal_load('module', 'commerce_product_ui');
  if (!commerce_product_type_load('giftcard')) {
    $product_type = commerce_product_ui_product_type_new();
    $product_type['type'] = 'giftcard';
    $product_type['name'] = t('Giftcard');
    $product_type['description'] = t('This product generates a giftcard when purchased.');
    $product_type['is_new'] = TRUE;
    commerce_product_ui_product_type_save($product_type, FALSE);
    _commerce_gc_product_install_helper();
  }
}

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

  // Remove field data for giftcard use line item:
  commerce_delete_instances('commerce_line_item', 'giftcard_purchase');

  // For giftcard products
  commerce_delete_instances('commerce_product', 'giftcard');

  // For purchased giftcard field on product discounts
  if ($instance = field_info_instance('commerce_line_item', 'commerce_giftcards', 'product_discount')) {
    commerce_delete_instance($instance);
  }
}