/** * Append and save product attributes. */ public function save() { if (!empty($this->eav)) { $this->model->setEavAttributes($this->eav, true); } }
/** * 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 model attributes * @param StoreProduct $model * @return boolean */ protected function processAttributes(StoreProduct $model) { $attributes = new CMap(Yii::app()->request->getPost('StoreAttribute', array())); if (empty($attributes)) { return false; } $deleteModel = StoreProduct::model()->findByPk($model->id); $deleteModel->deleteEavAttributes(array(), true); // Delete empty values foreach ($attributes as $key => $val) { if (is_string($val) && $val === '') { $attributes->remove($key); } } return $model->setEavAttributes($attributes->toArray(), true); }