public function testAvailability() { $this->config->setRuntime('INVENTORY_TRACKING', 'DISABLE'); $product1 = Product::getNewInstance($this->root); $product1->save(); $product2 = Product::getNewInstance($this->root); $product2->save(); ProductBundle::getNewInstance($this->container, $product1)->save(); ProductBundle::getNewInstance($this->container, $product2)->save(); // bundle container not enabled $this->assertFalse($this->container->isAvailable()); $this->container->isEnabled->set(true); $this->assertTrue($this->container->isAvailable()); // turn on inventory tracking $this->config->setRuntime('INVENTORY_TRACKING', 'ENABLE_AND_HIDE'); $product1->stockCount->set(2); $product1->save(); $product2->stockCount->set(2); $product2->save(); $this->assertTrue($this->container->isAvailable()); // remove inventory for one product $product2->stockCount->set(0); $product2->save(); $this->assertFalse($this->container->isAvailable()); }
public function setUp() { parent::setUp(); $this->product = Product::getNewInstance(Category::getRootNode(), 'test'); $this->product->save(); $this->user = User::getNewInstance('*****@*****.**'); $this->user->save(); }
public function setUp() { parent::setUp(); $this->product = Product::getNewInstance($this->rootCategory, 'test'); $this->product->save(); $this->group = ProductFileGroup::getNewInstance($this->product); $this->group->save(); // create temporary file file_put_contents($this->tmpFilePath, $this->fileBody); }
public function setUp() { parent::setUp(); // create a product without attributes $this->product = Product::getNewInstance($this->category, 'test'); $this->product->setValueByLang("name", "en", "TEST_PRODUCT"); $this->product->save(); $this->productAutoIncrementNumber = $this->product->getID(); for ($k = 1; $k < 4; $k++) { $currency = Currency::getNewInstance($k . 'zz'); $currency->save(); } }
public function setUp() { parent::setUp(); ActiveRecordModel::executeUpdate('DELETE FROM ProductRatingType'); $this->controller = new ProductController(self::getApplication()); $this->product = Product::getNewInstance(Category::getRootNode()); $this->product->isEnabled->set(true); $this->product->save(); $this->request->set('id', $this->product->getID()); self::getApplication()->getConfig()->set('ENABLE_REVIEWS', true); self::getApplication()->getConfig()->set('ENABLE_ANONYMOUS_RATINGS', true); self::getApplication()->getConfig()->set('REVIEWS_WITH_RATINGS', false); }
public function testContaining() { $root = Category::getRootNode(); $list = ProductList::getNewInstance($root); $list->save(); $product = Product::getNewInstance($root); $product->save(); $list->addProduct($product); $another = Product::getNewInstance($root); $another->save(); $this->assertTrue($list->contains($product)); $this->assertFalse($list->contains($another)); }
public function setUp() { parent::setUp(); $this->specField = SpecField::getNewInstance($this->rootCategory, SpecField::DATATYPE_TEXT, SpecField::TYPE_TEXT_SELECTOR); $this->specField->save(); $this->specFieldAutoIncrementNumber = $this->specField->getID(); $specFieldValue = SpecFieldValue::getNewInstance($this->specField); $specFieldValue->save(); $this->specFieldValueAutoIncrementNumber = $specFieldValue->getID(); $this->product = Product::getNewInstance($this->rootCategory, 'test'); $this->product->save(); $this->productAutoIncrementNumber = $this->product->getID(); }
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; } }
public function setUp() { parent::setUp(); Category::recalculateProductsCount(); $this->root = Category::getNewInstance(Category::getRootNode()); $this->root->save(); for ($k = 1; $k <= 2; $k++) { $cat = Category::getNewInstance($this->root); $cat->save(); $this->categories[$k] = $cat; } $this->product = Product::getNewInstance($this->categories[1]); $this->product->save(); $this->secondCategory = ProductCategory::getNewInstance($this->product, $this->categories[2]); $this->secondCategory->save(); }
public function testProductRatingTypes() { $subCategory = Category::getNewInstance(Category::getRootNode()); $subCategory->save(); $product = Product::getNewInstance($subCategory, 'test'); $product->save(); $rootType = ProductRatingType::getNewInstance(Category::getRootNode()); $rootType->save(); $subType = ProductRatingType::getNewInstance($subCategory); $subType->save(); $types = ProductRatingType::getProductRatingTypes($product); $this->assertEqual($types->size(), 2); // parent category types should go first $this->assertSame($types->get(0), $rootType); $this->assertSame($types->get(1), $subType); }
private function createOrder() { $user = User::getNewInstance('*****@*****.**'); $user->save(); $currency = Currency::getInstanceByID('USD'); $product = Product::getNewInstance(Category::getRootNode()); $product->isEnabled->set(true); $product->stockCount->set(100); $product->setPrice($currency, 100); $product->setValueByLang('name', null, 'Test name'); $product->setValueByLang('shortDescription', null, 'Really short description'); $product->save(); $order = CustomerOrder::getNewInstance($user); $order->addProduct($product, 1); $order->save(); return $order; }
public function testGetProductGroups() { // new product $product = Product::getNewInstance($this->rootCategory, 'test'); $product->save(); $groups = array(); foreach (range(1, 3) as $i) { $groups[$i] = ProductFileGroup::getNewInstance($product); $groups[$i]->position->set($i); $groups[$i]->setValueByLang('name', 'en', 'TEST_GROUP_' . $i); $groups[$i]->save(); } $this->assertEqual(count($groups), ProductFileGroup::getProductGroups($product)->getTotalRecordCount()); $i = 1; foreach (ProductFileGroup::getProductGroups($product) as $group) { $this->assertTrue($groups[$i] === $group); $i++; } }
public function testOrderTotalsWithRoundedPrices() { $currency = Currency::getNewInstance('RON'); $currency->setRoundingRule(0, Currency::TRIM, 0.09); $currency->save(); $product = Product::getNewInstance(Category::getRootNode()); $product->isEnabled->set(true); $product->setPrice($currency, 1.26); $product->save(); $order = CustomerOrder::getNewInstance(SessionUser::getAnonymousUser()); $order->addProduct($product); $order->save(true); $item = array_shift($order->getItemsByProduct($product)); $this->assertEquals($product->getPrice($currency), 1.29); $this->assertEquals($item->getSubTotal(), 1.29); $item->count->set(2); $this->assertEquals($item->getSubTotal(), 2.58); $this->assertEquals($order->getTotal(true), 2.58); // add another currency to mix - no rounding rules $bgn = Currency::getNewInstance('BGN'); $bgn->rate->set(2); $bgn->save(); $item->count->set(2); $order->changeCurrency($bgn); $this->assertEquals($product->getPrice($bgn), 0.63); $this->assertEquals($item->getPrice(), 0.63); $this->assertSame($item->getCurrency(), $bgn); $this->assertEquals($item->getSubTotal(), 1.26); $this->assertEquals($order->getTotal(true), 1.26); // add rounding rules $bgn->clearRoundingRules(); $bgn->setRoundingRule(0, Currency::TRIM, 0.07000000000000001); $order->changeCurrency($bgn); $this->assertEquals($product->getPrice($bgn), 0.67); $this->assertEquals($item->getSubTotal(), 1.34); $this->assertEquals($order->getTotal(true), 1.34); }
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; } }
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()); }
public function testTaxClassesWithDefaultZoneAndMultipleTaxes() { $order = CustomerOrder::getNewInstance($this->user); $zone = $order->getDeliveryZone(); $this->assertTrue($zone->isDefault()); // default tax level TaxRate::getNewInstance($zone, $this->tax, 10)->save(); $newTax = Tax::getNewInstance('test'); $newTax->save(); // diferent tax rate for books $books = TaxClass::getNewInstance('Books'); $books->save(); $booksRate = TaxRate::getNewInstance($zone, $this->tax, 5); $booksRate->taxClass->set($books); $booksRate->save(); $booksRate = TaxRate::getNewInstance($zone, $newTax, 20); $booksRate->taxClass->set($books); $booksRate->save(); // price = 100 $cd = $this->product; $book = Product::getNewInstance(Category::getRootNode()); $book->setPrice('USD', 50); $book->isEnabled->set(true); $book->taxClass->set($books); $book->save(); $order->addProduct($cd, 1, true); $order->addProduct($book, 1, true); $order->currency->set($this->currency); $order->save(); $this->assertEqual($order->getTaxAmount(), 19.41); $this->assertEqual($order->getTotal(true), 150); $service = ShippingService::getNewInstance($order->getDeliveryZone(), 'def', ShippingService::SUBTOTAL_BASED); $service->save(); $shippingRate = ShippingRate::getNewInstance($service, 0, 10000000); $shippingRate->flatCharge->set(100); $shippingRate->save(); $shipment = $order->getShipments()->get(0); $rates = $order->getDeliveryZone()->getShippingRates($shipment); $shipment->setAvailableRates($rates); $shipment->setRateId($rates->get(0)->getServiceID()); $shipment->save(); $this->assertEqual($order->getTotal(true), 250); $this->assertEqual((string) $order->getTaxAmount(), (string) 28.5); }
public function getNextProduct() { if (!$this->productSql) { $fields = array($this->getTablePrefix() . 'products.*'); $join = array(); foreach ($this->attributes as $attr) { $join[] = 'LEFT JOIN ' . $this->getTablePrefix() . 'product_options_values AS extra_' . $attr['optionID'] . ' ON (extra_' . $attr['optionID'] . '.productID=' . $this->getTablePrefix() . 'products.productID AND extra_' . $attr['optionID'] . '.optionID=' . $attr['optionID'] . ')'; $fields[] = 'extra_' . $attr['optionID'] . '.option_value AS extrafield_' . $attr['optionID']; } $validCats = implode(',', $this->categoryIds); $this->productSql = 'SELECT ' . implode(',', $fields) . ' FROM ' . $this->getTablePrefix() . 'products ' . implode(' ', $join) . ' LEFT JOIN ' . $this->getTablePrefix() . 'categories AS cat ON cat.categoryID=' . $this->getTablePrefix() . 'products.categoryID WHERE cat.categoryID IS NOT NULL AND cat.categoryID IN (' . $validCats . ')'; } if (!($data = $this->loadRecord($this->productSql))) { return null; } $rec = Product::getNewInstance(Category::getInstanceById($this->getRealId('Category', $data['categoryID']))); $rec->setID($data['productID']); $rec->keywords->set($data['meta_keywords']); $rec->setValueByLang('name', $this->defLang, $data['name']); $rec->setValueByLang('longDescription', $this->defLang, $data['description']); $rec->setValueByLang('shortDescription', $this->defLang, $data['brief_description']); foreach ($this->attributes as $attr) { if (!empty($data['extrafield_' . $attr['optionID']])) { $rec->setAttributeValueByLang(SpecField::getInstanceByID($this->getRealId('SpecField', $attr['optionID']), SpecField::LOAD_DATA), $this->defLang, $data['extrafield_' . $attr['optionID']]); } } $data['voteSum'] = round($data['customers_rating'] * $data['customer_votes']); if ($data['product_code']) { $rec->sku->set($data['product_code']); } foreach (array('shippingWeight' => 'weight', 'stockCount' => 'in_stock', 'shippingSurchargeAmount' => 'shipping_freight', 'minimumQuantity' => 'min_order_amount', 'dateCreated' => 'date_added', 'ratingSum' => 'voteSum', 'rating' => 'customers_rating', 'ratingCount' => 'customer_votes') as $lc => $xc) { $rec->{$lc}->set($data[$xc]); } $rec->isEnabled->set(1 == $data['enabled']); $rec->setPrice($this->getDefaultCurrency(), $data['Price']); if ($data['list_price']) { $price = $rec->getPricingHandler()->getPriceByCurrencyCode($this->getDefaultCurrency()); $price->listPrice->set($data['list_price']); } // images foreach ($this->getDataBySQL('SELECT * FROM ' . $this->getTablePrefix() . 'product_pictures WHERE productID=' . $data['productID'] . ' ORDER BY (photoID=' . (int) $data['default_picture'] . '), photoID ASC') as $image) { $file = $image['enlarged'] ? $image['enlarged'] : $image['filename']; $this->importProductImage($rec, $this->path . '/products_pictures/' . $file); } $rec->rawData = $data; return $rec; }
public function specFields() { $form = new Form($this->getValidator('specField')); if ($this->request->get('id')) { $product = Product::getInstanceByID((int) $this->request->get('id'), ActiveRecord::LOAD_DATA); } else { $cat = Category::getInstanceByID((int) $this->request->get('categoryID'), ActiveRecord::LOAD_DATA); $product = Product::getNewInstance($cat); } $product->loadSpecification(); $response = new BlockResponse('form', $form); $product->getSpecification()->setFormResponse($response, $form); $product->getSpecification()->setValidation($form->getValidator()); return $response; }
public function getNextProduct() { if (!$this->productSql) { foreach ($this->languages as $id => $code) { list($join[], $langs[]) = $this->joinProductFields($id, $code); } $this->productSql = 'SELECT *,' . implode(', ', $langs) . ' FROM ' . $this->getTablePrefix() . 'products ' . implode(' ', $join) . ' LEFT JOIN ' . $this->getTablePrefix() . 'products_to_categories ON ' . $this->getTablePrefix() . 'products.products_id=' . $this->getTablePrefix() . 'products_to_categories.products_id LEFT JOIN ' . $this->getTablePrefix() . 'categories ON ' . $this->getTablePrefix() . 'products_to_categories.categories_id=' . $this->getTablePrefix() . 'categories.categories_id WHERE ' . $this->getTablePrefix() . 'categories.categories_id IS NOT NULL GROUP BY ' . $this->getTablePrefix() . 'products.products_id'; } if (!($data = $this->loadRecord($this->productSql))) { return null; } $rec = Product::getNewInstance(Category::getInstanceById($this->getRealId('Category', $data['categories_id']))); $rec->setID($data['products_id']); foreach ($this->languages as $code) { $rec->setValueByLang('name', $code, $data['name_' . $code]); $rec->setValueByLang('longDescription', $code, $data['descr_' . $code]); // use the first line or paragraph of the long description as the short description $short = array_shift(preg_split("/\n|\\<br/", $data['descr_' . $code])); $rec->setValueByLang('shortDescription', $code, $short); } if ($data['manufacturers_id']) { try { $man = Manufacturer::getInstanceById($this->getRealId('Manufacturer', $data['manufacturers_id']), true); $man->load(); $rec->manufacturer->set($man); } catch (Exception $e) { // orphan data } } $rec->sku->set($data['products_model']); $rec->URL->set($data['products_url']); $rec->isEnabled->set((int) (1 == $data['products_status'])); $rec->shippingWeight->set($data['products_weight']); $rec->stockCount->set($data['products_quantity']); $rec->dateCreated->set($data['products_date_added']); $rec->setPrice($this->getConfigValue('DEFAULT_CURRENCY'), $data['products_price']); //product image if ($data['products_image']) { $this->importProductImage($rec, $this->path . '/images/' . $data['products_image']); } $rec->rawData = $data; return $rec; }
public function testDiscountForCategoryProductsBySubTotal() { $category = Category::getNewInstance(Category::getRootNode()); $category->save(); $newProduct = Product::getNewInstance($category); $newProduct->isEnabled->set(true); $newProduct->setPrice($this->usd, 100); $newProduct->save(); Category::recalculateProductsCount(); $newProduct->reload(); // order condition $condition = DiscountCondition::getNewInstance(); $condition->isEnabled->set(true); $condition->subTotal->set(300); $condition->comparisonType->set(DiscountCondition::COMPARE_GTEQ); $condition->conditionClass->set('RuleConditionContainsProduct'); $condition->save(); $record = DiscountConditionRecord::getNewInstance($condition, $category); $record->save(); $action = DiscountAction::getNewInstance($condition); $action->actionCondition->set($condition); $action->isEnabled->set(true); $action->type->set(DiscountAction::TYPE_ITEM_DISCOUNT); $action->amount->set(10); $action->actionClass->set('RuleActionPercentageDiscount'); $action->save(); $this->order->addProduct($this->products[0], 3, true); $this->order->addProduct($newProduct, 2, true); $this->order->save(); $price0 = $this->products[0]->getPrice($this->usd); $price1 = $newProduct->getPrice($this->usd); $expectedTotal = $price0 * 3 + $price1 * 0.9 * 2; $normalPrice = $price0 * 3 + $price1 * 2; $this->assertEquals($normalPrice, $this->order->getTotal(true)); // require subtotal to be at least 150 (we have 200) $condition->subTotal->set(150); $condition->save(); $this->assertEquals(count($this->order->getDiscountActions(true)), 1); $this->assertEquals($expectedTotal, $this->order->getTotal(true)); }
public function testTextRulesSpecField() { $field = SpecField::getNewInstance(Category::getRootNode(), SpecField::DATATYPE_TEXT, SpecField::TYPE_TEXT_ADVANCED); $field->save(); $product = Product::getNewInstance($this->categories['1.2.1']); $product->sku->set('testing'); $product->save(); $product->setAttributeValueByLang($field, $this->getApplication()->getDefaultLanguageCode(), 'Attribute value'); $product->save(); $rule = ClonedStoreRule::getNewInstance($this->stores[0], ClonedStoreRule::TYPE_TEXT); $rule->field->set('specField.' . $field->getID()); $rule->find->set('value'); $rule->repl->set('replaced'); $rule->save(); $updater = $this->getUpdater(); $updater->syncAll(); $row = ActiveRecord::getDataBySQL('SELECT * FROM ' . $updater->getImportDatabase() . '.SpecificationStringValue WHERE productID=' . $product->getID()); $row = array_shift($row); $name = unserialize($row['value']); $this->assertEqual($name[$this->getApplication()->getDefaultLanguageCode()], 'Attribute replaced'); }
/** * @role create */ public function create() { $product = Product::getNewInstance(Category::getInstanceByID($this->request->get('categoryID')), $this->translate('_new_product')); $response = $this->save($product); if ($response instanceof ActionResponse) { $response->get('productForm')->clearData(); $response->set('id', $product->getID()); return $response; } else { return $response; } }
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)); }
public function getNextProduct() { if (!$this->productSql) { foreach ($this->languages as $code => $id) { list($join[], $langs[]) = $this->joinProductFields($id, $code); } foreach ($this->attributes as $attr) { $join[] = 'LEFT JOIN ' . $this->getTablePrefix() . 'extra_field_values AS extra_' . $attr['fieldid'] . ' ON (extra_' . $attr['fieldid'] . '.productid=' . $this->getTablePrefix() . 'products.productid AND extra_' . $attr['fieldid'] . '.fieldid=' . $attr['fieldid'] . ')'; $langs[] = 'extra_' . $attr['fieldid'] . '.value AS extrafield_' . $attr['fieldid']; } $this->productSql = 'SELECT ' . $this->getTablePrefix() . 'products.*, ' . $this->getTablePrefix() . 'products_categories.categoryid ' . ($langs ? ',' : '') . implode(', ', $langs) . ', (SELECT price FROM ' . $this->getTablePrefix() . 'pricing WHERE ' . $this->getTablePrefix() . 'pricing.productid=' . $this->getTablePrefix() . 'products.productid ORDER BY quantity ASC LIMIT 1) AS price FROM ' . $this->getTablePrefix() . 'products ' . implode(' ', $join) . ' LEFT JOIN ' . $this->getTablePrefix() . 'products_categories ON (' . $this->getTablePrefix() . 'products.productid=' . $this->getTablePrefix() . 'products_categories.productid AND ' . $this->getTablePrefix() . 'products_categories.main="Y")'; } if (!($data = $this->loadRecord($this->productSql))) { return null; } $rec = Product::getNewInstance(Category::getInstanceById($this->getRealId('Category', $data['categoryid']))); $rec->setID($data['productid']); $rec->keywords->set($data['keywords']); $rec->setValueByLang('name', $this->defLang, $data['product']); $rec->setValueByLang('longDescription', $this->defLang, $data['fulldescr']); $rec->setValueByLang('shortDescription', $this->defLang, $data['descr']); foreach ($this->languages as $code => $id) { $rec->setValueByLang('name', $code, $data['name_' . $code]); $rec->setValueByLang('longDescription', $code, $data['fulldescr_' . $code]); $rec->setValueByLang('shortDescription', $code, $data['descr_' . $code]); } foreach ($this->attributes as $attr) { if (!empty($data['extrafield_' . $attr['fieldid']])) { $rec->setAttributeValueByLang(SpecField::getInstanceByID($this->getRealId('SpecField', $attr['fieldid']), SpecField::LOAD_DATA), $this->defLang, $data['extrafield_' . $attr['fieldid']]); } } if ($data['manufacturerid']) { $rec->manufacturer->set(Manufacturer::getInstanceById($this->getRealId('Manufacturer', $data['manufacturerid']))); } foreach (array('sku' => 'productcode', 'shippingWeight' => 'weight', 'stockCount' => 'avail', 'shippingSurchargeAmount' => 'shipping_freight', 'minimumQuantity' => 'min_amount', 'dateCreated' => 'add_date') as $lc => $xc) { $rec->{$lc}->set($data[$xc]); } $rec->isEnabled->set('Y' == $data['forsale']); $rec->setPrice($this->getDefaultCurrency(), $data['price']); //images $images = array_merge($this->getDataBySQL('SELECT * FROM ' . $this->getTablePrefix() . 'images_t WHERE id=' . $data['productid'] . ' ORDER BY orderby ASC'), $this->getDataBySQL('SELECT * FROM ' . $this->getTablePrefix() . 'images_d WHERE id=' . $data['productid'] . ' ORDER BY orderby ASC')); foreach ($images as $image) { $this->importProductImage($rec, $this->path . '/' . $image['image_path']); } $rec->rawData = $data; return $rec; }
public function testIsProductMatching() { $condition = DiscountCondition::getNewInstance(); $condition->isEnabled->set(true); $condition->conditionClass->set('RuleConditionContainsProduct'); $condition->save(); $record = DiscountConditionRecord::getNewInstance($condition, $this->product1); $record->save(); $condition->loadAll(); $ruleCondition = RuleCondition::createFromArray($condition->toArray()); $this->assertFalse($ruleCondition->isProductMatching($this->product2)); $this->assertTrue($ruleCondition->isProductMatching($this->product1)); // check matching by manufacturer $manufacturer = Manufacturer::getNewInstance('testing'); $manufacturer->save(); $this->product2->manufacturer->set($manufacturer); $record = DiscountConditionRecord::getNewInstance($condition, $manufacturer); $record->save(); $condition->loadAll(); $ruleCondition = RuleCondition::createFromArray($condition->toArray()); $this->assertTrue($ruleCondition->isProductMatching($this->product2)); // check matching by category $newCategory = Category::getNewInstance(Category::getRootNode()); $newCategory->save(); $newSubCategory = Category::getNewInstance($newCategory); $newSubCategory->save(); $newProduct = Product::getNewInstance($newSubCategory); $newProduct->save(); $newProduct->reload(); $record = DiscountConditionRecord::getNewInstance($condition, $newCategory); $record->save(); $this->assertFalse($ruleCondition->isProductMatching($newProduct)); $condition->loadAll(); $ruleCondition = RuleCondition::createFromArray($condition->toArray()); $this->assertTrue($ruleCondition->isProductMatching($newProduct)); // sub-condition $sub = DiscountCondition::getNewInstance($condition); $sub->isEnabled->set(true); $sub->conditionClass->set('RuleConditionContainsProduct'); $sub->save(); $condition->isAllSubconditions->set(true); $condition->save(); $condition->loadAll(); $manufacturer = Manufacturer::getNewInstance('new one'); $manufacturer->save(); $record = DiscountConditionRecord::getNewInstance($sub, $manufacturer); $record->save(); $sub->loadAll(); $subCondition = RuleCondition::createFromArray($sub->toArray()); $this->assertFalse($subCondition->isProductMatching($newProduct)); $ruleCondition = RuleCondition::createFromArray($condition->toArray()); $this->assertFalse($ruleCondition->isProductMatching($newProduct)); $newProduct->manufacturer->set($manufacturer); $newProduct->save(); $this->assertTrue($ruleCondition->isProductMatching($newProduct)); // sub-sub condition $sub->isAllSubconditions->set(false); $sub->save(); for ($k = 1; $k <= 2; $k++) { $subs[$k] = DiscountCondition::getNewInstance($sub); $subs[$k]->isEnabled->set(true); $subs[$k]->conditionClass->set('RuleConditionContainsProduct'); } // false $subs[1]->save(); $someManufacturer = Manufacturer::getNewInstance('Manufacturer without products'); $someManufacturer->save(); $record = DiscountConditionRecord::getNewInstance($subs[1], $someManufacturer); $record->save(); $subs[1]->loadAll(); $sub->loadAll(); $condition->loadAll(); $this->assertFalse(RuleCondition::create($subs[1])->isProductMatching($newProduct)); $this->assertFalse(RuleCondition::create($condition)->isProductMatching($newProduct)); // true $subs[2]->save(); $record = DiscountConditionRecord::getNewInstance($subs[2], $newProduct); $record->save(); $subs[2]->loadAll(); $sub->loadAll(); $condition->loadAll(); $this->assertTrue(RuleCondition::create($condition)->isProductMatching($newProduct)); }
public static function getRequestInstance(Request $request, $field = 'ID') { $product = parent::getRequestInstance($request, $field); if (!$product->getID()) { $data = $request->getJSON(); $category = Category::getRequestInstance($request, $field); $product = Product::getNewInstance($category); } return $product; }
protected function initOrder() { // set up currency $this->setUpCurrency(); $this->usd->decimalCount->set(2); $this->usd->clearRoundingRules(); $this->usd->save(); // initialize order ActiveRecordModel::executeUpdate('DELETE FROM User WHERE email="*****@*****.**"'); $user = User::getNewInstance('*****@*****.**'); $user->save(); $this->user = $user; $address = UserAddress::getNewInstance(); $address->countryID->set('US'); $state = State::getInstanceById(1, State::LOAD_DATA); $address->state->set(State::getInstanceById(1)); $address->postalCode->set(90210); $address->save(); $billing = BillingAddress::getNewInstance($user, $address); $billing->save(); $address = clone $address; $address->save(); $shipping = ShippingAddress::getNewInstance($user, $address); $shipping->save(); $this->order = CustomerOrder::getNewInstance($user); $this->order->shippingAddress->set($shipping->userAddress->get()); $this->order->billingAddress->set($billing->userAddress->get()); // set up products $product = Product::getNewInstance(Category::getInstanceById(Category::ROOT_ID), 'test1'); $product->save(); $product->setPrice('USD', 100); $product->stockCount->set(20); $product->isEnabled->set(1); $product->save(); $this->products[] = $product; $product = Product::getNewInstance(Category::getInstanceById(Category::ROOT_ID), 'test2'); $product->save(); $product->setPrice('USD', 200); $product->stockCount->set(20); $product->isEnabled->set(1); $product->save(); $this->products[] = $product; $product = Product::getNewInstance(Category::getInstanceById(Category::ROOT_ID), 'test3'); $product->save(); $product->setPrice('USD', 400); $product->isSeparateShipment->set(true); $product->stockCount->set(20); $product->isEnabled->set(1); $product->save(); $this->products[] = $product; }