继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
}
你可以张贴一些代码? –
当然可以,请立即检查 – Xubaer
找到了方法吗? :) –