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/csv_example/src/Plugin/migrate/source/Product.php \Drupal\commerce_migrate_csv_example\Plugin\migrate\source\Product
- 3.0.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.
File
- modules/
csv_example/ src/ Plugin/ migrate/ source/ Product.php, line 20
Namespace
Drupal\commerce_migrate_csv_example\Plugin\migrate\sourceView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Product:: |
protected | property | The file object that reads the CSV file. | |
Product:: |
protected | function | Builds a product row including an array of variation SKUs for this product. | |
Product:: |
public | function | ||
Product:: |
public | function |