You are here

class Product in Commerce Migrate 3.0.x

Same name in this branch
  1. 3.0.x modules/csv_example/src/Plugin/migrate/source/Product.php \Drupal\commerce_migrate_csv_example\Plugin\migrate\source\Product
  2. 3.0.x modules/magento/src/Plugin/migrate/source/m2/Product.php \Drupal\commerce_migrate_magento\Plugin\migrate\source\m2\Product
  3. 3.0.x modules/ubercart/src/Plugin/migrate/source/uc7/Product.php \Drupal\commerce_migrate_ubercart\Plugin\migrate\source\uc7\Product
  4. 3.0.x modules/ubercart/src/Plugin/migrate/source/uc6/Product.php \Drupal\commerce_migrate_ubercart\Plugin\migrate\source\uc6\Product
Same name and namespace in other branches
  1. 8.2 modules/csv_example/src/Plugin/migrate/source/Product.php \Drupal\commerce_migrate_csv_example\Plugin\migrate\source\Product
  2. 3.1.x modules/csv_example/src/Plugin/migrate/source/Product.php \Drupal\commerce_migrate_csv_example\Plugin\migrate\source\Product

Builds the product rows.

Each row in the source CSV contains one product variation with a unique SKU. And all variations for a product have the same title. Using the title field as the key, create a set of product rows that has the variation SKUs as an array.

Plugin annotation


@MigrateSource(
  id = "csv_example_product"
)

Hierarchy

  • class \Drupal\commerce_migrate_csv_example\Plugin\migrate\source\Product extends \Drupal\migrate_source_csv\Plugin\migrate\source\CSV

Expanded class hierarchy of Product

17 string references to 'Product'
FieldTest::providerSource in modules/ubercart/tests/src/Kernel/Plugin/migrate/source/uc7/FieldTest.php
The data provider.
FieldTest::providerSource in modules/commerce/tests/src/Kernel/Plugin/migrate/source/commerce1/FieldTest.php
The data provider.
magento2_product.yml in modules/magento/migrations/magento2_product.yml
modules/magento/migrations/magento2_product.yml
ProductTest::providerSource in modules/ubercart/tests/src/Kernel/Plugin/migrate/source/uc6/ProductTest.php
The data provider.
ProductTypeTest::providerSource in modules/ubercart/tests/src/Kernel/Plugin/migrate/source/uc7/ProductTypeTest.php
The data provider.

... See full list

File

modules/csv_example/src/Plugin/migrate/source/Product.php, line 20

Namespace

Drupal\commerce_migrate_csv_example\Plugin\migrate\source
View source
class Product extends CSV {

  /**
   * The file object that reads the CSV file.
   *
   * @var \SplFileObject
   */
  protected $file = NULL;

  /**
   * {@inheritdoc}
   */
  public function initializeIterator() {
    $file = parent::initializeIterator();

    // Get the product rows.
    $rows = $this
      ->getProductsWithVariations($file);
    return new \ArrayIterator($rows);
  }

  /**
   * Builds a product row including an array of variation SKUs for this product.
   *
   * @param \Generator $file
   *   The source CSV file object.
   *
   * @return array
   *   The product array.
   */
  protected function getProductsWithVariations(\Generator $file) {

    // Initialize the new row.
    $new_row = [];
    foreach ($file as $line) {
      if (array_key_exists($line['title'], $new_row)) {

        // The new_row has a row for this product, add this SKU.
        $new_row[$line['title']]['variation_sku'][] = trim($line['sku']);
      }
      else {

        // This is a new product, initialize the new_row.
        $line['variation_sku'][] = trim($line['sku']);
        $new_row[$line['title']] = $line;
      }
    }
    return $new_row;
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {

    // Trim all the source values.
    foreach ($row
      ->getSource() as $key => $value) {
      if (is_string($value)) {
        $row
          ->setSourceProperty($key, trim($value));
      }
    }
    return parent::prepareRow($row);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Product::$file protected property The file object that reads the CSV file.
Product::getProductsWithVariations protected function Builds a product row including an array of variation SKUs for this product.
Product::initializeIterator public function
Product::prepareRow public function