public static function PriceListItemImportForm::batchProcess in Commerce Pricelist 8.2
Batch process to import price list items from the CSV.
Parameters
string $file_uri: The CSV file URI.
array $mapping: The mapping options.
array $csv_options: The CSV options.
string $price_list_id: The price list ID.
bool $delete_existing: The "delete existing" flag.
array $context: The batch context.
File
- src/
Form/ PriceListItemImportForm.php, line 317
Class
Namespace
Drupal\commerce_pricelist\FormCode
public static function batchProcess($file_uri, array $mapping, array $csv_options, $price_list_id, $delete_existing, array &$context) {
$entity_type_manager = \Drupal::entityTypeManager();
$price_list_storage = $entity_type_manager
->getStorage('commerce_pricelist');
$price_list_item_storage = $entity_type_manager
->getStorage('commerce_pricelist_item');
/** @var \Drupal\commerce_pricelist\Entity\PriceList $price_list */
$price_list = $price_list_storage
->load($price_list_id);
$purchasable_entity_storage = $entity_type_manager
->getStorage($price_list
->bundle());
$header_mapping = static::buildHeaderMapping($mapping);
ini_set("auto_detect_line_endings", TRUE);
$csv = new CsvFileObject($file_uri, TRUE, $header_mapping, $csv_options);
if (empty($context['sandbox'])) {
$context['sandbox']['import_total'] = (int) $csv
->count();
$context['sandbox']['import_count'] = 0;
$context['results']['import_created'] = 0;
$context['results']['import_updated'] = 0;
$context['results']['import_skipped'] = 0;
}
// The file is invalid, stop here.
if (!$csv
->valid()) {
$context['results']['error'] = t('The provided CSV file is invalid.');
$context['finished'] = 1;
return;
}
$import_total = $context['sandbox']['import_total'];
$import_count =& $context['sandbox']['import_count'];
$remaining = $import_total - $import_count;
$limit = $remaining < self::BATCH_SIZE ? $remaining : self::BATCH_SIZE;
$csv
->seek($import_count + 1);
for ($i = 0; $i < $limit; $i++) {
$row = $csv
->current();
$row = array_map('trim', $row);
// Skip the row if one of the required columns is empty.
foreach ([
'purchasable_entity',
'price',
'currency_code',
] as $required_column) {
if (empty($row[$required_column])) {
$context['results']['import_skipped']++;
$import_count++;
$csv
->next();
continue 2;
}
}
$purchasable_entity = $purchasable_entity_storage
->loadByProperties([
$mapping['purchasable_entity_column_type'] => $row['purchasable_entity'],
]);
$purchasable_entity = reset($purchasable_entity);
// Skip the row if the purchasable entity could not be loaded.
if (!$purchasable_entity) {
$context['results']['import_skipped']++;
$import_count++;
$csv
->next();
continue;
}
$quantity = !empty($row['quantity']) ? $row['quantity'] : '1';
// If existing price list items weren't deleted before the import,
// try to find one to update.
$price_list_item = NULL;
if (!$delete_existing) {
$result = $price_list_item_storage
->getQuery()
->condition('type', $price_list
->bundle())
->condition('price_list_id', $price_list
->id())
->condition('purchasable_entity', $purchasable_entity
->id())
->condition('quantity', $quantity)
->execute();
if (!empty($result)) {
$existing_price_list_item_id = reset($result);
$price_list_item = $price_list_item_storage
->load($existing_price_list_item_id);
assert($price_list_item instanceof PriceListItemInterface);
}
}
if (is_null($price_list_item)) {
// No price list item was found and updated, create a new one.
$price_list_item = $price_list_item_storage
->create([
'type' => $price_list
->bundle(),
'price_list_id' => $price_list
->id(),
'purchasable_entity' => $purchasable_entity
->id(),
'quantity' => $quantity,
]);
}
try {
static::processRow($row, $price_list_item);
} catch (\Exception $e) {
$context['results']['import_skipped']++;
$import_count++;
$csv
->next();
continue;
}
$import_type = $price_list_item
->isNew() ? 'created' : 'updated';
$price_list_item
->save();
$import_count++;
$context['results']['import_' . $import_type]++;
$csv
->next();
}
$context['message'] = t('Importing @created of @import_total price list items', [
'@created' => $import_count,
'@import_total' => $import_total,
]);
$context['finished'] = $import_count / $import_total;
}