Created
November 23, 2015 19:28
-
-
Save samueljon/343db834dab411b81a9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LsRetailVariant extends LsRetailVorurMigration { | |
private function getVariantType($fieldName){ | |
switch($fieldName){ | |
//Afbrigði | |
case 'AFBRIGÐI': | |
return 'field_nav_afbrigdi'; | |
break; | |
//Litur | |
case 'COLOR': | |
case 'LITUR': | |
return 'field_nav_litur'; | |
break; | |
//Styrkur | |
case 'STYRKUR': | |
return 'field_nav_styrkur'; | |
break; | |
//Stærð | |
case 'INLEGG NR': | |
case 'NÚMER': | |
case 'SIZENO': | |
case 'STÆRÐ': | |
return 'field_nav_staerd'; | |
break; | |
} | |
return null; | |
} | |
public function __construct($arguments) { | |
parent::__construct($arguments); | |
$this->description = t('Import product variants from LS Retail.'); | |
$this->dependencies = array( | |
'LsRetailCategoriesTop', | |
'LsRetailCategoriesSecond', | |
'LsRetailCategoriesThird', | |
'LsRetailProduct', | |
); | |
$this->team = array( | |
new MigrateTeamMember('Kosmos og Kaos', '[email protected]', | |
t('Product Owner')), | |
new MigrateTeamMember('Samúel Jón Gunnarsson', '[email protected]', | |
t('Implementor')), | |
); | |
// Create a map object for tracking the relationships between source rows | |
$this->map = new MigrateSQLMap($this->machineName, | |
array( | |
'Barcode' => array( | |
'type' => 'varchar', | |
'length' => 255, | |
'not null' => TRUE, | |
), | |
), | |
MigrateDestinationEntityAPI::getKeySchema('commerce_product', 'product') | |
); | |
// Create a MigrateSource object. | |
$query = db_select('lsretail_integration_product_itemvar', 'p') | |
->fields('p', | |
array( | |
'kkey', | |
'Item_No', | |
'Framework_Code', | |
'Variant_Dimension_1', | |
'Variant_Dimension_2', | |
'Variant_Dimension_3', | |
'Variant_Dimension_4', | |
'Variant_Dimension_5', | |
'Variant_Dimension_6', | |
'VarDimname1', | |
'VarDimname2', | |
'VarDimname3', | |
'VarDimname4', | |
'VarDimname5', | |
'VarDimname6', | |
'Variant', | |
'Barcode', | |
'Date_Filter', | |
'Location_Filter', | |
'Qty_Sold', | |
'Sales_LCY', | |
'COGS_LCY', | |
'Description', | |
'Inventory', | |
)); | |
$query->orderBy('Item_No', 'ASC'); | |
$query->isNotNull('Barcode'); | |
$query->innerJoin('lsretail_integration_product', 'ls_prods', 'p.Item_No = ls_prods.No'); | |
$query->addField('ls_prods', 'Unit_Price_Including_VAT'); | |
$query->addField('ls_prods', 'Vorumyndir'); | |
$query->addField('ls_prods', 'No'); | |
$query->addField('ls_prods', 'Description_2'); | |
$options = array('track_changes' => 1); | |
$this->source = new MigrateSourceSQL($query, $this->fields(), NULL, $options); | |
//$this->destination = new MigrateDestinationEntityAPI('commerce_product', 'product'); | |
$this->destination = new MigrateDestinationCommerceProduct('commerce_product', 'product'); | |
$this->addFieldMapping('title', 'product_description'); | |
$this->addFieldMapping('sku', 'Barcode'); | |
$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'); | |
// Afbrigði Möppuð inn í Drupal Commerce | |
$this->addFieldMapping('field_nav_afbrigdi', 'prep_afbrigdi'); | |
$this->addFieldMapping('field_nav_afbrigdi:create_term')->defaultValue(TRUE); | |
$this->addFieldMapping('field_nav_litur', 'prep_litur'); | |
$this->addFieldMapping('field_nav_litur:create_term')->defaultValue(TRUE); | |
$this->addFieldMapping('field_nav_styrkur', 'prep_styrkur'); | |
$this->addFieldMapping('field_nav_styrkur:create_term')->defaultValue(TRUE); | |
$this->addFieldMapping('field_nav_staerd', 'prep_staerd'); | |
$this->addFieldMapping('field_nav_staerd:create_term')->defaultValue(TRUE); | |
} | |
function fields() { | |
return array( | |
'product_images' => 'An array of images, populated during prepareRow().', | |
'product_description' => 'Combined text for variants', | |
'prep_afbrigdi' => 'Afbrigði úr navision', | |
'prep_litur' => 'Litur úr navision', | |
'prep_styrkur' => 'Styrkur úr navision', | |
'prep_staerd' => 'Stærð úr navision', | |
); | |
} | |
function prepareRow($row) { | |
$images = explode(', ', $row->Vorumyndir); | |
$row->product_images = $images[0]; | |
$row->product_description = "$row->Description_2 - $row->Description"; | |
$row->uid = 1; | |
$row->status = 1; | |
$row->language = LANGUAGE_NONE; | |
$litur = null; | |
$afbrigdi = null; | |
$staerd = null; | |
$styrkur = null; | |
// Traverse through Variant columns and find correct data. | |
foreach(range(1,6) as $i) { | |
$fieldName = 'VarDimname' . $i; | |
$dataFieldName = 'Variant_Dimension_' . $i; | |
// Call helper function to return which field the value belongs to. | |
$result = $this->getVariantType($row->$fieldName); | |
if($result == 'field_nav_afbrigdi' ) { | |
$afbrigdi = $row->$dataFieldName; | |
} | |
if($result == 'field_nav_litur' ) { | |
$litur = $row->$dataFieldName; | |
} | |
if($result == 'field_nav_styrkur' ) { | |
$styrkur = $row->$dataFieldName; | |
} | |
if($result == 'field_nav_staerd' ) { | |
$staerd = $row->$dataFieldName; | |
} | |
} | |
$row->prep_afbrigdi = $afbrigdi; | |
$row->prep_litur = $litur; | |
$row->prep_styrkur = $styrkur; | |
$row->prep_staerd = $staerd; | |
$debug=TRUE; | |
if($debug){ | |
watchdog('lsretail_vorur', t('Importing %sku - %barcode'), array('%sku'=>$row->No , '%barcode'=>$row->Barcode), WATCHDOG_INFO); | |
} | |
} | |
public function preImport() { | |
parent::preImport(); | |
$foo = 'bar'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment