Skip to content

Instantly share code, notes, and snippets.

@theodorosploumis
Last active August 29, 2015 14:11
Show Gist options
  • Save theodorosploumis/0a90de2dd81e1288dd6d to your computer and use it in GitHub Desktop.
Save theodorosploumis/0a90de2dd81e1288dd6d to your computer and use it in GitHub Desktop.
Drupal 7: field_data_commerce_line_items table is empty after Feeds import.
/*
* Drupal 7: Using the latest EXPERIMENTAL Feeds module (7.x-2.x-dev from 2014-Dec-16) to import Commerce orders and line items the line items are not attached on each order.
* Technically, the database table "field_data_commerce_line_items" that keeps the reference for each order is empty
* and values only exist on "commerce_line_item" table. We are using Feeds to import data for "Orders" as also as "Product Line Items".
* With this simple function we fill up the "field_data_commerce_line_items" table from "commerce_line_item" table.
*/
// Update line items after feeds import (so table field_data_commerce_line_items has values)
function MYMODULE_update_product_line_items() {
// Get required values from table commerce_line_item
$result = db_query(
'SELECT c.line_item_id, c.order_id
FROM {commerce_line_item} c
WHERE c.type = :type',
array(':type' => 'product')
);
foreach ($result as $record) {
// $record->nid, $record->title, $record->created
$order_id = $record->order_id;
$line_item_id = $record->line_item_id;
if ($order_id && $line_item_id) {
// Get order from order_id
$order = commerce_order_load($order_id);
// Get line_item from line_item_id
$line_item = commerce_line_item_load($line_item_id);
// Load order object
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// Add line_item to the order
$order_wrapper->commerce_line_items[] = $line_item;
// Save order. This will fill the empty field_data_commerce_line_items
commerce_order_save($order);
}
}
drupal_exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment