/** * Migrate Attributes */ public function actionStep3() { $step = MigrateSteps::model()->find("sorder = 3"); $result = MigrateSteps::checkStep($step->sorder); if ($result['allowed']) { //get migrated data of step1 from session $migrated_store_ids = isset(Yii::app()->session['migrated_store_ids']) ? Yii::app()->session['migrated_store_ids'] : array(); $migrated_attribute_set_ids = $migrated_attribute_group_ids = $migrated_attribute_ids = array(); //get product entity type id $product_entity_type_id = MigrateSteps::getMage1EntityTypeId(MigrateSteps::PRODUCT_TYPE_CODE); //get all product attribute sets in magento1 $attribute_sets = Mage1AttributeSet::model()->findAll("entity_type_id = {$product_entity_type_id}"); //get all product attributes //$condition = "entity_type_id = {$product_entity_type_id} AND is_user_defined = 1"; $attributes = Mage1Attribute::model()->findAll("entity_type_id = {$product_entity_type_id}"); if (Yii::app()->request->isPostRequest && $step->status == MigrateSteps::STATUS_NOT_DONE) { //uncheck foreign key Yii::app()->mage2->createCommand("SET FOREIGN_KEY_CHECKS=0")->execute(); //migrate attribute sets //eav_attribute_set if ($attribute_sets) { foreach ($attribute_sets as $attribute_set) { $entity_type_id2 = MigrateSteps::_getMage2EntityTypeId($attribute_set->entity_type_id); $condition = "entity_type_id = {$entity_type_id2} AND attribute_set_name = '{$attribute_set->attribute_set_name}'"; $attribute_set2 = Mage2AttributeSet::model()->find($condition); if (!$attribute_set2) { $attribute_set2 = new Mage2AttributeSet(); $attribute_set2->entity_type_id = MigrateSteps::_getMage2EntityTypeId($attribute_set->entity_type_id); $attribute_set2->attribute_set_name = $attribute_set->attribute_set_name; $attribute_set2->sort_order = $attribute_set->sort_order; } if ($attribute_set2->save()) { $migrated_attribute_set_ids[] = $attribute_set->attribute_set_id; } //get all attribute groups of current attribute set $condition = "attribute_set_id = {$attribute_set->attribute_set_id}"; $attribute_groups = Mage1AttributeGroup::model()->findAll($condition); if ($attribute_groups) { foreach ($attribute_groups as $attribute_group) { $attribute_set_id2 = MigrateSteps::getMage2AttributeSetId($attribute_group->attribute_set_id, MigrateSteps::PRODUCT_TYPE_CODE); $condition = "attribute_set_id = {$attribute_set_id2} AND attribute_group_name = '{$attribute_group->attribute_group_name}'"; $attribute_group2 = Mage2AttributeGroup::model()->find($condition); if (!$attribute_group2) { $attribute_group2 = new Mage2AttributeGroup(); $attribute_group2->attribute_set_id = $attribute_set_id2; $attribute_group2->attribute_group_name = $attribute_group->attribute_group_name; $attribute_group2->sort_order = $attribute_group->sort_order; $attribute_group2->default_id = $attribute_group->default_id; //NOTE: this values is new added in Magento2, we will update after migrated in back-end of Magento2 $attribute_group2->attribute_group_code = null; $attribute_group2->tab_group_code = null; } if ($attribute_group2->save()) { $migrated_attribute_group_ids[] = $attribute_group->attribute_group_id; } } } } } //migrate product attributes if ($attributes) { //Some tables need to reset before migrate Mage2AttributeOption::model()->deleteAll(); Mage2AttributeOptionValue::model()->deleteAll(); foreach ($attributes as $attribute) { //msrp_enabled was changed to msrp in magento2 if ($attribute->attribute_code == 'msrp_enabled') { $attribute_code2 = 'msrp'; } else { $attribute_code2 = $attribute->attribute_code; } $entity_type_id2 = MigrateSteps::_getMage2EntityTypeId($attribute->entity_type_id); $condition = "entity_type_id = {$entity_type_id2} AND attribute_code = '{$attribute_code2}'"; $attribute2 = Mage2Attribute::model()->find($condition); if (!$attribute2) { $attribute2 = new Mage2Attribute(); foreach ($attribute2->attributes as $key => $value) { if (isset($attribute->{$key})) { $attribute2->{$key} = $attribute->{$key}; } } //We need re-update some values $attribute2->attribute_id = null; $attribute2->entity_type_id = MigrateSteps::_getMage2EntityTypeId($attribute->entity_type_id); $attribute2->attribute_model = null; $attribute2->backend_model = null; $attribute2->frontend_model = null; $attribute2->source_model = null; } //save or update data of a attribute if ($attribute2->save()) { //update total $migrated_attribute_ids[] = $attribute->attribute_id; if ($migrated_store_ids) { //eav_attribute_label $condition = "attribute_id = {$attribute->attribute_id}"; $str_store_ids = implode(',', $migrated_store_ids); $condition .= " AND store_id IN ({$str_store_ids})"; $attribute_labels = Mage1AttributeLabel::model()->findAll($condition); if ($attribute_labels) { foreach ($attribute_labels as $attribute_label) { $attribute_label2 = new Mage2AttributeLabel(); $attribute_label2->attribute_label_id = $attribute_label->attribute_label_id; $attribute_label2->attribute_id = $attribute2->attribute_id; $attribute_label2->store_id = MigrateSteps::getMage2StoreId($attribute_label->store_id); $attribute_label2->value = $attribute_label->value; //save or update $attribute_label2->save(); } } } //eav_attribute_option $attribute_options = Mage1AttributeOption::model()->findAll("attribute_id = {$attribute->attribute_id}"); if ($attribute_options) { foreach ($attribute_options as $attribute_option) { $attribute_option2 = new Mage2AttributeOption(); $attribute_option2->option_id = $attribute_option->option_id; $attribute_option2->attribute_id = $attribute2->attribute_id; $attribute_option2->sort_order = $attribute_option->sort_order; //save or update if ($attribute_option2->save()) { //eav_attribute_option_value, if ($migrated_store_ids) { //get all option values of current option in Magento1 $condition = "option_id = {$attribute_option->option_id}"; $str_store_ids = implode(',', $migrated_store_ids); $condition .= " AND store_id IN ({$str_store_ids})"; $option_values = Mage1AttributeOptionValue::model()->findAll($condition); if ($option_values) { foreach ($option_values as $option_value) { $option_value2 = new Mage2AttributeOptionValue(); $option_value2->value_id = $option_value->value_id; $option_value2->option_id = $option_value->option_id; $option_value2->store_id = MigrateSteps::getMage2StoreId($option_value->store_id); $option_value2->value = $option_value->value; //update or save $option_value2->save(); } } } } } } //catalog_eav_attribute $catalog_eav_attribute = Mage1CatalogEavAttribute::model()->find("attribute_id = {$attribute->attribute_id}"); if ($catalog_eav_attribute) { $catalog_eav_attribute2 = Mage2CatalogEavAttribute::model()->find("attribute_id = {$attribute2->attribute_id}"); if (!$catalog_eav_attribute2) { //create new $catalog_eav_attribute2 = new Mage2CatalogEavAttribute(); foreach ($catalog_eav_attribute2->attributes as $key => $value) { if (isset($catalog_eav_attribute->{$key})) { $catalog_eav_attribute2->{$key} = $catalog_eav_attribute->{$key}; } } //update new attribute_id $catalog_eav_attribute2->attribute_id = $attribute2->attribute_id; $catalog_eav_attribute2->is_required_in_admin_store = 0; //this not take because was changed in magento2 $catalog_eav_attribute2->frontend_input_renderer = null; } else { // //update settings values // $catalog_eav_attribute2->is_global = $catalog_eav_attribute->is_global; // $catalog_eav_attribute2->is_visible = $catalog_eav_attribute->is_visible; // $catalog_eav_attribute2->is_searchable = $catalog_eav_attribute->is_searchable; // $catalog_eav_attribute2->is_filterable = $catalog_eav_attribute->is_filterable; // $catalog_eav_attribute2->is_comparable = $catalog_eav_attribute->is_comparable; // $catalog_eav_attribute2->is_visible_on_front = $catalog_eav_attribute->is_visible_on_front; // $catalog_eav_attribute2->is_html_allowed_on_front = $catalog_eav_attribute->is_html_allowed_on_front; // $catalog_eav_attribute2->is_filterable_in_search = $catalog_eav_attribute->is_filterable_in_search; // $catalog_eav_attribute2->used_in_product_listing = $catalog_eav_attribute->used_in_product_listing; // $catalog_eav_attribute2->used_for_sort_by = $catalog_eav_attribute->used_for_sort_by; // $catalog_eav_attribute2->apply_to = $catalog_eav_attribute->apply_to; // $catalog_eav_attribute2->is_visible_in_advanced_search = $catalog_eav_attribute->is_visible_in_advanced_search; $catalog_eav_attribute2->position = $catalog_eav_attribute->position; $catalog_eav_attribute2->is_wysiwyg_enabled = $catalog_eav_attribute->is_wysiwyg_enabled; $catalog_eav_attribute2->is_used_for_price_rules = $catalog_eav_attribute->is_used_for_price_rules; $catalog_eav_attribute2->is_used_for_promo_rules = $catalog_eav_attribute->is_used_for_promo_rules; } //save $catalog_eav_attribute2->save(); } } } //end foreach attributes } //eav_entity_attribute //we only migrate related with products if ($migrated_attribute_set_ids && $migrated_attribute_group_ids && $migrated_attribute_ids) { //make condition $str_migrated_attribute_ids = implode(',', $migrated_attribute_ids); $str_migrated_attribute_set_ids = implode(',', $migrated_attribute_set_ids); $str_migrated_attribute_group_ids = implode(',', $migrated_attribute_group_ids); $condition = "entity_type_id = {$product_entity_type_id} AND attribute_id IN ({$str_migrated_attribute_ids})"; $condition .= " AND attribute_set_id IN ({$str_migrated_attribute_set_ids})"; $condition .= " AND attribute_group_id IN ({$str_migrated_attribute_group_ids})"; $entity_attributes = Mage1EntityAttribute::model()->findAll($condition); if ($entity_attributes) { foreach ($entity_attributes as $entity_attribute) { $attribute_id2 = MigrateSteps::getMage2AttributeId($entity_attribute->attribute_id, '4'); $attribute_set_id2 = MigrateSteps::getMage2AttributeSetId($entity_attribute->attribute_set_id, MigrateSteps::PRODUCT_TYPE_CODE); $attribute_group_id2 = MigrateSteps::getMage2AttributeGroupId($entity_attribute->attribute_group_id); if ($attribute_id2 && $attribute_set_id2 && $attribute_group_id2) { $condition = "attribute_id = {$attribute_id2} AND entity_type_id = 4"; $condition .= " AND attribute_set_id = {$attribute_set_id2}"; $entity_attribute2 = Mage2EntityAttribute::model()->find($condition); if (!$entity_attribute2) { $entity_attribute2 = new Mage2EntityAttribute(); $entity_attribute2->entity_type_id = MigrateSteps::_getMage2EntityTypeId($entity_attribute->entity_type_id); $entity_attribute2->attribute_set_id = $attribute_set_id2; $entity_attribute2->attribute_group_id = $attribute_group_id2; $entity_attribute2->attribute_id = $attribute_id2; $entity_attribute2->sort_order = $entity_attribute->sort_order; } //save or update $entity_attribute2->save(); } } } } //Update step status if ($migrated_attribute_set_ids && $migrated_attribute_group_ids && $migrated_attribute_ids) { $step->status = MigrateSteps::STATUS_DONE; if ($step->update()) { //check foreign key Yii::app()->mage2->createCommand("SET FOREIGN_KEY_CHECKS=1")->execute(); $message = "Migrated successfully. Total Attribute Sets: %s1, Total Attribute Groups: %s2, Total Attributes: %s3"; $message = Yii::t('frontend', $message, array('%s1' => sizeof($migrated_attribute_set_ids), '%s2' => sizeof($migrated_attribute_group_ids), '%s3' => sizeof($migrated_attribute_ids))); Yii::app()->user->setFlash('success', $message); } } } else { if ($step->status == MigrateSteps::STATUS_DONE) { Yii::app()->user->setFlash('note', Yii::t('frontend', "This step was finished. If you want to update data of this step, the first you have to click to 'Reset' button.")); } } $assign_data = array('step' => $step, 'attribute_sets' => $attribute_sets, 'attributes' => $attributes); $this->render("step{$step->sorder}", $assign_data); } else { Yii::app()->user->setFlash('note', Yii::t('frontend', "The first you need to finish the %s.", array("%s" => ucfirst($result['back_step'])))); $this->redirect(array($result['back_step'])); } }
public static function getMage2AttributeSetId($mage1AttrSetId, $entity_type_code = null) { $id = null; if (isset($mage1AttrSetId)) { $cacheId = "attribute_set_id2_{$mage1AttrSetId}_{$entity_type_code}"; $val = Yii::app()->cache->get($cacheId); if (!$val) { $model1 = Mage1AttributeSet::model()->findByPk($mage1AttrSetId); if ($model1) { if (!$entity_type_code) { $entity_type_code = self::PRODUCT_TYPE_CODE; } $entity_type_id2 = MigrateSteps::getMage2EntityTypeId($entity_type_code); $model2 = Mage2AttributeSet::model()->find("entity_type_id = {$entity_type_id2} AND attribute_set_name = '{$model1->attribute_set_name}'"); if ($model2) { $id = $model2->attribute_set_id; } } //save to cache for later Yii::app()->cache->set($cacheId, $id, 86400); // one day } else { $id = $val; } } return $id; }
public static function getMage2AttributeSetId($mage1AttrSetId, $entity_type_code = null) { $id = null; if (isset($mage1AttrSetId)) { $model1 = Mage1AttributeSet::model()->findByPk($mage1AttrSetId); if ($model1) { if (!$entity_type_code) { $entity_type_code = self::PRODUCT_TYPE_CODE; } $entity_type_id2 = MigrateSteps::getMage2EntityTypeId($entity_type_code); $model2 = Mage2AttributeSet::model()->find("entity_type_id = {$entity_type_id2} AND attribute_set_name = '{$model1->attribute_set_name}'"); if ($model2) { $id = $model2->attribute_set_id; } } } return $id; }