1
Answers
Vote up!
0
Vote down!

Adding a fee to order during migration

I'm basically migrating from Ubercart which applied fee to orders but these fees are added on Commerce using the pricing rules.

Once I've migrated my orders it's not displaying the fee which I'd like to include for historical purposes

Is there a way I can programmatically add the fee to the order during the migration, or should I being doing this during the import of products?

Asked by: dottodotdesign
on June 25, 2013

1 Answer

Vote up!
0
Vote down!

Just managed to figure it out by adding the fee to the line item and on complete loading and saving the order.

function prepare($line_item, stdClass $row) {
    $line_item->type = 'product';

    // Add a base price component.
    $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
    $price = $line_item_wrapper->commerce_unit_price;
    $base_price = array(
      'amount' => $price->amount->value(),
      'currency_code' => $price->currency_code->value(),
      'data' => array(),
    );
    $booking_fee = array(
      'amount' => '100',
      'currency_code' => $price->currency_code->value(),
      'data' => array(),
    );
    $line_item_wrapper->commerce_unit_price->data = commerce_price_component_add($base_price, 'booking_fee', $booking_fee, TRUE);

  }

  /**
   * A line item has been saved. Reference it from the order.
   */
  function complete($line_item, stdClass $row) {
    $order_id = $line_item->order_id['destid1'];
    $delta = db_query("SELECT COUNT(entity_id) FROM {field_data_commerce_line_items} WHERE entity_id = :order_id",
                      array(':order_id' => $order_id))->fetchField();

    db_insert('field_data_commerce_line_items')
      ->fields(array(
        'entity_id' => $order_id,
        'revision_id' => $order_id,
        'entity_type' => 'commerce_order',
        'bundle' => 'commerce_order',
        'deleted' => 0,
        'language' => LANGUAGE_NONE,
        'delta' => $delta,
        'commerce_line_items_line_item_id' => $line_item->line_item_id,
      ))
      ->execute();
      $order = commerce_order_load($order_id);
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  commerce_order_save($order_wrapper->value());
  }
Answer by: dottodotdesign
Posted: Jun 28, 2013