Skip to content

Instantly share code, notes, and snippets.

@samueljon
Created November 23, 2015 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samueljon/843137111fe5790e21c4 to your computer and use it in GitHub Desktop.
Save samueljon/843137111fe5790e21c4 to your computer and use it in GitHub Desktop.
class LsRetailProduct extends LsRetailVorurMigration {
public function __construct($arguments) {
parent::__construct($arguments);
$this->description = t('Import products from LS Retail.');
$this->dependencies = array(
'LsRetailCategoriesTop',
'LsRetailCategoriesSecond',
'LsRetailCategoriesThird',
);
$this->team = array(
new MigrateTeamMember('Kosmos og Kaos', 'samskipti@kosmosogkaos.is',
t('Product Owner')),
new MigrateTeamMember('Samúel Jón Gunnarsson', 'sammi@kosmosogkaos.is',
t('Implementor')),
);
// Create a map object for tracking the relationships between source rows
$this->map = new MigrateSQLMap($this->machineName,
array(
'No' => array(
'type' => 'varchar',
'length' => 45,
'not null' => TRUE,
),
),
MigrateDestinationEntityAPI::getKeySchema('commerce_product', 'product')
);
// Create a MigrateSource object.
//$this->source = new MigrateSourceCSV(drupal_get_path('module', 'lsretail_vorur') . '/import/products.csv', $this->csvcolumns(), array('header_rows' => 1), $this->fields());
$query = db_select('lsretail_integration_product', 'p')
->fields('p',
array(
'No',
'Blocked',
'Description',
'Description_2',
'Base_Unit_of_Measure',
'Item_Category_Code',
'Product_Group_Code',
'Inventory',
'Sales_Unit_of_Measure',
'Unit_Price_Including_VAT',
'Show_on_web',
'Hide_price_on_web',
'Inventory_control',
'Related_Items',
'Web_Item_Category',
'Product_Manager',
'Millifyrirsogn',
'Vorumyndir',
'BestDiscountPercent',
'BestDiscountPrice',
));
//$query->condition('Show_on_web','true',$operator='=');
$options = array('track_changes' => 1);
$this->source = new MigrateSourceSQL($query, $this->fields(), NULL, $options);
//$this->source = new MigrateSourceSQL($query);
//$this->destination = new MigrateDestinationEntityAPI('commerce_product', 'product');
$this->destination = new MigrateDestinationCommerceProduct('commerce_product', 'product');
$this->addFieldMapping('title', 'Description_2');
$this->addFieldMapping('sku', 'No');
$this->addFieldMapping('commerce_price', 'Unit_Price_Including_VAT');
$this->addFieldMapping('commerce_stock', 'Inventory');
// Images
$this->addFieldMapping('field_images', 'product_images');
$this->addFieldMapping('field_images:file_replace')->defaultValue(FILE_EXISTS_REPLACE);
$this->addFieldMapping('field_images:source_dir')->defaultValue('/mnt/Myndasafn');
$this->addFieldMapping('uid', 'uid');
$this->addFieldMapping('language', 'language');
}
function fields() {
return array(
'product_images' => 'An array of images, populated during prepareRow().',
);
}
function prepareRow($row) {
$row->product_images = explode(', ', $row->Vorumyndir);
$row->uid = 1;
$row->status = 1;
$row->language = LANGUAGE_NONE;
//$this->addOrUpdateRules($row->Description_2,$row->BestDiscountPercent, $row->No);
$debug=FALSE;
if($debug){
watchdog('lsretail_vorur', t('Importing %sku'), array('%sku'=>$row->No), WATCHDOG_INFO);
}
}
/**
* SJG: 20150927
* Here we are disabling products and product_display nodes as a precaution.
* Afterwards in deleteStaleProducts() we delete all products and product_display nodes that can be deleted.
* The remaining products and product_display are items that cannot be deleted if they have
* a line item in order history are now unpublished and not visible in the store front.
*/
public function preImport() {
parent::preImport();
// Code to execute before first article row is imported
// from lsretail_integration
deleteStaleProducts();
$foo = 'bar';
}
public function postImport(){
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment