/** * Find or create option by attribute and value. * * @param StoreAttribute $attribute * @param $val * @return StoreAttributeOption */ public function getOption(StoreAttribute $attribute, $val) { $val = trim($val); $cacheKey = sha1($attribute->id . $val); if (isset($this->optionsCache[$cacheKey])) { return $this->optionsCache[$cacheKey]; } // Search for option $cr = new CDbCriteria(); $cr->with = 'option_translate'; $cr->compare('option_translate.value', $val); $cr->compare('t.attribute_id', $attribute->id); $option = StoreAttributeOption::model()->find($cr); if (!$option) { // Create new option $option = $this->addOptionToAttribute($attribute->id, $val); } $this->optionsCache[$cacheKey] = $option; return $option; }
/** * Import catalog products */ public function importProducts() { foreach ($this->xml->{"Каталог"}->{"Товары"}->{"Товар"} as $product) { $createExId = false; $model = C1ExternalFinder::getObject(C1ExternalFinder::OBJECT_TYPE_PRODUCT, $product->{"Ид"}); if (!$model) { $model = new StoreProduct(); $model->type_id = self::DEFAULT_TYPE; $model->price = 0; $model->is_active = 1; $createExId = true; } $model->name = $product->{"Наименование"}; $model->sku = $product->{"Артикул"}; $model->save(); // Create external id if ($createExId === true) { $this->createExternalId(C1ExternalFinder::OBJECT_TYPE_PRODUCT, $model->id, $product->{"Ид"}); } // Set category $categoryId = C1ExternalFinder::getObject(C1ExternalFinder::OBJECT_TYPE_CATEGORY, $product->{"Группы"}->{"Ид"}, false); $model->setCategories(array($categoryId), $categoryId); // Set image $image = C1ProductImage::create($this->buildPathToTempFile($product->{"Картинка"})); if ($image && !$model->mainImage) { $model->addImage($image); } // Process properties if (isset($product->{"ЗначенияСвойств"}->{"ЗначенияСвойства"})) { $attrsdata = array(); foreach ($product->{"ЗначенияСвойств"}->{"ЗначенияСвойства"} as $attribute) { $attributeModel = C1ExternalFinder::getObject(C1ExternalFinder::OBJECT_TYPE_ATTRIBUTE, $attribute->{"Ид"}); if ($attributeModel && $attribute->{"Значение"} != '') { $cr = new CDbCriteria(); $cr->with = 'option_translate'; $cr->compare('option_translate.value', $attribute->{"Значение"}); $option = StoreAttributeOption::model()->find($cr); if (!$option) { $option = $this->addOptionToAttribute($attributeModel->id, $attribute->{"Значение"}); } $attrsdata[$attributeModel->name] = $option->id; } } if (!empty($attrsdata)) { $model->setEavAttributes($attrsdata, true); } } } }
/** * Save attribute options * @param StoreAttribute $model */ protected function saveOptions($model) { $dontDelete = array(); if (!empty($_POST['options'])) { $position = 0; foreach ($_POST['options'] as $key => $val) { if (isset($val[0]) && $val[0] != '') { $index = 0; $attributeOption = StoreAttributeOption::model()->findByAttributes(array('id' => $key, 'attribute_id' => $model->id)); if (!$attributeOption) { $attributeOption = new StoreAttributeOption(); $attributeOption->attribute_id = $model->id; } $attributeOption->position = $position; $attributeOption->save(false); foreach (Yii::app()->languageManager->languages as $lang) { $attributeOption = StoreAttributeOption::model()->language($lang->id)->findByAttributes(array('id' => $attributeOption->id)); $attributeOption->value = $val[$index]; $attributeOption->save(false); ++$index; } array_push($dontDelete, $attributeOption->id); $position++; } } } if (sizeof($dontDelete)) { $cr = new CDbCriteria(); $cr->addNotInCondition('t.id', $dontDelete); $optionsToDelete = StoreAttributeOption::model()->findAllByAttributes(array('attribute_id' => $model->id), $cr); } else { // Clear all attribute options $optionsToDelete = StoreAttributeOption::model()->findAllByAttributes(array('attribute_id' => $model->id)); } if (!empty($optionsToDelete)) { foreach ($optionsToDelete as $o) { $o->delete(); } } }