/** * Duplicate one product and return model * * @param ShopProduct $model * @return ShopProduct */ public function duplicateProduct(ShopProduct $model) { $product = new ShopProduct(null); // Add "null" by PANIX $product->attributes = $model->attributes; $behaviors = $model->behaviors(); foreach ($behaviors['TranslateBehavior']['translateAttributes'] as $attr) { $product->{$attr} = $model->{$attr}; } $product->name .= $this->getSuffix(); $product->seo_alias .= CMS::translit($this->getSuffix()) . '-' . time(); if ($product->validate()) { if ($product->save(false, false)) { foreach ($this->duplicate as $feature) { $method_name = 'copy' . ucfirst($feature); if (method_exists($this, $method_name)) { $this->{$method_name}($model, $product); } } return $product; } else { die(__FUNCTION__ . ': Error save'); return false; } } else { // print_r($product->getErrors());die; } }
/** * 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['seo_alias']) && !empty($data['seo_alias']) && $data['seo_alias'] != '') // $cr->compare('t.seo_alias', $data['seo_alias']); if (isset($data['sku']) && !empty($data['sku']) && $data['sku'] != '') { $cr->compare('t.sku', $data['sku']); } else { $cr->compare('translate.name', $data['name']); } $model = ShopProduct::model()->applyCategories($category_id)->find($cr); if (!$model) { $newProduct = true; $model = new ShopProduct(); $this->stats['create']++; } else { $this->stats['update']++; } $model->name = $data['name']; $model->price = $data['price']; $model->seo_alias = CMS::translit($data['name']); // 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 XmlAttributesProcessor($model, $data['attributes']); 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(false, false); // Update EAV data $attributes->save(); // Update categories $model->setCategories($categories, $category_id); // Process product main image if product doesn't have one $this->setImages($model, $data); } else { $errors = $model->getErrors(); $error = array_shift($errors); $this->errors[] = array('line' => $this->line, 'error' => $error[0]); } }