MigrateDestinationCommerceProduct

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

The contrib module Commerce Migrate provides a few destination classes that extend the Entity API destination class provided by Migrate Extras.

When declaring the destination map, use "commerce_product" as the entity type (which is defined by the Commerce module suite), and "my_product_type" as the bundle type.

The following class imports commerce products. "sku" and "commerce_price" mappings are relevant to products. In the following example, "my_product_type" is the machine name of the commerce product type (defined via UI config), "import_database" is the import database name (defined in settings.php), and "import_data" is the table in that database where the import data exists. In the source map, "ItemNumber" is the unique identifier column in the table.

class AseEndoMigration extends Migration {
  /**
   * Doc comment.
   */
  public function __construct() {
    parent::__construct();

    // The defintion of the columns.
    $columns = array(
      'Category',
      'Name',
      'ItemNumber',
      'Description',
      'USRetailPrice',
    );

    // Entity type, and bundle.
    $this->destination = new MigrateDestinationCommerceProduct('commerce_product', 'my_product_type');

    // Select all endodontic products.
    $query = Database::getConnection('default', 'import_database')
             ->select('import_data', 'd')
             ->fields('d', $columns)
             ->condition('Category', 'ENDO');

    $this->source = new MigrateSourceSQL($query);

    // Source and destination relation for rollbacks.
    $this->map = new MigrateSQLMap(
      $this->machineName,
      array(
        'ItemNumber' => array(
          'type' => 'varchar',
          'length' => 64,
          'not null' => TRUE,
        ),
      ),
      MigrateDestinationCommerceProduct::getKeySchema('commerce_product')
    );

    $this->addFieldMapping('sku', 'ItemNumber');
    $this->addFieldMapping('title', 'Name');
    $this->addFieldMapping('field_part_description', 'Description');
    $this->addFieldMapping('commerce_price', 'USRetailPrice');

  }
}

Help improve this page

Page status: Not set

You can: