Ejemplo n.º 1
0
 /**
  * Duplicate one product and return model
  *
  * @param StoreProduct $model
  * @return StoreProduct
  */
 public function duplicateProduct(StoreProduct $model)
 {
     $product = new StoreProduct();
     $product->attributes = $model->attributes;
     $behaviors = $model->behaviors();
     foreach ($behaviors['STranslateBehavior']['translateAttributes'] as $attr) {
         $product->{$attr} = $model->{$attr};
     }
     $product->name .= $this->getSuffix();
     if ($product->save()) {
         foreach ($this->duplicate as $feature) {
             $method_name = 'copy' . ucfirst($feature);
             if (method_exists($this, $method_name)) {
                 $this->{$method_name}($model, $product);
             }
         }
         return $product;
     } else {
         return false;
     }
 }
Ejemplo n.º 2
0
 /**
  * Create/update product from key=>value array
  * @param $data array of product attributes
  */
 protected function importRow($data)
 {
     if (!isset($data['category']) || empty($data['category'])) {
         $data['category'] = 'root';
     }
     $newProduct = false;
     $category_id = $this->getCategoryByPath($data['category']);
     // Search product by name, category
     // or create new one
     $cr = new CDbCriteria();
     $cr->with = array('translate');
     if (isset($data['url']) && !empty($data['url']) && $data['url'] != '') {
         $cr->compare('t.url', $data['url']);
     }
     if (isset($data['sku']) && !empty($data['sku']) && $data['sku'] != '') {
         $cr->compare('t.sku', $data['sku']);
     } else {
         $cr->compare('translate.name', $data['name']);
     }
     $model = StoreProduct::model()->applyCategories($category_id)->find($cr);
     if (!$model) {
         $newProduct = true;
         $model = new StoreProduct();
         $this->stats['created']++;
     } else {
         $this->stats['updated']++;
     }
     // Process product type
     $model->type_id = $this->getTypeIdByName($data['type']);
     // Manufacturer
     if (isset($data['manufacturer']) && !empty($data['manufacturer'])) {
         $model->manufacturer_id = $this->getManufacturerIdByName($data['manufacturer']);
     }
     // Update product variables and eav attributes.
     $attributes = new CsvAttributesProcessor($model, $data);
     if ($model->validate()) {
         $categories = array($category_id);
         if (isset($data['additionalCategories'])) {
             $categories = array_merge($categories, $this->getAdditionalCategories($data['additionalCategories']));
         }
         if (!$newProduct) {
             foreach ($model->categorization as $c) {
                 $categories[] = $c->category;
             }
             $categories = array_unique($categories);
         }
         // Save product
         $model->save();
         // Update EAV data
         $attributes->save();
         // Update categories
         $model->setCategories($categories, $category_id);
         // Process product main image if product doesn't have one
         if (isset($data['image']) && !empty($data['image'])) {
             $image = CsvImage::create($data['image']);
             if ($image && $model->mainImage === null) {
                 $model->addImage($image);
             }
             if ($image && $this->deleteDownloadedImages) {
                 $image->deleteTempFile();
             }
         }
     } else {
         $errors = $model->getErrors();
         $error = array_shift($errors);
         $this->errors[] = array('line' => $this->line, 'error ' => $error[0]);
     }
 }
Ejemplo n.º 3
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 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);
             }
         }
     }
 }
 public function actionAjax_Create()
 {
     if (isset($_POST['StoreProduct'])) {
         $model = new StoreProduct();
         //set the submitted values
         $model->attributes = $_POST['StoreProduct'];
         //return the JSON result to provide feedback.
         if ($model->save(false)) {
             echo json_encode(array('success' => true, 'id' => $model->primaryKey));
             exit;
         } else {
             echo json_encode(array('success' => false));
             exit;
         }
     }
 }