Example #1
0
 public function testIsRelatedTo()
 {
     $notRelatedProduct = Product::getNewInstance($this->productCategory);
     $notRelatedProduct->save();
     $relatedProduct = Product::getNewInstance($this->productCategory);
     $relatedProduct->save();
     $this->product->addRelatedProduct($relatedProduct);
     $this->product->save();
     $this->assertFalse($notRelatedProduct->isRelatedTo($this->product, ProductRelationship::TYPE_CROSS));
     $this->assertTrue($relatedProduct->isRelatedTo($this->product, ProductRelationship::TYPE_CROSS));
     // isRelatedTo provide one direction testing is related to means that
     // this product is in that product's related products list
     $this->assertFalse($this->product->isRelatedTo($relatedProduct, ProductRelationship::TYPE_CROSS));
 }
 protected function processRecord(Product $product)
 {
     $act = $this->getAction();
     $field = $this->getField();
     if ('manufacturer' == $act) {
         $product->manufacturer->set($this->params['manufacturer']);
     } else {
         if ('price' == $act) {
             $product->setPrice($this->params['baseCurrency'], $this->params['price']);
         } else {
             if (in_array($act, array('inc_price', 'multi_price', 'div_price'))) {
                 $actions = array('inc_price' => 'increasePriceByPercent', 'multi_price' => 'multiplyPrice', 'div_price' => 'dividePrice');
                 $action = $actions[$act];
                 $pricing = $product->getPricingHandler();
                 foreach ($this->params['currencies'] as $currency) {
                     if ($pricing->isPriceSet($currency)) {
                         $p = $pricing->getPrice($currency);
                         $p->{$action}($this->params['inc_price_value'], $this->params['inc_quant_price']);
                         $p->save();
                     }
                 }
             } else {
                 if ('inc_stock' == $act) {
                     $product->stockCount->set($product->stockCount->get() + $this->request->get($act));
                 } else {
                     if ('addRelated' == $act) {
                         $product->addRelatedProduct($this->params['relatedProduct']);
                     } else {
                         if ('copy' == $act) {
                             $cloned = clone $product;
                             $cloned->category->set($this->params['category']);
                             $cloned->save();
                         } else {
                             if ('addCat' == $act) {
                                 // check if the product is not assigned to this category already
                                 $relation = ActiveRecordModel::getInstanceByIdIfExists('ProductCategory', array('productID' => $product->getID(), 'categoryID' => $this->params['category']->getID()));
                                 if (!$relation->isExistingRecord() && $product->category->get() !== $category) {
                                     $relation->save();
                                 }
                             } else {
                                 if ('theme' == $act) {
                                     $instance = CategoryPresentation::getInstance($product);
                                     $instance->theme->set($this->params['theme']);
                                     $instance->save();
                                 } else {
                                     if ('shippingClass' == $act) {
                                         $product->shippingClass->set(ActiveRecordModel::getInstanceByIDIfExists('ShippingClass', $this->params['shippingClass'], false));
                                     } else {
                                         if ('taxClass' == $act) {
                                             $product->taxClass->set(ActiveRecordModel::getInstanceByIDIfExists('TaxClass', $this->params['taxClass'], false));
                                         } else {
                                             if (substr($act, 0, 13) == 'set_specField') {
                                                 $this->params['request']->remove('manufacturer');
                                                 $product->loadRequestData($this->params['request']);
                                             } else {
                                                 if (substr($act, 0, 16) == 'remove_specField') {
                                                     $this->params['request']->remove('manufacturer');
                                                     if ($this->params['field']->isMultiValue->get()) {
                                                         // remove only selected multi-select options
                                                         $product->loadRequestData($this->params['request']);
                                                     } else {
                                                         $product->removeAttribute($this->params['field']);
                                                     }
                                                 } else {
                                                     parent::processRecord($product);
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Example #3
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);
         }
     }
 }
 protected function processRecord(Product $product)
 {
     $act = $this->getAction();
     $field = $this->getField();
     if ('manufacturer' == $act) {
         $product->manufacturer->set($this->params['manufacturer']);
     } else {
         if ('price' == $act) {
             $product->setPrice($this->params['baseCurrency'], $this->params['price']);
         } else {
             if ('inc_price' == $act) {
                 $pricing = $product->getPricingHandler();
                 foreach ($this->params['currencies'] as $currency) {
                     if ($pricing->isPriceSet($currency)) {
                         $p = $pricing->getPrice($currency);
                         $p->increasePriceByPercent($this->params['inc_price_value'], $this->params['inc_quant_price']);
                         $p->save();
                     }
                 }
             } else {
                 if ('inc_stock' == $act) {
                     $product->stockCount->set($product->stockCount->get() + $this->request->get($act));
                 } else {
                     if ('addRelated' == $act) {
                         $product->addRelatedProduct($this->params['relatedProduct']);
                     } else {
                         if ('copy' == $act) {
                             $cloned = clone $product;
                             $cloned->category->set($this->params['category']);
                             $cloned->save();
                         } else {
                             if ('addCat' == $act) {
                                 // check if the product is not assigned to this category already
                                 $relation = ActiveRecordModel::getInstanceByIdIfExists('ProductCategory', array('productID' => $product->getID(), 'categoryID' => $this->params['category']->getID()));
                                 if (!$relation->isExistingRecord() && $product->category->get() !== $category) {
                                     $relation->save();
                                 }
                             } else {
                                 if ('theme' == $act) {
                                     $instance = CategoryPresentation::getInstance($product);
                                     $instance->theme->set($this->params['theme']);
                                     $instance->save();
                                 } else {
                                     if ('shippingClass' == $act) {
                                         $product->shippingClass->set(ActiveRecordModel::getInstanceByIDIfExists('ShippingClass', $this->params['shippingClass'], false));
                                     } else {
                                         if ('taxClass' == $act) {
                                             $product->taxClass->set(ActiveRecordModel::getInstanceByIDIfExists('TaxClass', $this->params['taxClass'], false));
                                         } else {
                                             parent::processRecord($product);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }