Exemplo n.º 1
0
 /**
  * Prepare search condition for attribute
  *
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute
  * @param string|array $value
  * @return string|array
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function prepareCondition($attribute, $value)
 {
     $condition = false;
     if (is_array($value)) {
         if ($attribute->getBackendType() == 'varchar') {
             // multiselect
             // multiselect
             $condition = ['in_set' => $value];
         } elseif (!isset($value['from']) && !isset($value['to'])) {
             // select
             // select
             $condition = ['in' => $value];
         } elseif (isset($value['from']) && '' !== $value['from'] || isset($value['to']) && '' !== $value['to']) {
             // range
             $condition = $value;
         }
     } else {
         if (strlen($value) > 0) {
             if (in_array($attribute->getBackendType(), ['varchar', 'text', 'static'])) {
                 $condition = ['like' => '%' . $value . '%'];
                 // text search
             } else {
                 $condition = $value;
             }
         }
     }
     return $condition;
 }
Exemplo n.º 2
0
 /**
  * Load counfigurable attribute by product and product's attribute
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute  $attribute
  * @return void
  */
 public function loadByProductAndAttribute($product, $attribute)
 {
     $id = $this->_getResource()->getIdByProductIdAndAttributeId($this, $product->getId(), $attribute->getId());
     if ($id) {
         $this->load($id);
     }
 }
Exemplo n.º 3
0
 public function testIndexerAfterDeleteAttribute()
 {
     $this->_processor->expects($this->once())->method('markIndexerAsInvalid');
     $this->_model->setOrigData('id', 2);
     $this->_model->setOrigData('used_in_product_listing', 1);
     $this->_model->delete();
 }
Exemplo n.º 4
0
 public function testAddFiltersVerifyAddConditionsToRegistry()
 {
     $registry = new \Magento\Framework\Registry();
     $values = array('sku' => 'simple');
     $this->skuAttribute->expects($this->once())->method('getTable')->will($this->returnValue('catalog_product_entity'));
     $this->collection->expects($this->any())->method('addAttributeToSelect')->will($this->returnSelf());
     $this->collection->expects($this->any())->method('setStore')->will($this->returnSelf());
     $this->collection->expects($this->any())->method('addMinimalPrice')->will($this->returnSelf());
     $this->collection->expects($this->any())->method('addTaxPercents')->will($this->returnSelf());
     $this->collection->expects($this->any())->method('addStoreFilter')->will($this->returnSelf());
     $this->collection->expects($this->any())->method('setVisibility')->will($this->returnSelf());
     $this->resource->expects($this->any())->method('prepareCondition')->will($this->returnValue(array('like' => '%simple%')));
     $this->resource->expects($this->any())->method('getIdFieldName')->will($this->returnValue('entity_id'));
     $this->engine->expects($this->any())->method('getResource')->will($this->returnValue($this->resource));
     $this->engine->expects($this->any())->method('getAdvancedResultCollection')->will($this->returnValue($this->collection));
     $this->engineProvider->expects($this->any())->method('get')->will($this->returnValue($this->engine));
     $this->attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue('sku'));
     $this->attribute->expects($this->any())->method('getStoreLabel')->will($this->returnValue('SKU'));
     $this->attribute->expects($this->any())->method('getFrontendInput')->will($this->returnValue('text'));
     $this->attribute->expects($this->any())->method('getBackend')->will($this->returnValue($this->skuAttribute));
     $this->attribute->expects($this->any())->method('getBackendType')->will($this->returnValue('static'));
     $this->dataCollection->expects($this->any())->method('getIterator')->will($this->returnValue(new \ArrayIterator(array($this->attribute))));
     $objectManager = new \Magento\TestFramework\Helper\ObjectManager($this);
     /** @var \Magento\CatalogSearch\Model\Advanced $instance */
     $instance = $objectManager->getObject('Magento\\CatalogSearch\\Model\\Advanced', array('registry' => $registry, 'engineProvider' => $this->engineProvider, 'data' => array('attributes' => $this->dataCollection)));
     $instance->addFilters($values);
     $this->assertNotNull($registry->registry('advanced_search_conditions'));
 }
Exemplo n.º 5
0
 /**
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute $subject
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute
  * @return \Magento\Catalog\Model\Resource\Eav\Attribute
  *
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function afterDelete(\Magento\Catalog\Model\Resource\Eav\Attribute $subject, \Magento\Catalog\Model\Resource\Eav\Attribute $attribute)
 {
     if ($attribute->getIsUsedForPromoRules()) {
         $this->checkCatalogRulesAvailability($attribute->getAttributeCode());
     }
     return $attribute;
 }
Exemplo n.º 6
0
 /**
  * Redefine Attribute scope
  *
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute
  * @return $this
  */
 public function setScope($attribute)
 {
     if ($this->_helper->isPriceGlobal()) {
         $attribute->setIsGlobal(\Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_GLOBAL);
     } else {
         $attribute->setIsGlobal(\Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_WEBSITE);
     }
     return $this;
 }
Exemplo n.º 7
0
 /**
  * Get Attribute Filter Class Name
  *
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute
  * @return string
  */
 protected function getAttributeFilterClass(\Magento\Catalog\Model\Resource\Eav\Attribute $attribute)
 {
     $filterClassName = $this->filterTypes[self::ATTRIBUTE_FILTER];
     if ($attribute->getAttributeCode() == 'price') {
         $filterClassName = $this->filterTypes[self::PRICE_FILTER];
     } elseif ($attribute->getBackendType() == 'decimal') {
         $filterClassName = $this->filterTypes[self::DECIMAL_FILTER];
     }
     return $filterClassName;
 }
Exemplo n.º 8
0
 /**
  * Return Google Content Attribute Type By Product Attribute
  *
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute
  * @return string Google Content Attribute Type
  */
 public function getGcontentAttributeType($attribute)
 {
     $typesMapping = ['price' => self::ATTRIBUTE_TYPE_FLOAT, 'decimal' => self::ATTRIBUTE_TYPE_INT];
     if (isset($typesMapping[$attribute->getFrontendInput()])) {
         return $typesMapping[$attribute->getFrontendInput()];
     } elseif (isset($typesMapping[$attribute->getBackendType()])) {
         return $typesMapping[$attribute->getBackendType()];
     } else {
         return self::ATTRIBUTE_TYPE_TEXT;
     }
 }
Exemplo n.º 9
0
 /**
  * Add additional data to attribute.
  *
  * @param Attribute $attribute
  * @return bool
  */
 public function overrideAttribute(Attribute $attribute)
 {
     if (!empty($this->_attributeOverrides[$attribute->getAttributeCode()])) {
         $data = $this->_attributeOverrides[$attribute->getAttributeCode()];
         if (isset($data['options_method']) && method_exists($this, $data['options_method'])) {
             $data['filter_options'] = $this->{$data['options_method']}();
         }
         $attribute->addData($data);
         return true;
     }
     return false;
 }
Exemplo n.º 10
0
 public function testUpdateAttributes()
 {
     $productIds = [1, 2, 2, 4];
     $productIdsUnique = [0 => 1, 1 => 2, 3 => 4];
     $attrData = [1];
     $storeId = 1;
     $this->resource->expects($this->any())->method('updateAttributes')->with($productIds, $attrData, $storeId)->will($this->returnSelf());
     $this->categoryIndexer->expects($this->any())->method('isScheduled')->will($this->returnValue(false));
     $this->categoryIndexer->expects($this->any())->method('reindexList')->will($this->returnValue($productIds));
     $this->prepareIndexer();
     $this->eavConfig->expects($this->any())->method('getAttribute')->will($this->returnValue($this->eavAttribute));
     $this->eavAttribute->expects($this->any())->method('isIndexable')->will($this->returnValue(false));
     $this->assertEquals($this->model, $this->model->updateAttributes($productIds, $attrData, $storeId));
     $this->assertEquals($this->model->getDataByKey('product_ids'), $productIdsUnique);
     $this->assertEquals($this->model->getDataByKey('attributes_data'), $attrData);
     $this->assertEquals($this->model->getDataByKey('store_id'), $storeId);
 }
Exemplo n.º 11
0
 /**
  * @dataProvider validateDataProvider
  *
  * @param string $attributeValue
  * @param string|array $parsedValue
  * @param string $newValue
  * @param string $operator
  * @param array $input
  */
 public function testValidateWithDatetimeValue($attributeValue, $parsedValue, $newValue, $operator, $input)
 {
     $this->product->setData('attribute', 'attribute_key');
     $this->product->setData('value_parsed', $parsedValue);
     $this->product->setData('operator', $operator);
     $this->config->expects($this->any())->method('getAttribute')->will($this->returnValue($this->eavAttributeResource));
     $this->eavAttributeResource->expects($this->any())->method('isScopeGlobal')->will($this->returnValue(false));
     $this->eavAttributeResource->expects($this->any())->method($input['method'])->will($this->returnValue($input['type']));
     $this->productModel->expects($this->any())->method('hasData')->will($this->returnValue(true));
     $this->productModel->expects($this->at(0))->method('getData')->will($this->returnValue(['1' => ['1' => $attributeValue]]));
     $this->productModel->expects($this->any())->method('getData')->will($this->returnValue($newValue));
     $this->productModel->expects($this->any())->method('getId')->will($this->returnValue('1'));
     $this->productModel->expects($this->once())->method('getStoreId')->will($this->returnValue('1'));
     $this->productModel->expects($this->any())->method('getResource')->will($this->returnValue($this->productResource));
     $this->productResource->expects($this->any())->method('getAttribute')->will($this->returnValue($this->eavAttributeResource));
     $this->product->collectValidatedAttributes($this->productModel);
     $this->assertTrue($this->product->validate($this->productModel));
 }
Exemplo n.º 12
0
 /**
  * Return Product Attribute Store Label
  * Set attribute name like frontend lable for custom attributes (which wasn't defined by Google)
  *
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute
  * @param int $storeId Store View Id
  * @return string Attribute Store View Label or Attribute code
  */
 public function getAttributeLabel($attribute, $storeId)
 {
     $attributeId = $attribute->getId();
     $frontendLabel = $attribute->getFrontend()->getLabel();
     if (is_array($frontendLabel)) {
         $frontendLabel = array_shift($frontendLabel);
     }
     if (!isset($this->_attributeLabels[$attributeId])) {
         $this->_attributeLabels[$attributeId] = $attribute->getStoreLabels();
     }
     if (isset($this->_attributeLabels[$attributeId][$storeId])) {
         return $this->_attributeLabels[$attributeId][$storeId];
     } elseif (!empty($frontendLabel)) {
         return $frontendLabel;
     } else {
         return $attribute->getAttributeCode();
     }
 }
Exemplo n.º 13
0
 /**
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute
  * @param array $data
  * @return array
  */
 protected function getOption($attribute, $data)
 {
     $result = [];
     $data['option'] = explode("\n", $data['option']);
     /** @var \Magento\Eav\Model\Resource\Entity\Attribute\Option\Collection $options */
     $options = $this->attrOptionCollectionFactory->create()->setAttributeFilter($attribute->getId())->setPositionOrder('asc', true)->load();
     foreach ($data['option'] as $value) {
         if (!$options->getItemByColumnValue('value', $value)) {
             $result[] = $value;
         }
     }
     return $result ? $this->convertOption($result) : $result;
 }
Exemplo n.º 14
0
 public function testCRUD()
 {
     $this->_model->setAttributeCode('test')->setEntityTypeId(\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Eav\\Model\\Config')->getEntityType('catalog_product')->getId())->setFrontendLabel('test');
     $crud = new \Magento\TestFramework\Entity($this->_model, array('frontend_label' => uniqid()));
     $crud->testCrud();
 }
Exemplo n.º 15
0
 /**
  * Check whether the attribute is Applicable to the object
  *
  * @param \Magento\Framework\Object $object
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute
  * @return boolean
  */
 protected function _isApplicableAttribute($object, $attribute)
 {
     $applyTo = $attribute->getApplyTo();
     return (count($applyTo) == 0 || in_array($object->getTypeId(), $applyTo)) && $attribute->isInSet($object->getAttributeSetId());
 }
Exemplo n.º 16
0
 /**
  * Retrieve Product Attribute Value
  *
  * @param Product $product
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute
  * @return string
  */
 public function getProductAttributeValue($product, $attribute)
 {
     if (!$product->hasData($attribute->getAttributeCode())) {
         return __('N/A');
     }
     if ($attribute->getSourceModel() || in_array($attribute->getFrontendInput(), array('select', 'boolean', 'multiselect'))) {
         //$value = $attribute->getSource()->getOptionText($product->getData($attribute->getAttributeCode()));
         $value = $attribute->getFrontend()->getValue($product);
     } else {
         $value = $product->getData($attribute->getAttributeCode());
     }
     return (string) $value == '' ? __('No') : $value;
 }
Exemplo n.º 17
0
 /**
  * Create filter
  *
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute
  * @param \Magento\Catalog\Model\Layer $layer
  * @return mixed
  */
 protected function createAttributeFilter(\Magento\Catalog\Model\Resource\Eav\Attribute $attribute, \Magento\Catalog\Model\Layer $layer)
 {
     $filterClassName = $this->filterTypes[self::ATTRIBUTE_FILTER];
     if ($attribute->getAttributeCode() == 'price') {
         $filterClassName = $this->filterTypes[self::PRICE_FILTER];
     } elseif ($attribute->getBackendType() == 'decimal') {
         $filterClassName = $this->filterTypes[self::DECIMAL_FILTER];
     }
     $filter = $this->objectManager->create($filterClassName, array('data' => array('attribute_model' => $attribute), 'layer' => $layer));
     return $filter;
 }
Exemplo n.º 18
0
 /**
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute
  * @param \Magento\Catalog\Model\Resource\Product\Collection $collection
  * @return $this
  */
 protected function addGlobalAttribute(\Magento\Catalog\Model\Resource\Eav\Attribute $attribute, \Magento\Catalog\Model\Resource\Product\Collection $collection)
 {
     $storeId = $this->storeManager->getStore()->getId();
     switch ($attribute->getBackendType()) {
         case 'decimal':
         case 'datetime':
         case 'int':
             $alias = 'at_' . $attribute->getAttributeCode();
             $collection->addAttributeToSelect($attribute->getAttributeCode(), 'inner');
             break;
         default:
             $alias = 'at_' . md5($this->getId()) . $attribute->getAttributeCode();
             $collection->getSelect()->join([$alias => $collection->getTable('catalog_product_index_eav')], "({$alias}.entity_id = e.entity_id) AND ({$alias}.store_id = {$storeId})" . " AND ({$alias}.attribute_id = {$attribute->getId()})", []);
     }
     $this->joinedAttributes[$attribute->getAttributeCode()] = $alias;
     return $this;
 }
Exemplo n.º 19
0
 public function testGetScopeStore()
 {
     $objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);
     $this->_model = $objectManagerHelper->getObject('\\Magento\\Catalog\\Model\\Resource\\Eav\\Attribute', ['context' => $this->contextMock, 'productFlatIndexerProcessor' => $this->_processor, 'indexerEavProcessor' => $this->_eavProcessor, 'resource' => $this->resourceMock, 'data' => ['is_global' => 'some value']]);
     $this->assertEquals('store', $this->_model->getScope());
 }
Exemplo n.º 20
0
 /**
  * Add filter by indexable attribute
  *
  * @param \Magento\CatalogSearch\Model\Resource\Advanced\Collection $collection
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute
  * @param string|array $value
  * @return bool
  */
 public function addIndexableAttributeModifiedFilter($collection, $attribute, $value)
 {
     if ($attribute->getIndexType() == 'decimal') {
         $table = $this->getTable('catalog_product_index_eav_decimal');
     } else {
         $table = $this->getTable('catalog_product_index_eav');
     }
     $tableAlias = 'a_' . $attribute->getAttributeId();
     $storeId = $this->_storeManager->getStore()->getId();
     $select = $collection->getSelect();
     if (is_array($value)) {
         if (isset($value['from']) && isset($value['to'])) {
             if (empty($value['from']) && empty($value['to'])) {
                 return false;
             }
         }
     }
     $select->distinct(true);
     $select->join(array($tableAlias => $table), "e.entity_id={$tableAlias}.entity_id " . " AND {$tableAlias}.attribute_id={$attribute->getAttributeId()}" . " AND {$tableAlias}.store_id={$storeId}", array());
     if (is_array($value) && (isset($value['from']) || isset($value['to']))) {
         if (isset($value['from']) && !empty($value['from'])) {
             $select->where("{$tableAlias}.value >= ?", $value['from']);
         }
         if (isset($value['to']) && !empty($value['to'])) {
             $select->where("{$tableAlias}.value <= ?", $value['to']);
         }
         return true;
     }
     $select->where("{$tableAlias}.value IN(?)", $value);
     return true;
 }
Exemplo n.º 21
0
 /**
  * Check whether specified attribute can be used in LN
  *
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute  $attribute
  * @return int
  */
 protected function getAttributeIsFilterable($attribute)
 {
     return $attribute->getIsFilterableInSearch();
 }
Exemplo n.º 22
0
 /**
  * Check attribute availability for super product creation
  *
  * @param  \Magento\Catalog\Model\Resource\Eav\Attribute $attribute
  * @return bool
  */
 public function canUseAttribute(\Magento\Catalog\Model\Resource\Eav\Attribute $attribute)
 {
     return $attribute->getIsGlobal() == \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_GLOBAL && $attribute->getIsVisible() && $attribute->usesSource() && $attribute->getIsUserDefined();
 }
Exemplo n.º 23
0
 /**
  * Check whether specified attribute can be used in LN
  *
  * @param \Magento\Catalog\Model\Resource\Eav\Attribute $attribute
  * @return bool
  */
 protected function _getIsFilterableAttribute($attribute)
 {
     return $attribute->getIsFilterable();
 }