You are here

commerce_gc_product_example.module in Commerce GC 7

File

modules/commerce_gc_product_example/commerce_gc_product_example.module
View source
<?php

/**
 * @file
 * Code for the Commerce giftcard product example feature.
 */
include_once 'commerce_gc_product_example.features.inc';

/*
 * Implements hook_modules_enabled().
 */
function commerce_gc_product_example_modules_enabled($modules) {
  if (in_array('commerce_gc_product_example', $modules) && !variable_get('commerce_gc_product_example_added_content')) {
    _commerce_gc_product_example_ensure_config();
    field_info_cache_clear();

    // Create the new product.
    $product = commerce_product_new('giftcard');
    $product->sku = 'giftcard-1';
    $product->title = 'Giftcard 1';
    $product->language = LANGUAGE_NONE;
    $product->uid = 1;

    // Set a default price.
    $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
    $price = array(
      'amount' => 1000,
      'currency_code' => 'USD',
    );
    $product_wrapper->commerce_gc_value = $price;
    $product_wrapper->commerce_price = $price;

    // Save it.
    commerce_product_save($product);

    // Create a sample product and node display
    $node = (object) array(
      'type' => 'giftcard_product_display',
    );
    node_object_prepare($node);
    $node->title = $product->title;
    $node->uid = 1;

    // Reference the product we just made.
    // $node->field_product[LANGUAGE_NONE][]['product_id'] = $product->product_id;
    $node_wrapper = entity_metadata_wrapper('node', $node);
    $node_wrapper->field_product[] = $product->product_id;

    // Make sure we set the default language
    $node->language = LANGUAGE_NONE;

    // Save it and retain a copy.
    node_save($node);
    variable_set('commerce_gc_product_example_added_content', TRUE);
  }
}

/**
 * Ensure that certain configuration elements actually exist.
 */
function _commerce_gc_product_example_ensure_config() {
  $module = 'commerce_gc_product_example';

  // Copied from features_modules_enabled():
  $feature = features_load_feature($module, TRUE);
  $items[$module] = array_keys($feature->info['features']);

  // Need to include any new files.
  features_include_defaults(NULL, TRUE);
  _features_restore('enable', $items);

  // Rebuild the list of features includes.
  features_include(TRUE);

  // Reorders components to match hook order and removes non-existant.
  $all_components = array_keys(features_get_components());
  foreach ($items as $module => $components) {
    $items[$module] = array_intersect($all_components, $components);
  }
  _features_restore('rebuild', $items);

  // This is necessary to ensure that giftcard products have a price when we are
  // setting it.
  commerce_product_configure_product_type('giftcard');
}

Functions

Namesort descending Description
commerce_gc_product_example_modules_enabled
_commerce_gc_product_example_ensure_config Ensure that certain configuration elements actually exist.