Ejemplo n.º 1
0
 /**
  * Append and save product attributes.
  */
 public function save()
 {
     if (!empty($this->eav)) {
         $this->model->setEavAttributes($this->eav, true);
     }
 }
Ejemplo n.º 2
0
 /**
  * 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 ShopProduct();
             // Add "null" by PANIX
             $model->type_id = self::DEFAULT_TYPE;
             $model->price = 0;
             $model->switch = 1;
             $createExId = true;
         }
         $model->name = $product->{"Наименование"};
         $model->seo_alias = CMS::translit($model->name);
         $model->sku = $product->{"Артикул"};
         if ($model->save(false, false)) {
         } else {
             // Yii::log(CJSON::encode($model->getErrors()),'info','application');
         }
         // 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);
         if (!empty($product->{"Картинка"})) {
             foreach ($product->{"Картинка"} as $pi) {
                 $image = C1ProductImage::create($this->buildPathToTempFile($pi));
                 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 = ShopAttributeOption::model()->find($cr);
                     if (!$option) {
                         $option = $this->addOptionToAttribute($attributeModel->id, $attribute->{"Значение"});
                     }
                     $attrsdata[$attributeModel->name] = $option->id;
                 }
             }
             if (!empty($attrsdata)) {
                 $model->setEavAttributes($attrsdata, true);
             }
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * Save model attributes
  * @param ShopProduct $model
  * @return boolean
  */
 protected function processAttributes(ShopProduct $model)
 {
     $attributes = new CMap(Yii::app()->request->getPost('ShopAttribute', array()));
     if (empty($attributes)) {
         return false;
     }
     $deleteModel = ShopProduct::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);
 }