2015-12-23 221 views
5

我试图检索下拉属性列表并检查值是否存在(如果它确实需要获取值并将其分配给产品)以及if它不是我必须创建它并获得它的价值来分配给产品。如何通过Magento 2.0中的属性代码获取产品属性选项

$attribute = $this->objectManager->create('Magento\Eav\Model\Entity\Attribute'); 
$attributeId = $attribute->getIdByCode('catalog_product','manufacturer'); 
$model = $this->objectManager->create('Magento\Catalog\Model\ResourceModel\Eav\Attribute'); 
$model->load($attributeId); 
print_r($model->getFrontendLabel()); 
+0

你可以张贴一些代码? –

+0

当然可以,请立即检查 – Xubaer

+0

找到了方法吗? :) –

回答

14

继Magento的2条准则,你不应该由自己使用的ObjectManager。相反,您必须使用依赖注入。 More info here

在您的Block/Controller/Helper ...中,创建一个构造函数并注入\Magento\Catalog\Model\Product\Attribute\Repository类。例如:

/** 
* @var \Magento\Catalog\Model\Product\Attribute\Repository $_productAttributeRepository 
*/ 
protected $_productAttributeRepository; 

/** 
* @param \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository 
*/ 
public function __construct(\Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository) 
{ 
    $this->_productAttributeRepository = $productAttributeRepository; 
} 

然后,在专用你的方法,你要调用(添加PHPDoc的为清楚起见)

/** @var \Magento\Eav\Api\Data\AttributeOptionInterface[] $manufacturerOptions */ 
$manufacturerOptions = $this->_productAttributeRepository->get('manufacturer')->getOptions(); 

你现在可选项的值和标签是这样的:

foreach ($manufacturerOptions as $manufacturerOption) { 
    $manufacturerOption->getValue(); // Value 
    $manufacturerOption->getLabel(); // Label 
} 
-6

下面的代码有助于查找特定的属性值。就像这里,颜色是我的属性,通过下面的代码,我们可以得到什么和所有的颜色与这个属性映射。

$attributeId = Mage::getResourceModel(‘eav/entity_attribute’)>getIdByCode(‘catalog_product’,’color’); 
$collection =Mage::getResourceModel(‘eav/entity_attribute_option_collection’)>setPositionOrder(‘asc’)->setAttributeFilter($attributeId)->setStoreFilter(0)->load(); 
print_r($collection->getData()); 
+3

这就是magento 1.x,我需要magento 2.0 – Xubaer

0
<?php echo $_product->getResource()->getAttribute('movement')->getFrontend()->getValue($_product);?> 

$ _产品是产品的对象210以上代码返回属性名称“movement”的属性值。

0

在构造函数注入的\Magento\Catalog\Model\Product\Attribute\Repository一个实例(块,辅助类或其它地方):

/** 
* @var \Magento\Catalog\Model\Product\Attribute\Repository $_productAttributeRepository 
*/ 
protected $_productAttributeRepository; 

/** 
* ... 
* @param \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository 
* ... 
*/ 
public function __construct(
    ... 
    \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository, 
    ... 
) { 
    ... 
    $this->_productAttributeRepository = $productAttributeRepository; 
    ... 
} 

然后在您的类中创建一个方法,通过代码来获取属性:

/** 
* Get single product attribute data 
* 
* @return Magento\Catalog\Api\Data\ProductAttributeInterface 
*/ 
public function getProductAttributeByCode($code) 
{ 
    $attribute = $this->_productAttributeRepository->get($code); 
    return $attribute; 
} 

然后你可以像这样调用这个方法,例如在.phtml文件中

$attrTest = $block->getProductAttributeByCode('test'); 

然后,您可以在属性对象上进行调用,例如,

  1. 获取选项:$attrTest->getOptions()
  2. 获取前端标签为每个商店:$attrTest->getFrontendLabels()
  3. 调试数据阵列:echo '> ' . print_r($attrTest->debug(), true);

调试:阵列( [attribute_id] => 274 [entity_type_id] => 4 [attribute_code] => product_manual_download_label [backen d_type] => VARCHAR [frontend_input] =>文本 [frontend_label] =>产品手册下载Label [is_required] => 0 [is_user_defined] => 1 [DEFAULT_VALUE] =>产品手册下载 [is_unique] = > 0 [is_global] => 0 [is_visible] => 1 [is_searchable] => 0 [is_filterable] => 0 [is_comparable] => 0 [is_visible_on_front] => 0 [is_html_allowed_on_front] => 1 [is_used_for_price_rules] => 0 [is_filterable_in_search] => 0 [used_in_product_listing] => 0 [used_for_sort_by] => 0 [is_visible_in_advanced_search] => 0 [位置] => 0 [is_wysiwyg_enabled] => 0 [is_used_for_promo_rules] => 0 [is_required_in_admin_store] => 0 [is_used_in_grid] => 1 [is_visible_in_grid] => 1 [is_filterable_in_grid] => 1 [search_weight] => 1)

+0

任何想法如何使用productAttributeRepository加载所有属性? 我想收集的属性,但与你显示的所有这些选项。 –

相关问题