Example #1
0
 public function setUp()
 {
     parent::setUp();
     $this->product = Product::getNewInstance(Category::getRootNode(), 'test');
     $this->product->save();
     $this->option = ProductOption::getNewInstance($this->product);
     $this->option->type->set(ProductOption::TYPE_SELECT);
     $this->option->save();
     for ($k = 0; $k <= 1; $k++) {
         $choice = ProductOptionChoice::getNewInstance($this->option);
         $choice->priceDiff->set(10 + $k);
         $choice->save();
         $this->choices[] = $choice;
     }
 }
Example #2
0
 public function addChoice(ProductOptionChoice $choice)
 {
     $this->choices[$choice->getID()] = $choice;
 }
Example #3
0
 public function importInstance($record, CsvImportProfile $profile)
 {
     $this->className = 'Product';
     $impReq = new Request();
     $defLang = $this->application->getDefaultLanguageCode();
     $references = array('DefaultImage' => 'ProductImage', 'Manufacturer', 'ShippingClass', 'TaxClass');
     $cat = $this->getCategory($profile, $record);
     $extraCategories = null;
     $fields = $profile->getSortedFields();
     if (isset($fields['Categories']['ExtraCategories'])) {
         $extraCategories = explode('; ', $record[$fields['Categories']['ExtraCategories']]);
     }
     if (isset($fields['Product']) && $cat) {
         $product = null;
         if (isset($fields['Product']['ID']) && !empty($record[$fields['Product']['ID']])) {
             $id = $record[$fields['Product']['ID']];
             if (ActiveRecord::objectExists('Product', $id)) {
                 $product = Product::getInstanceByID($id, Product::LOAD_DATA, $references);
             }
         } else {
             if (isset($fields['Product']['sku']) && !empty($record[$fields['Product']['sku']])) {
                 $product = Product::getInstanceBySku($record[$fields['Product']['sku']], $references);
             }
         }
         if ($product && $product->getID()) {
             $this->registerImportedID($product->getID());
         }
         if (!$product && 'update' == $this->options['action'] || $product && 'add' == $this->options['action']) {
             return false;
         }
         if ($product) {
             $product->loadSpecification();
             $product->loadPricing();
         } else {
             if ($cat instanceof Category) {
                 $product = Product::getNewInstance($cat);
             } else {
                 $product = $cat->createChildProduct();
             }
             $product->isEnabled->set(true);
         }
         // product information
         $impReq->clearData();
         foreach ($profile->getFields() as $csvIndex => $field) {
             $column = $field['name'];
             $params = $field['params'];
             if (!isset($record[$csvIndex]) || empty($column)) {
                 continue;
             }
             $value = $record[$csvIndex];
             list($className, $field) = explode('.', $column, 2);
             if (isset($params['language'])) {
                 $lang = $params['language'];
                 if ($lang != $defLang) {
                     $field .= '_' . $lang;
                 }
             }
             if ($value) {
                 if ('Product.parentID' == $column) {
                     $product->parent->set();
                     continue;
                 }
                 if ('Product.parentSKU' == $column) {
                     $product->parent->set(Product::getInstanceBySKU($value));
                     continue;
                 }
             }
             if ('Product.taxClass' == $column) {
                 $product->taxClass->set(TaxClass::findByName($value));
             }
             if ('Product.shippingClass' == $column) {
                 $product->shippingClass->set(ShippingClass::findByName($value));
             }
             if ('Product' == $className) {
                 if ('shippingWeight' == $field) {
                     if ($this->application->getConfig()->get('UNIT_SYSTEM') == 'ENGLISH') {
                         $value = $value / 0.45359237;
                     }
                 }
                 if ('shippingWeight' == $field && $product->parent->get()) {
                     $value = $this->setChildSetting($product, 'weight', $value);
                 }
                 $impReq->set($field, $value);
             } else {
                 if ('Manufacturer' == $className) {
                     $impReq->set('manufacturer', $value);
                 } else {
                     if ('ProductPrice.price' == $column) {
                         if ($product->parent->get()) {
                             $value = $this->setChildSetting($product, 'price', $value);
                         }
                         $value = preg_replace('/,([0-9]{3})/', '\\1', $value);
                         $value = (double) preg_replace('/[^\\.0-9]/', '', str_replace(',', '.', $value));
                         $currency = isset($params['currency']) ? $params['currency'] : $this->application->getDefaultCurrencyCode();
                         $quantityLevel = isset($params['quantityLevel']) ? $params['quantityLevel'] : '';
                         $group = isset($params['group']) ? $params['group'] : '';
                         $price = $product->getPricingHandler()->getPriceByCurrencyCode($currency);
                         $product->getPricingHandler()->setPrice($price);
                         if ($group || $quantityLevel) {
                             if ($value > 0) {
                                 $quantity = $quantityLevel ? $record[$fields['ProductPrice'][$quantityLevel]] : 1;
                                 $group = $group ? UserGroup::getInstanceByID($group) : null;
                                 $price->setPriceRule($quantity, $group, $value);
                             }
                         } else {
                             $price->price->set($value);
                         }
                     } else {
                         if ('ProductPrice.listPrice' == $column) {
                             $value = (double) preg_replace('/[^\\.0-9]/', '', str_replace(',', '.', $value));
                             $currency = $params['currency'];
                             $price = $product->getPricingHandler()->getPriceByCurrencyCode($currency);
                             $price->listPrice->set($value);
                             $product->getPricingHandler()->setPrice($price);
                         } else {
                             if ('ProductVariation' == $className) {
                                 if ($parent = $product->parent->get()) {
                                     $this->importProductVariationValue($product, $field, $value);
                                 } else {
                                     $this->importVariationType($product, $field, $value);
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $product->loadRequestData($impReq);
         $product->save();
         $this->importAttributes($product, $record, $fields, 'specField');
         $this->setLastImportedRecordName($product->getValueByLang('name'));
         if (isset($fields['ProductImage']['mainurl'])) {
             if (!($image = $product->defaultImage->get())) {
                 $image = ProductImage::getNewInstance($product);
             }
             $image->setOwner($product);
             // this is needed when ProductApi imports default ProductImage.
             $this->importImage($image, $record[$fields['ProductImage']['mainurl']]);
             unset($image);
         }
         if (isset($fields['ProductAdditionalImage'])) {
             foreach ($fields['ProductAdditionalImage'] as $index) {
                 $this->importImage(ProductImage::getNewInstance($product), $record[$index]);
             }
         }
         if (isset($fields['ProductImage']['Images'])) {
             $images = explode('; ', $record[$fields['ProductImage']['Images']]);
             if ($images) {
                 $product->deleteRelatedRecordSet('ProductImage');
                 foreach ($images as $path) {
                     $image = ProductImage::getNewInstance($product);
                     $this->importImage($image, $path);
                     unset($image);
                 }
             }
         }
         if (isset($fields['ProductOption']['options'])) {
             $options = explode('; ', $record[$fields['ProductOption']['options']]);
             if ($options) {
                 $product->deleteRelatedRecordSet('ProductOption');
                 foreach ($options as $option) {
                     $parts = explode(':', $option, 2);
                     if (count($parts) < 2) {
                         continue;
                     }
                     $optionInstance = ProductOption::getNewInstance($product);
                     $optionInstance->setValueByLang('name', null, trim($parts[0]));
                     $optionInstance->type->set(ProductOption::TYPE_SELECT);
                     $optionInstance->isDisplayed->set(true);
                     $optionInstance->save();
                     foreach (explode(',', $parts[1]) as $choice) {
                         $choiceInstance = ProductOptionChoice::getNewInstance($optionInstance);
                         $choiceInstance->setValueByLang('name', null, trim($choice));
                         $choiceInstance->save();
                     }
                 }
             }
         }
         // create variation by name
         if ((isset($fields['Product']['parentID']) || isset($fields['Parent']['parentSKU'])) && !isset($fields['ProductVariation']) && $product->parent->get()) {
             $this->importProductVariationValue($product, 1, $product->getValueByLang('name', 'en'));
         }
         // additional categories
         if (is_array($extraCategories)) {
             $this->importAdditionalCategories($profile, $product, $extraCategories);
         }
         if ($this->callback) {
             call_user_func($this->callback, $product);
         }
         $product->__destruct();
         $product->destruct(true);
         ActiveRecord::clearPool();
         return true;
     }
 }
Example #4
0
 public function addOptionChoice(ProductOptionChoice $choice)
 {
     if (!$choice->isLoaded()) {
         $choice->load();
     }
     foreach ($this->optionChoices as $key => $ch) {
         // already added?
         if ($ch->choice->get()->getID() == $choice->getID()) {
             return $ch;
         }
         // other choice from the same option - needs removal
         if ($ch->choice->get()->option->get()->getID() == $choice->option->get()->getID()) {
             $this->removeOptionChoice($ch->choice->get());
         }
     }
     $choice = OrderedItemOption::getNewInstance($this, $choice);
     $this->optionChoices[$choice->choice->get()->option->get()->getID()] = $choice;
     if ($this->isFinalized()) {
         $choice->updatePriceDiff();
         $this->price->set($this->price->get() + $this->reduceBaseTaxes($choice->priceDiff->get()));
     }
     return $choice;
 }
Example #5
0
 public function testMergeItemsWithOptions()
 {
     $product = $this->products[0];
     $option = ProductOption::getNewInstance($product);
     $option->save();
     $choice1 = ProductOptionChoice::getNewInstance($option);
     $choice1->save();
     $choice2 = ProductOptionChoice::getNewInstance($option);
     $choice2->save();
     $order = CustomerOrder::getNewInstance($this->user);
     $item = $order->addProduct($product, 1);
     $this->assertIsA($item, 'OrderedItem');
     $this->assertEqual(count($order->getShoppingCartItemCount()), 1);
     $order->addProduct($product, 1);
     $this->assertEqual(count($order->getItemsByProduct($product)), 2);
     $this->assertEqual($order->getShoppingCartItemCount(), 2);
     // merge without options
     $order->mergeItems();
     $this->assertEqual(count($order->getItemsByProduct($product)), 1);
     $this->assertEqual($order->getShoppingCartItemCount(), 2);
     // merge with options
     $item = array_shift($order->getItemsByProduct($product));
     $item->count->set(1);
     $item->addOptionChoice($choice1);
     $item2 = $order->addProduct($product, 1);
     $item2->addOptionChoice($choice2);
     $order->mergeItems();
     $this->assertEqual(count($order->getItemsByProduct($product)), 2);
     $this->assertEqual($order->getShoppingCartItemCount(), 2);
     // set same options
     $item2->removeOptionChoice($choice2);
     $item2->addOptionChoice($choice1);
     $order->mergeItems();
     $this->assertEqual(count($order->getItemsByProduct($product)), 1);
     $this->assertEqual($order->getShoppingCartItemCount(), 2);
 }
 /**
  * Sort product option choices
  *
  * @return JSONResponse
  */
 public function sortChoice()
 {
     $target = $this->request->get('target');
     foreach ($this->request->get($target, array()) as $position => $key) {
         if (!empty($key)) {
             $productOption = ProductOptionChoice::getInstanceByID((int) $key);
             $productOption->setFieldValue('position', (int) $position);
             $productOption->save();
         }
     }
     return new JSONResponse(false, 'success');
 }
Example #7
0
 public function set()
 {
     $request = $this->application->getRequest();
     $orderedItemID = $request->get('orderedItemID');
     $choiceID = $request->get('choiceID');
     $optionText = $request->get('optionText');
     if (!isset($orderedItemID)) {
         throw new Exception("Ordered item is required");
     }
     if (!isset($choiceID)) {
         throw new Exception("Choice item is required");
     }
     //throw new Exception(" response : " . $orderedItemID);
     //$orderItem = ActiveRecordModel::getInstanceByID('OrderedItem', $orderedItemID, ActiveRecordModel::LOAD_DATA, array('Product'));
     $orderItem = OrderedItem::getInstanceByID('OrderedItem', $orderedItemID, true, true);
     $orderItem->load(true);
     $orderItem->loadOptions();
     $product_option_choice = ProductOptionChoice::getInstanceByID($choiceID, true, true);
     $product_option_choice->load(true);
     if (!isset($product_option_choice)) {
         throw new Exception('Option not loaded');
     }
     $choice = $orderItem->addOptionChoice($product_option_choice);
     $orderItem->save();
     $response = new LiveCartSimpleXMLElement('<response datetime="' . date('c') . '"></response>');
     $response->addChild("message", "Success");
     if (isset($choice)) {
         $choice->load(true);
         if (isset($optionText)) {
             $choice->optionText->set($optionText);
             $choice->save();
         }
     }
     return new SimpleXMLResponse($response);
     //good
     /*$ordered_item = OrderedItemOption::getNewInstance($orderItem,$product_option_choice);
       $ordered_item->load(true);
       //$ordered_item->updatePriceDiff();
       $ordered_item->save();
       throw new Exception('is set --- ');*/
     /*$productOption = ProductOption::getInstanceByID($product_option_choice->optionID->get(), true);
             $productOption->load(true);
     
             $choice = $orderItem->getOptionChoice($productOption);
     
             if(isset($choice)) {
                 $choice->load(true);
                 $orderItem->removeOptionChoice($choice->choice->get());
                 $orderItem->save();
                 //$orderItem->removeOption($productOption);
                 //$orderItem->addOptionChoice($choice);
                 //$orderItem->save();
                 throw new Exception('is set');
                 //throw new Exception('is set ' . $choice->choice->get()->option->get()->getID());
             } else {
                 //$orderItem->addOption($productOption->option->get());
                 //$orderItem->addOptionChoice($productOption->getChoiceByID($product_option_choice->ID->get()));
                 //$orderItem->save();
     
                 $orderItem->addOptionChoice($product_option_choice);
                 $orderItem->save();
                 throw new Exception('is not set ');
             }*/
 }
Example #8
0
 public function addOptionChoice(ProductOptionChoice $choice)
 {
     if (!$choice->isLoaded()) {
         $choice->load();
     }
     foreach ($this->optionChoices as $key => $ch) {
         // already added?
         if ($ch->choice->get()->getID() == $choice->getID()) {
             return $ch;
         }
         // other choice from the same option - needs removal
         if ($ch->choice->get()->option->get()->getID() == $choice->option->get()->getID()) {
             $this->removedChoices[] = $ch;
             unset($this->optionChoices[$key]);
         }
     }
     $choice = OrderedItemOption::getNewInstance($this, $choice);
     $this->optionChoices[$choice->choice->get()->option->get()->getID()] = $choice;
     return $choice;
 }