class Product in Commerce Migrate 8.2
Same name in this branch
- 8.2 modules/csv_example/src/Plugin/migrate/source/Product.php \Drupal\commerce_migrate_csv_example\Plugin\migrate\source\Product
- 8.2 modules/magento/src/Plugin/migrate/source/m2/Product.php \Drupal\commerce_migrate_magento\Plugin\migrate\source\m2\Product
- 8.2 modules/ubercart/src/Plugin/migrate/source/uc7/Product.php \Drupal\commerce_migrate_ubercart\Plugin\migrate\source\uc7\Product
- 8.2 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
- 3.1.x modules/magento/src/Plugin/migrate/source/m2/Product.php \Drupal\commerce_migrate_magento\Plugin\migrate\source\m2\Product
- 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
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.
File
- modules/
magento/ src/ Plugin/ migrate/ source/ m2/ Product.php, line 24
Namespace
Drupal\commerce_migrate_magento\Plugin\migrate\source\m2View 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Product:: |
protected | property | Product data from the source file. | |
Product:: |
protected | property | Product data. | |
Product:: |
private | function | Prepares an array of product SKU information. | |
Product:: |
private | function | Returns an array of product SKUs for the variations of this product. | |
Product:: |
public | function | Prepare one row of product data. | |
Product:: |
public | function |