【问题标题】:How to get Product Attribute Options by attribute code in Magento 2.0如何通过 Magento 2.0 中的属性代码获取产品属性选项
【发布时间】:2016-03-29 23:35:51
【问题描述】:

我正在尝试检索下拉属性列表并检查该值是否存在(如果存在,我需要获取该值并将其分配给产品),如果不存在,我将不得不创建它并获取其值将其分配给产品。

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

【问题讨论】:

  • 你能发布一些代码吗?
  • 是的,请立即查看

标签: php magento-2.0


【解决方案1】:

遵循 Magento 2 指南,您不应自己使用 ObjectManager。相反,您必须使用依赖注入。 More info here

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

/**
 * @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface $_productAttributeRepository
 */
protected $_productAttributeRepository;

/**
 * @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $productAttributeRepository
 */
public function __construct(\Magento\Catalog\Api\ProductAttributeRepositoryInterface $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
}

【讨论】:

    【解决方案2】:
    <?php echo $_product->getResource()->getAttribute('movement')->getFrontend()->getValue($_product);?>
    

    $_product 是 Product 的对象 以上代码返回属性名“movement”的属性值。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 这很有帮助,如果你想通过标签获取属性代码,如果你使用上面的方法获取属性,那么你可以这样做获取单个属性代码:$attributeCodeId = $attribute->getSource()->getOptionId('Attribute Label');
      【解决方案4】:

      在您的构造函数中注入\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-&gt;getOptions()
      2. 获取每个商店的前端标签:$attrTest-&gt;getFrontendLabels()
      3. 调试数据数组:echo '&gt; ' . print_r($attrTest-&gt;debug(), true);

      调试:数组( [attribute_id] => 274 [entity_type_id] => 4 [attribute_code] => product_manual_download_label [后端类型] => varchar [前端输入] => 文本 [frontend_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 [搜索权重] => 1)

      【讨论】:

      • 任何想法如何使用 productAttributeRepository 加载所有属性?我想要属性集合,但是你展示了所有这些选项。
      【解决方案5】:
      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);
      

      【讨论】:

        【解决方案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());
        

        【讨论】:

        • 这是 magento 1.x,我需要 magento 2.0
        猜你喜欢
        • 2023-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多