Example #1
0
 public function save()
 {
     $product = Product::model()->findByPk($this->id);
     if (is_null($product)) {
         // insert
         // Product
         $product = new Product();
         $product->model = $this->model;
         $product->sku = $this->sku;
         $product->upc = $this->upc;
         $product->ean = $this->ean;
         $product->jan = $this->jan;
         $product->isbn = $this->isbn;
         $product->mpn = $this->mpn;
         $product->location = $this->location;
         $product->price = $this->price;
         $product->tax_class_id = $this->taxClass;
         $product->quantity = $this->quantity;
         $product->minimum = $this->minimumQuantity;
         $product->subtract = $this->subtractStock;
         $product->stock_status_id = $this->outOfStockStatus;
         $product->shipping = $this->requiresShipping;
         $product->image = $this->image;
         $product->date_available = $this->dateAvailable;
         $product->length = $this->dimensionL;
         $product->height = $this->dimensionH;
         $product->width = $this->dimensionW;
         $product->weight = $this->weight;
         $product->weight_class_id = $this->weightClass;
         $product->sort_order = $this->sortOrder;
         $product->status = $this->status;
         $product->manufacturer_id = $this->manufacturer;
         $product->save();
         // Description
         $description = new ProductDescription();
         $description->product_id = $product->product_id;
         $description->language_id = 1;
         // TODO: make this dynamic
         $description->name = $this->name;
         $description->meta_description = $this->metaTagDescription;
         $description->meta_keyword = $this->metaTagKeywords;
         $description->description = $this->description;
         $description->tag = $this->productTags;
         $description->save();
     } else {
         // update
         // Product
         $product->model = $this->model;
         $product->sku = $this->sku;
         $product->upc = $this->upc;
         $product->ean = $this->ean;
         $product->jan = $this->jan;
         $product->isbn = $this->isbn;
         $product->mpn = $this->mpn;
         $product->location = $this->location;
         $product->price = $this->price;
         $product->tax_class_id = $this->taxClass;
         $product->quantity = $this->quantity;
         $product->minimum = $this->minimumQuantity;
         $product->subtract = $this->subtractStock;
         $product->stock_status_id = $this->outOfStockStatus;
         $product->shipping = $this->requiresShipping;
         $product->image = $this->image;
         $product->date_available = $this->dateAvailable;
         $product->length = $this->dimensionL;
         $product->height = $this->dimensionH;
         $product->width = $this->dimensionW;
         $product->weight = $this->weight;
         $product->weight_class_id = $this->weightClass;
         $product->sort_order = $this->sortOrder;
         $product->status = $this->status;
         $product->manufacturer_id = $this->manufacturer;
         $product->save();
         // Description
         $product->description->name = $this->name;
         $product->description->meta_description = $this->metaTagDescription;
         $product->description->meta_keyword = $this->metaTagKeywords;
         $product->description->description = $this->description;
         $product->description->tag = $this->productTags;
         $product->description->save();
     }
     // SEO Keyword
     $product->updateSEOKeyword($this->seoKeyword);
     // Filters
     $product->clearAllFiltersRelations();
     if (isset($this->filters) && count($this->filters) > 0) {
         foreach ($this->filters as $filterId) {
             $product->addFilter($filterId);
         }
     }
     // Categories
     $product->clearAllCategoriesRelations();
     if (isset($this->categories) && count($this->categories)) {
         foreach ($this->categories as $categoryId) {
             $product->addToCategory($categoryId);
         }
     }
     // Stores
     $product->clearAllStoresRelations();
     if (isset($this->stores) && count($this->stores)) {
         foreach ($this->stores as $storeId) {
             $product->addToStore($storeId);
         }
     }
     // Downloads
     $product->clearAllDownloadsRelations();
     if (isset($this->downloads) && count($this->downloads)) {
         foreach ($this->downloads as $downloadId) {
             $product->addToDownload($downloadId);
         }
     }
     // Related Products
     $product->clearAllRelatedProductsRelations();
     if (isset($this->relatedProducts) && count($this->relatedProducts)) {
         foreach ($this->relatedProducts as $relatedId) {
             $product->addRelatedProduct($relatedId);
         }
     }
 }