View source
<?php
namespace Drupal\commerce_migrate_commerce\Plugin\migrate\source\commerce1;
use CommerceGuys\Intl\Currency\CurrencyRepository;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_store\Resolver\DefaultStoreResolver;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Order extends FieldableEntity {
protected $defaultStoreResolver;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityTypeManagerInterface $entity_type_manager, DefaultStoreResolver $default_store_resolver) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_type_manager);
$this->defaultStoreResolver = $default_store_resolver;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('state'), $container
->get('entity_type.manager'), $container
->get('commerce_store.default_store_resolver'));
}
public function fields() {
return [
'order_id' => t('Order ID'),
'commerce_order_total' => t('Order Number'),
'revision_id' => t('Revision ID'),
'type' => t('Type'),
'uid' => t('User ID'),
'mail' => t('Email'),
'status' => t('Status'),
'created' => t('Created'),
'changed' => t('Changed'),
'default_store_id' => t('Default store id'),
'refresh_state' => t('Order refresh state'),
'hostname' => t('Hostname'),
'data' => t('Data'),
];
}
public function getIds() {
$ids['order_id']['type'] = 'integer';
return $ids;
}
public function query() {
return $this
->select('commerce_order', 'ord')
->fields('ord');
}
public function prepareRow(Row $row) {
$row
->setSourceProperty('refresh_state', OrderInterface::REFRESH_SKIP);
$default_store = $this->defaultStoreResolver
->resolve();
if ($default_store) {
$row
->setSourceProperty('default_store_id', $default_store
->id());
}
else {
throw new MigrateException('You must have a store saved in order to import orders.');
}
$order_id = $row
->getSourceProperty('order_id');
$revision_id = $row
->getSourceProperty('revision_id');
foreach (array_keys($this
->getFields('commerce_order', $row
->getSourceProperty('type'))) as $field) {
$row
->setSourceProperty($field, $this
->getFieldValues('commerce_order', $field, $order_id, $revision_id));
}
$currencyRepository = new CurrencyRepository();
$value = $row
->getSourceProperty('commerce_order_total');
$currency_code = $value[0]['currency_code'];
$value[0]['fraction_digits'] = $currencyRepository
->get($currency_code)
->getFractionDigits();
$row
->setSourceProperty('commerce_order_total', $value);
$row
->setSourceProperty('data', unserialize($row
->getSourceProperty('data')));
$query = $this
->select('commerce_line_item', 'cli')
->fields('cli')
->condition('cli.order_id', $order_id)
->condition('cli.type', 'shipping');
$shipping_line_items = $query
->execute()
->fetchAll();
foreach ($shipping_line_items as $key => $shipping_line_item) {
$line_item_id = $shipping_line_item['line_item_id'];
foreach (array_keys($this
->getFields('commerce_line_item', 'shipping')) as $field) {
$shipping_line_items[$key][$field] = $this
->getFieldValues('commerce_line_item', $field, $line_item_id, $line_item_id);
}
$shipping_line_items[$key]['data'] = unserialize($shipping_line_item['data']);
$currency_code = $shipping_line_items[$key]['commerce_total'][0]['currency_code'];
$shipping_line_items[$key]['commerce_total'][0]['fraction_digits'] = $currencyRepository
->get($currency_code)
->getFractionDigits();
}
$row
->setSourceProperty('shipping_line_items', $shipping_line_items);
return parent::prepareRow($row);
}
public function getFieldValues($entity_type, $field, $entity_id, $revision_id = NULL, $language = NULL) {
$values = parent::getFieldValues($entity_type, $field, $entity_id, $revision_id, $language);
foreach ($values as $key => &$value) {
if (isset($value['data'])) {
$values[$key]['data'] = unserialize($value['data']);
}
}
return $values;
}
}