Injizieren Sie eine Instanz von \Magento\Catalog\Model\Product\Attribute\Repository
in Ihrem Konstruktor (in einem Block, Hilfsklasse oder wo auch immer):
/**
* @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;
...
}
Dann eine Methode in Ihrer Klasse erstellen Sie das Attribut von Code zu erhalten:
/**
* Get single product attribute data
*
* @return Magento\Catalog\Api\Data\ProductAttributeInterface
*/
public function getProductAttributeByCode($code)
{
$attribute = $this->_productAttributeRepository->get($code);
return $attribute;
}
Sie können diese Methode dann wie folgt aufrufen, z innerhalb einer .phtml-Datei
$attrTest = $block->getProductAttributeByCode('test');
Dann können Sie auf das Attributobjekt, z.
- Get Optionen:
$attrTest->getOptions()
- Get Frontend-Label für jeden Speicher:
$attrTest->getFrontendLabels()
- Debug das Datenfeld:
echo '> ' . print_r($attrTest->debug(), true);
Debug: Array ( [attribute_id] => 274 [entity_type_id] => 4 [attribute_code] => product_manual_download_label [zurück d_type] => varchar [frontend_input] => Text [frontend_label] => Produkthandbuch-Download Label [is_required] => 0 [is_user_defined] => 1 [default_value] => Produkthandbuch herunterladen [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 [Position] => 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)
Könnten Sie einige Code schreiben? –
yeah sure., Bitte überprüfen Sie jetzt – Xubaer
Haben Sie einen Weg gefunden? :) –