You are here

class Product in Commerce Migrate 3.1.x

Same name in this branch
  1. 3.1.x modules/csv_example/src/Plugin/migrate/source/Product.php \Drupal\commerce_migrate_csv_example\Plugin\migrate\source\Product
  2. 3.1.x modules/magento/src/Plugin/migrate/source/m2/Product.php \Drupal\commerce_migrate_magento\Plugin\migrate\source\m2\Product
  3. 3.1.x modules/ubercart/src/Plugin/migrate/source/uc7/Product.php \Drupal\commerce_migrate_ubercart\Plugin\migrate\source\uc7\Product
  4. 3.1.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/magento/src/Plugin/migrate/source/m2/Product.php \Drupal\commerce_migrate_magento\Plugin\migrate\source\m2\Product
  2. 3.0.x modules/magento/src/Plugin/migrate/source/m2/Product.php \Drupal\commerce_migrate_magento\Plugin\migrate\source\m2\Product

Gets the product rows.

The standard CSV export does not include any link between a configurable product and the associated simple products. The 'bundle' type and the bundle products are migrated as individual products.

Assumptions:

  • The product SKUs of a configurable product and the products have the same

pattern or root. The root is used to search for the associated products.

  • WH09-S-Purple: The root is 'WH09'
  • A configurable product has at least one simple product.

Plugin annotation


@MigrateSource(
  id = "product_csv"
)

Hierarchy

  • class \Drupal\commerce_migrate_magento\Plugin\migrate\source\m2\Product extends \Drupal\migrate_source_csv\Plugin\migrate\source\CSV

Expanded class hierarchy of Product

20 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
MigrateUpgradeReviewPageTest::getAvailablePaths in modules/ubercart/tests/src/Functional/uc7/MigrateUpgradeReviewPageTest.php
Gets the available upgrade paths.
MigrateUpgradeReviewPageTest::getAvailablePaths in modules/ubercart/tests/src/Functional/uc6/MigrateUpgradeReviewPageTest.php
Gets the available upgrade paths.

... See full list

File

modules/magento/src/Plugin/migrate/source/m2/Product.php, line 24

Namespace

Drupal\commerce_migrate_magento\Plugin\migrate\source\m2
View source
class Product extends CSV {

  /**
   * Product data.
   *
   * @var array
   */
  protected $productData = [];

  /**
   * Product data from the source file.
   *
   * An array of product SKUs with two keys, 'configurable' and 'all'.
   * Configurable is an array of all the SKUs for product of type
   * 'configurable' * and 'all' is a list of all SKUs.
   *
   * @var array
   */
  protected $fileData = [];

  /**
   * {@inheritdoc}
   */
  public function initializeIterator() {
    $file = parent::initializeIterator();
    if (empty($this->productData)) {
      $this->fileData = $this
        ->getFileData($file);
      $file = parent::initializeIterator();
    }
    return $this
      ->getYield($file);
  }

  /**
   * Prepare one row of product data.
   *
   * @param \Generator $file
   *   The source CSV file object.
   *
   * @codingStandardsIgnoreStart
   *
   * @return \Generator
   *   A new row, one for each unique vocabulary.
   *
   * @codingStandardsIgnoreEnd
   */
  public function getYield(\Generator $file) {
    foreach ($file as $row) {
      $variations = $this
        ->getVariations($row);
      $count = count($variations);

      // Yield a row if this product has one variation or if it configurable and
      // has more than product variation.
      if ($count === 1 || $count > 1 && $row['product_type'] === 'configurable') {

        // Get the variations and write.
        $new_row = $row;
        $new_row['variations'] = $variations;
        (yield $new_row);
      }
    }
  }

  /**
   * Returns an array of product SKUs for the variations of this product.
   *
   * If this product is a variation the return array contains the SKUs for all
   * product variations of the product.
   *
   * @param array $row
   *   The current row.
   *
   * @return array
   *   An array of variation SKUs.
   */
  private function getVariations(array $row) {
    static $searched = [];
    $search_sku = strstr($row['sku'], '-', TRUE);
    if (!$search_sku) {
      $search_sku = $row['sku'];
    }
    if (isset($searched[$search_sku])) {
      $variations = $searched[$search_sku];
    }
    else {
      if (ctype_alpha($search_sku[0])) {
        $pattern = "/" . $search_sku . ".*/";
        $subject = $this->fileData['all_sku'];
        $variations = preg_grep($pattern, $subject);

        // Exclude the search pattern, it is likely the 'configurable' product
        // SKU.
        $variations = array_diff($variations, [
          $search_sku,
        ]);
        $searched[$search_sku] = $variations;
      }
      else {
        $variations = [
          $row['sku'],
        ];
      }
    }
    return $variations;
  }

  /**
   * Prepares an array of product SKU information.
   *
   * @param \Generator $file
   *   The file object for the CSV file being processed.
   *
   * @return array
   *   An array of product SKUs with two keys, 'configurable' and 'all'.
   *   Configurable is an array of all the SKUs for product of type
   *   'configurable' * and 'all' is a list of all SKUs.
   */
  private function getFileData(\Generator $file) {
    $file_data = [];
    if (!$this->fileData) {
      $file_data = [];
      foreach ($file as $row) {
        if ($row['product_type'] === 'configurable') {
          $file_data['configurable'][] = $row['sku'];
        }
        $file_data['all_sku'][] = $row['sku'];
      }
    }
    return $file_data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Product::$fileData protected property Product data from the source file.
Product::$productData protected property Product data.
Product::getFileData private function Prepares an array of product SKU information.
Product::getVariations private function Returns an array of product SKUs for the variations of this product.
Product::getYield public function Prepare one row of product data.
Product::initializeIterator public function