public function testGetCustomAttributesMetadata() { $filterMock = $this->getMock('Magento\\Framework\\Service\\V1\\Data\\Filter', [], [], '', false); $this->filterBuilderMock->expects($this->once())->method('setField')->with('attribute_set_id')->willReturnSelf(); $this->filterBuilderMock->expects($this->once())->method('setValue')->with(\Magento\Catalog\Api\Data\CategoryAttributeInterface::DEFAULT_ATTRIBUTE_SET_ID)->willReturnSelf(); $this->filterBuilderMock->expects($this->once())->method('create')->willReturn($filterMock); $this->searchBuilderMock->expects($this->once())->method('addFilter')->with([$filterMock])->willReturnSelf(); $searchCriteriaMock = $this->getMock('Magento\\Framework\\Api\\SearchCriteria', [], [], '', false); $this->searchBuilderMock->expects($this->once())->method('create')->willReturn($searchCriteriaMock); $itemMock = $this->getMock('Magento\\Framework\\Object', [], [], '', false); $this->attributeRepositoryMock->expects($this->once())->method('getList')->with(\Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE, $searchCriteriaMock)->willReturn($this->searchResultMock); $this->searchResultMock->expects($this->once())->method('getItems')->willReturn([$itemMock]); $this->metadataConfigMock->expects($this->once())->method('getCustomAttributesMetadata')->with(null)->willReturn(['attribute']); $expected = array_merge([$itemMock], ['attribute']); $this->assertEquals($expected, $this->model->getCustomAttributesMetadata(null)); }
public function testGetsAttributeEntityValues() { $a1 = 1; $a2 = 2; $e1 = 10; $v1 = '13'; $v2 = '222'; $category = CustomAttributeCategory::USER; $entityIds = array(1, 4, 6, $e1); $row1 = $this->GetAttributeValueRow($a1, $e1, $v1); $row2 = $this->GetAttributeValueRow($a2, $e1, $v2); $this->db->SetRows(array($row1, $row2)); $values = $this->repository->GetEntityValues($category, $entityIds); $this->assertEquals(new GetAttributeMultipleValuesCommand($category, $entityIds), $this->db->_LastCommand); $this->assertEquals($a1, $values[0]->AttributeId); $this->assertEquals($e1, $values[0]->EntityId); $this->assertEquals($v1, $values[0]->Value); $this->assertEquals($a2, $values[1]->AttributeId); $this->assertEquals($e1, $values[1]->EntityId); $this->assertEquals($v2, $values[1]->Value); }
public static function redirectUrl($originalUrl, array $filters) { $params = array(); if (isset($filters['price'])) { $price = $filters['price']; $minPriceKey = self::FROM_PRICE_MIN; if (isset($price[$minPriceKey]) && is_numeric($price[$minPriceKey])) { $params[self::TO_PRICE_MIN] = $price[$minPriceKey] > 0 ? $price[$minPriceKey] : null; } $maxPriceKey = self::FROM_PRICE_MAX; if (isset($price[$maxPriceKey]) && is_numeric($price[$maxPriceKey]) && $price[$maxPriceKey] > 0) { //TODO: set to null if max price $params[self::TO_PRICE_MAX] = $price[$maxPriceKey]; } } if (isset($filters['attributes']) && is_array($attributes = $filters['attributes'])) { $aRepo = new AttributeRepository(); $out = $aRepo->get(array_keys($attributes), 'getAttributesBySrls'); $objects = array(); foreach ($out as $o) { $objects[$o->attribute_srl] = $o; } unset($out); foreach ($attributes as $srl => $filterValue) { if (array_key_exists($srl, $objects)) { /** @var $attribute Attribute */ $attribute = $objects[$srl]; if ($filterValue) { if ($attribute->isNumeric()) { if (is_array($filterValue)) { if (isset($filterValue['min']) && ctype_digit($filterValue['min']) && $filterValue['min']) { $key = str_replace('SRL', $srl, self::TO_ATTRIBUTE_NUMERIC_MIN); if ($filterValue['min'] != $attribute->getMinValue()) { $params[$key] = $filterValue['min']; } } if (isset($filterValue['max']) && ctype_digit($filterValue['max']) && $filterValue['max']) { $key = str_replace('SRL', $srl, self::TO_ATTRIBUTE_NUMERIC_MAX); if ($filterValue['max'] != $attribute->getMaxValue()) { $params[$key] = $filterValue['max']; } } } } elseif ($attribute->isSelect()) { $key = str_replace('SRL', $srl, self::TO_ATTRIBUTE_SELECT); $params[$key] = $filterValue; } elseif ($attribute->isMultipleSelect()) { $key = str_replace('SRL', $srl, self::TO_ATTRIBUTE_SELECT_MULTIPLE); $params[$key] = implode(self::SEPARATOR_MULTIPLE, $filterValue); } } } } } //force it go to dispShop $params = array_merge(array('act' => 'dispShop'), $params); //unset empty filters meant to be removed $originalQuery = parse_url($originalUrl, PHP_URL_QUERY); parse_str($originalQuery, $originalQueryParts); $newQueryParts = array_merge($originalQueryParts, $params); $patterns = array(self::TO_ATTRIBUTE_NUMERIC_MIN, self::TO_ATTRIBUTE_NUMERIC_MAX, self::TO_ATTRIBUTE_SELECT, self::TO_ATTRIBUTE_SELECT_MULTIPLE); foreach ($newQueryParts as $k => $p) { if (!isset($params[$k])) { foreach ($patterns as $pattern) { if (preg_match("/" . str_replace('SRL', '(\\d+)', $pattern) . "/i", $k)) { unset($newQueryParts[$k]); } } } } $newQuery = http_build_query($newQueryParts); $goto = $originalQuery ? str_replace("?{$originalQuery}", "?{$newQuery}", $originalUrl) : FrontFilters::http_build_url($originalUrl, array('query' => http_build_query($params)), HTTP_URL_JOIN_QUERY); return $goto; }
/** * Gets attribute max value (comparing all products that have the numeric $attribute associated) * @return int|mixed */ public function getMaxValue() { return $this->repo->getAttributeMaxValue($this->module_srl, $this->attribute_srl); }
/** * insert attribute * @return Object */ public function procShopToolInsertAttribute() { $repository = new AttributeRepository(); $args = Context::getRequestVars(); $args->is_filter = (int)(bool)$args->is_filter; if ($args->is_filter) { if (!in_array($args->type, array(AttributeRepository::TYPE_SELECT, AttributeRepository::TYPE_SELECT_MULTIPLE, AttributeRepository::TYPE_NUMERIC))) { $args->is_filter = 0; } } $args->module_srl = $this->module_info->module_srl; $logged_info = Context::get('logged_info'); $args->member_srl = $logged_info->member_srl; $attribute = new Attribute($args); $attribute->module_srl = $this->module_srl; try { if ($attribute->attribute_srl) { $repository->updateAttribute($attribute); $this->setMessage("success_updated"); } else { $output = $repository->insertAttribute($attribute); $this->setMessage("success_registed"); } } catch (Exception $e) { return new Object(-1, $e->getMessage()); } $returnUrl = getNotEncodedUrl('', 'act', 'dispShopToolManageAttributes'); $this->setRedirectUrl($returnUrl); }
/** * Returns Attributes displayable for a specific category / set of categories, or for no category. * * @param $module_srl * @param array $category_srls * @param bool $withParents takes categories parents into account * @return mixed * @throws ShopException */ public function getFilterAttributes($module_srl, array $category_srls = null, $withParents = true) { if (!is_numeric($module_srl)) { throw new ShopException('module_srl must be a valid natural number'); } $catSerials = array(); if ($withParents && !empty($category_srls)) { $cRepo = new CategoryRepository(); $paths = $cRepo->getCategoryPaths($category_srls); foreach ($paths as $srl => $path) { $catSerials = array_merge($catSerials, $path, array($srl)); } } else { $catSerials = $category_srls; } $params = array('module_srl' => $module_srl); if (!empty($catSerials)) { $params['category_srls'] = $catSerials; } $aRepo = new AttributeRepository(); $attributes = $aRepo->get(null, 'getCategoriesAttributeFilters', null, $params); return $attributes; }
/** * Frontend shop product page */ public function dispShopProduct() { /** @var $product Product */ if ($product = Context::get('product')) { //came from from routing if (!($product instanceof Product) || !$product->isPersisted()) throw new ShopException('Wrong product'); } else { if (!$product_srl = Context::get('product_srl')) throw new ShopException('Could not identify product'); $product_repository = new ProductRepository(); $product = $product_repository->getProduct($product_srl); if (!$product instanceof Product) throw new ShopException('Product does not exist'); Context::set('product', $product); } //add product document if it does not exist (link to comments) if(!isset($product->document_srl)){ $documentController = getController('document'); $document = new stdClass(); $document->title = $product->product_srl; $document->commentStatus = 'ALLOW'; $document->module_srl = $product->module_srl; $output = $documentController->insertDocument($document); $product->document_srl = $output->variables['document_srl']; unset($product->images); $product->repo->updateProduct($product); } $documentModel = getModel('document'); $product->document = $documentModel->getDocument($product->document_srl); $product->comment_list = $_comment_list = $product->document->getComments(); if (is_array($_comment_list)) { foreach ($product->comment_list as $comment){ $comment->variables['relativeDate'] = $this->model->zdateRelative($comment->getRegdateTime()); } } // Setup Javscript datasource for linked dropdowns $datasourceJS = $this->getAssociatedProductsAttributesAsJavascriptArray(array($product)); Context::set('datasourceJS', $datasourceJS); // Setup attributes names for display if(count($product->attributes)) { $attribute_repository = new AttributeRepository(); $attributes = $attribute_repository->getAttributes(array_keys($product->attributes)); Context::set('attributes', $attributes); } // Categories left tree // Retrieve existing categories $category_repository = new CategoryRepository(); $tree = $category_repository->getNavigationCategoriesTree($this->module_srl); // Prepare tree for display $tree_config = new HtmlCategoryTreeConfig(); $tree_config->linkCategoryName = TRUE; $tree_config->linkGetUrlParams = array('vid', $this->mid, 'act', 'dispShop'); $tree_config->selected = $product->categories; $HTML_tree = $tree->toHTML($tree_config); Context::set('HTML_tree', $HTML_tree); $this->loadShopCategoryTree(); $this->setTemplateFile('product.html'); }