如何通过Magento 2.0中的属性代码获取产品属性选项

时间:2015-12-23 13:58:17

标签: php 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());

6 个答案:

答案 0 :(得分:16)

遵循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
}

答案 1 :(得分:2)

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

$ _ product是Product的对象 上面的代码返回属性名称“运动”的属性值。

答案 2 :(得分:0)

在构造函数中注入*ngIf="mySubject | async"的实例(在块,帮助器类或任何地方):

\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;
    ...
}

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

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

然后你可以对属性对象进行调用,例如

  1. 获取选项:$attrTest = $block->getProductAttributeByCode('test');
  2. 获取每个商店的前端标签:$attrTest->getOptions()
  3. 调试数据阵列:$attrTest->getFrontendLabels()
  4.   

    debug:Array(       [attribute_id] =&gt; 274       [entity_type_id] =&gt; 4       [attribute_code] =&gt; product_manual_download_label       [backend_type] =&gt; VARCHAR       [frontend_input] =&gt;文本       [frontend_label] =&gt;产品手册下载标签       [is_required] =&gt; 0       [is_user_defined] =&gt; 1       [default_value] =&gt;产品手册下载       [is_unique] =&gt; 0       [is_global] =&gt; 0       [is_visible] =&gt; 1       [is_searchable] =&gt; 0       [is_filterable] =&gt; 0       [is_comparable] =&gt; 0       [is_visible_on_front] =&gt; 0       [is_html_allowed_on_front] =&gt; 1       [is_used_for_price_rules] =&gt; 0       [is_filterable_in_search] =&gt; 0       [used_in_product_listing] =&gt; 0       [used_for_sort_by] =&gt; 0       [is_visible_in_advanced_search] =&gt; 0       [position] =&gt; 0       [is_wysiwyg_enabled] =&gt; 0       [is_used_for_promo_rules] =&gt; 0       [is_required_in_admin_store] =&gt; 0       [is_used_in_grid] =&gt; 1       [is_visible_in_grid] =&gt; 1       [is_filterable_in_grid] =&gt; 1       [search_weight] =&gt; 1)

答案 3 :(得分:0)

对于任何实体类型的EAV属性,使用API​​服务层,如下所示将服务数据成员注入构造函数中。

protected $eavAttributeRepository;
public function __construct(
    ...
    \Magento\Eav\Api\AttributeRepositoryInterface $eavAttributeRepositoryInterface,
    ...
){
    ...
    $this->eavAttributeRepository = $eavAttributeRepositoryInterface;
    ...
}

然后您可以使用此属性。

$attribute = $this->eavAttributeRepository->get('catalog_product', 'attribute_code_here');
// vardump($attribute->getData());

为了获取属性选项值数组,请使用它。

$options = $attribute->getSource()->getAllOptions();

答案 4 :(得分:0)

Try the following code

$attribute_code = "coffe_type";
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();

$eavConfig = $objectManager->get('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product',$attribute_code );

$options = $attribute->getSource()->getAllOptions();
foreach($options as $option) {
    $optionsExists[] = array('label' => $option['label'], 'value'=> $option['value'] );
}

print_r($optionsExists);

答案 5 :(得分:-7)

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

$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());