View source
<?php
namespace Drupal\commerce_migrate_ubercart\Plugin\migrate\source;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
use Drupal\migrate\Row;
class OrderProduct extends DrupalSqlBase {
public function query() {
$query = $this
->select('uc_order_products', 'uop')
->fields('uop', [
'order_product_id',
'order_id',
'nid',
'title',
'qty',
'price',
'data',
]);
$query
->innerJoin('uc_orders', 'uo', 'uop.order_id = uo.order_id');
$query
->fields('uo', [
'order_total',
'created',
'modified',
]);
$query
->orderBy('order_product_id');
if ($this
->getDatabase()
->schema()
->fieldExists('uc_orders', 'currency')) {
$query
->addField('uo', 'currency');
}
else {
$currency_code = $this
->variableGet('uc_currency_code', 'USD');
$query
->addExpression(':currency_code', 'currency', [
':currency_code' => $currency_code,
]);
}
return $query;
}
public function fields() {
$fields = [
'order_product_id' => $this
->t('Line item ID'),
'order_id' => $this
->t('Order ID'),
'nid' => $this
->t('Product ID'),
'title' => $this
->t('Product name'),
'qty' => $this
->t('Quantity sold'),
'price' => $this
->t('Price of product sold'),
'data' => $this
->t('Order line item data'),
'created' => $this
->t('Created timestamp, from the order'),
'modified' => $this
->t('Modified timestamp, from the order'),
'currency' => $this
->t("Currency, default to USD'"),
];
return $fields;
}
public function prepareRow(Row $row) {
$data = unserialize($row
->getSourceProperty('data'));
unset($data['module']);
$row
->setSourceProperty('data', $data);
$row
->setSourceProperty('adjustments', $this
->getAdjustmentData($row));
return parent::prepareRow($row);
}
public function getIds() {
return [
'order_product_id' => [
'type' => 'integer',
'alias' => 'uop',
],
];
}
protected function getAdjustmentData(Row $row) {
$order_id = $row
->getSourceProperty('order_id');
$query = $this
->select('uc_order_line_items', 'uol')
->fields('uol')
->fields('uo', [
'order_id',
])
->orderBy('weight', 'ASC')
->condition('uol.order_id', $order_id)
->condition('type', 'shipping', '!=');
$query
->innerJoin('uc_orders', 'uo', 'uol.order_id = uo.order_id');
$adjustments = $query
->execute()
->fetchAll();
$currency_code = $row
->getSourceProperty('currency');
foreach ($adjustments as &$adjustment) {
$adjustment['currency_code'] = $currency_code;
$adjustment['data'] = unserialize($adjustment['data']);
}
$query = $this
->select('uc_order_products', 'uop')
->condition('uop.order_id', $order_id);
$query
->addExpression('COUNT(order_id)', 'num_product_line');
$query
->addExpression('MAX(order_product_id)', 'max_order_product_id');
$results = $query
->execute()
->fetchAll();
$row
->setSourceProperty('num_product_line', $results[0]['num_product_line']);
$row
->setSourceProperty('max_order_product_id', $results[0]['max_order_product_id']);
return $adjustments;
}
}