ShippingFlatRate.php 1.79 KB
Newer Older
1 2
<?php

3
namespace Drupal\commerce_migrate_commerce\Plugin\migrate\source\commerce1;
4 5 6 7 8 9 10 11 12

use CommerceGuys\Intl\Currency\CurrencyRepository;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;

/**
 * Gets the flat rate shipping service.
 *
 * @MigrateSource(
13
 *   id = "commerce1_shipping_flat_rate",
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
 *   source_module = "commerce_order"
 * )
 */
class ShippingFlatRate extends DrupalSqlBase {

  /**
   * {@inheritdoc}
   */
  public function query() {
    $query = $this->select('commerce_flat_rate_service', 'fr')
      ->fields('fr');
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    return [
      'name' => t('Flat rate service name'),
      'title' => t('Flat rate title'),
      'display_title' => t('Flat rate display title'),
      'description' => t('Description'),
      'rules_component' => t('Rules component'),
      'amount' => t('Amount'),
      'number' => t('The amount converted to a Commerce price amount'),
      'currency_code' => t('Currency code'),
      'data' => t('Shipping data'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    $row->setSourceProperty('data', unserialize($row->getSourceProperty('data')));

    // Include the number of currency fraction digits in the rate.
    $currencyRepository = new CurrencyRepository();
    $currency_code = $row->getSourceProperty('currency_code');
    $fraction_digits = $currencyRepository->get($currency_code)->getFractionDigits();
    $number = bcdiv($row->getSourceProperty('amount'), bcpow(10, $fraction_digits), $fraction_digits);
    $row->setSourceProperty('number', $number);
    return parent::prepareRow($row);
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    $ids['name']['type'] = 'string';
    return $ids;
  }

}