Example #1
0
 public function __clone()
 {
     parent::__clone();
     $original = $this->originalRecord;
     $original->loadSpecification();
     $this->specificationInstance = clone $original->getSpecification();
     $this->specificationInstance->setOwner($this);
     $original->loadPricing();
     $this->pricingHandlerInstance = clone $original->pricingHandlerInstance;
     $this->pricingHandlerInstance->setProduct($this);
     $this->save();
     // images
     if ($original->defaultImage->get()) {
         foreach ($original->getRelatedRecordSet('ProductImage', $original->getImageFilter()) as $image) {
             $image->_clone($this);
         }
     }
     // options
     foreach (ProductOption::getProductOptions($original) as $option) {
         $clonedOpt = clone $option;
         $clonedOpt->product->set($this);
         $clonedOpt->save();
     }
     // related products
     $groups[] = array();
     foreach ($original->getRelationships() as $relationship) {
         $group = $relationship->productRelationshipGroup->get();
         $id = $group ? $group->getID() : null;
         if ($id) {
             $groups[$id] = clone $group;
             $groups[$id]->product->set($this);
             $groups[$id]->save();
         }
         $cloned = ProductRelationship::getNewInstance($this, $relationship->relatedProduct->get(), $id ? $groups[$id] : null);
         $cloned->save();
     }
 }
 /**
  * @role update
  */
 public function sort()
 {
     $target = $this->request->get('target');
     if (!$target) {
         foreach ($this->request->toArray() as $key => $value) {
             if (is_array($value)) {
                 $target = $key;
                 break;
             }
         }
     }
     $product = Product::getInstanceByID($this->request->get('id'));
     $type = $this->request->get('type');
     preg_match('/_(\\d+)$/', $target, $match);
     // Get group.
     if (substr($target, 0, 8) == 'noGroup_') {
         $match = array();
     }
     foreach ($this->request->get($target, array()) as $position => $key) {
         if (empty($key)) {
             continue;
         }
         $relationship = ProductRelationship::getInstance($product, Product::getInstanceByID((int) $key), $type);
         $relationship->position->set((int) $position);
         if (isset($match[1])) {
             $relationship->productRelationshipGroup->set(ProductRelationshipGroup::getInstanceByID((int) $match[1]));
         } else {
             $relationship->productRelationshipGroup->setNull();
         }
         $relationship->save();
     }
     return new JSONResponse(false, 'success');
 }
Example #3
0
 public function testClone()
 {
     $image = ActiveRecordModel::getNewInstance('ProductImage');
     $image->product->set($this->product);
     $image->save();
     $this->assertSame($image, $this->product->defaultImage->get());
     $numField = SpecField::getNewInstance($this->productCategory, SpecField::DATATYPE_NUMBERS, SpecField::TYPE_NUMBERS_SIMPLE);
     $numField->save();
     $this->product->setAttributeValue($numField, 100);
     $this->product->save();
     $option = ProductOption::getNewInstance($this->product);
     $option->type->set(ProductOption::TYPE_SELECT);
     $option->setValueByLang('name', 'en', 'test');
     $option->save();
     $related = Product::getNewInstance($this->productCategory, 'related');
     $related->save();
     $relGroup = ProductRelationshipGroup::getNewInstance($this->product, ProductRelationship::TYPE_CROSS);
     $relGroup->save();
     $rel = ProductRelationship::getNewInstance($this->product, $related, $relGroup);
     $rel->save();
     $this->assertEquals(1, $this->product->getRelationships()->size());
     $cloned = clone $this->product;
     $this->assertEquals(100, $cloned->getSpecification()->getAttribute($numField)->value->get());
     $cloned->setAttributeValue($numField, 200);
     $cloned->setPrice($this->usd, 80);
     $cloned->save();
     $this->assertNotEquals($cloned->getID(), $this->product->getID());
     ActiveRecordModel::clearPool();
     $reloaded = Product::getInstanceByID($cloned->getID(), true);
     $reloaded->loadPricing();
     $this->assertEquals(80, $reloaded->getPrice($this->usd));
     $reloaded->loadSpecification();
     $this->assertEquals(200, $reloaded->getSpecification()->getAttribute($numField)->value->get());
     // related products
     $rel = $reloaded->getRelationships();
     $this->assertEquals(1, $rel->size());
     $this->assertSame($reloaded, $rel->get(0)->productRelationshipGroup->get()->product->get());
     // options
     $clonedOpts = ProductOption::getProductOptions($reloaded);
     $this->assertEquals(1, $clonedOpts->size());
     // image
     $this->assertTrue(is_object($reloaded->defaultImage->get()));
     $this->assertNotEquals($reloaded->defaultImage->get()->getID(), $this->product->defaultImage->get()->getID());
 }
Example #4
0
 public function __clone()
 {
     parent::__clone();
     $original = $this->originalRecord;
     $original->loadSpecification();
     $this->specificationInstance = clone $original->getSpecification();
     $this->specificationInstance->setOwner($this);
     $original->loadPricing();
     $this->pricingHandlerInstance = clone $original->pricingHandlerInstance;
     $this->pricingHandlerInstance->setProduct($this);
     $this->save();
     // images
     if ($original->defaultImage->get()) {
         foreach ($original->getRelatedRecordSet('ProductImage', $original->getImageFilter()) as $image) {
             $image->_clone($this);
         }
     }
     // options
     foreach (ProductOption::getProductOptions($original) as $option) {
         $clonedOpt = clone $option;
         $clonedOpt->product->set($this);
         $clonedOpt->save();
     }
     // variations
     $variations = $original->getRelatedRecordSet('Product');
     if ($variations->size()) {
         $idMap = array();
         foreach ($original->getRelatedRecordSet('ProductVariationType') as $type) {
             $newType = clone $type;
             $newType->product->set($this);
             $newType->save();
             foreach ($type->getRelatedRecordSet('ProductVariation') as $var) {
                 $newVar = clone $var;
                 $newVar->type->set($newType);
                 $newVar->save();
                 $idMap[$var->getID()] = $newVar->getID();
             }
         }
         foreach ($variations as $variation) {
             $newVariation = clone $variation;
             $newVariation->parent->set($this);
             $newVariation->save();
             foreach ($variation->getRelatedRecordSet('ProductVariationValue') as $value) {
                 $newValue = clone $value;
                 $newValue->product->set($newVariation);
                 $newValue->variation->set(ActiveRecordModel::getInstanceByID('ProductVariation', $idMap[$value->variation->get()->getID()], true));
                 $newValue->save();
             }
         }
     }
     // additional categories
     foreach ($original->getRelatedRecordSet('ProductCategory') as $additionalCat) {
         $newCat = clone $additionalCat;
         $newCat->product->set($this);
         $newCat->save();
     }
     // related products
     $groups[] = array();
     foreach ($original->getRelationships() as $relationship) {
         $group = $relationship->productRelationshipGroup->get();
         $id = $group ? $group->getID() : null;
         if ($id) {
             $groups[$id] = clone $group;
             $groups[$id]->product->set($this);
             $groups[$id]->save();
         }
         $cloned = ProductRelationship::getNewInstance($this, $relationship->relatedProduct->get(), $id ? $groups[$id] : null);
         $cloned->save();
     }
 }
 public function testHasRelationship()
 {
     $product = array();
     foreach (range(1, 3) as $i) {
         $product[$i] = Product::getNewInstance($this->rootCategory, 'test');
         $product[$i]->save();
     }
     $relationship = ProductRelationship::getNewInstance($product[1], $product[2]);
     // Check relationship
     $this->assertFalse(ProductRelationship::hasRelationship($product[1], $product[2], ProductRelationship::TYPE_CROSS));
     $this->assertFalse(ProductRelationship::hasRelationship($product[1], $product[3], ProductRelationship::TYPE_CROSS));
     // Double check relationship to be sure that it is not being created by previous test
     $this->assertFalse(ProductRelationship::hasRelationship($product[1], $product[3], ProductRelationship::TYPE_CROSS));
     // Save and check again. Has relationship will return true if the record was set
     $relationship->save();
     $this->assertTrue(ProductRelationship::hasRelationship($product[1], $product[2], ProductRelationship::TYPE_CROSS));
     $this->assertFalse(ProductRelationship::hasRelationship($product[1], $product[3], ProductRelationship::TYPE_CROSS));
 }
Example #6
0
 public function getNextProductRelationship()
 {
     if (!($data = $this->loadRecord('SELECT * FROM ' . $this->getTablePrefix() . 'related_items'))) {
         return null;
     }
     $owner = Product::getInstanceByID($this->getRealId('Product', $data['Owner']), Product::LOAD_DATA);
     $target = Product::getInstanceByID($this->getRealId('Product', $data['productID']), Product::LOAD_DATA);
     return ProductRelationship::getNewInstance($owner, $target);
 }