You are here

commerce_kickstart_lite_product.migrate.inc in Commerce Kickstart 7.2

Migrations for commerce_kickstart_lite_product.

File

modules/commerce_kickstart/commerce_kickstart_lite_product/commerce_kickstart_lite_product.migrate.inc
View source
<?php

/**
 * @file
 * Migrations for commerce_kickstart_lite_product.
 */

/**
 * Class CommerceKickstartProduct.
 */
class CommerceKickstartProduct extends CommerceKickstartMigration {

  /**
   * {@inheritdoc}
   */
  public function __construct($arguments) {
    parent::__construct($arguments);
    $this->description = t('Import products.');

    // Create a map object for tracking the relationships between source rows
    $this->map = new MigrateSQLMap($this->machineName, array(
      'sku' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
    ), MigrateDestinationEntityAPI::getKeySchema('commerce_product'));

    // Create a MigrateSource object.
    $this->source = new MigrateSourceCSV(drupal_get_path('module', 'commerce_kickstart_lite_product') . '/import/products.csv', $this
      ->csvcolumns(), array(
      'header_rows' => 1,
    ), $this
      ->fields());
    $this->destination = new MigrateDestinationEntityAPI('commerce_product', 'product');
    $this
      ->addFieldMapping('title', 'title');
    $this
      ->addFieldMapping('sku', 'sku');
    $this
      ->addFieldMapping('commerce_price', 'price');

    // Images
    $this
      ->addFieldMapping('field_images', 'product_images');
    $this
      ->addFieldMapping('field_images:file_replace')
      ->defaultValue(FILE_EXISTS_REPLACE);
    $this
      ->addFieldMapping('field_images:source_dir')
      ->defaultValue(drupal_get_path('module', 'commerce_kickstart_lite_product') . '/import/images');
    $this
      ->addFieldMapping('uid', 'uid');
    $this
      ->addFieldMapping('language', 'language');
  }

  /**
   * Maps CSV column headers.
   *
   * @return array
   *    Array of mappings
   */
  public function csvcolumns() {
    $columns[0] = array(
      'title',
      'Title',
    );
    $columns[1] = array(
      'sku',
      'SKU',
    );
    $columns[2] = array(
      'price',
      'Price',
    );
    $columns[3] = array(
      'images',
      'Images',
    );
    $columns[4] = array(
      'category',
      'Category',
    );
    return $columns;
  }

  /**
   * Returns mapping for fields.
   *
   * @return array
   *    Array of fields names and description.
   */
  public function fields() {
    return array(
      'product_images' => 'An array of images, populated during prepareRow().',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow($row) {
    $row->product_images = explode(', ', $row->images);
    $row->uid = 1;
    $row->language = LANGUAGE_NONE;
  }

}

/**
 * Class CommerceKickstartNode.
 */
class CommerceKickstartNode extends CommerceKickstartMigration {

  /**
   * {@inheritdoc}
   */
  public function __construct($arguments) {
    parent::__construct($arguments);
    $this->description = t('Import product nodes.');
    $this->dependencies = array(
      'CommerceKickstartProduct',
    );

    // Create a map object for tracking the relationships between source rows
    $this->map = new MigrateSQLMap($this->machineName, array(
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
    ), MigrateDestinationNode::getKeySchema());

    // Create a MigrateSource object.
    $this->source = new MigrateSourceCSV(drupal_get_path('module', 'commerce_kickstart_lite_product') . '/import/nodes.csv', $this
      ->csvcolumns(), array(
      'header_rows' => 1,
    ));
    $this->destination = new MigrateDestinationNode('product_display');
    $this
      ->addFieldMapping('uid', 'uid')
      ->defaultValue(1);
    $this
      ->addFieldMapping('title', 'title');
    $this
      ->addFieldMapping('body', 'description');
    $this
      ->addFieldMapping('body:summary', 'excerpt');
    $this
      ->addFieldMapping('field_product_category', 'category');
    $this
      ->addFieldMapping('field_product_category:create_term')
      ->defaultValue(TRUE);
    $this
      ->addFieldMapping('field_product', 'skus');
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow($row) {
    $products = array();
    foreach (explode(', ', $row->sku) as $sku) {
      $product = commerce_product_load_by_sku($sku);
      $products[] = $product->product_id;
    }
    $row->skus = $products;
  }

  /**
   * Maps CSV column headers.
   *
   * @return array
   *    Array of mappings
   */
  function csvcolumns() {
    $columns[0] = array(
      'title',
      'Title',
    );
    $columns[1] = array(
      'description',
      'Description',
    );
    $columns[2] = array(
      'category',
      'Category',
    );
    $columns[3] = array(
      'sku',
      'SKU',
    );
    return $columns;
  }

}

Classes

Namesort descending Description
CommerceKickstartNode Class CommerceKickstartNode.
CommerceKickstartProduct Class CommerceKickstartProduct.