2015-12-23 17 views
5

Próbuję pobrać listę atrybutów rozwijanych i sprawdzić, czy wartość istnieje (jeśli jest potrzebna, aby uzyskać wartość i przypisać ją do produktu) i czy to nie będzie musiał go stworzyć i uzyskać jego wartość, aby przypisać go do produktu.Jak uzyskać opcje atrybutów produktu według kodu atrybutu w 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

mógłbyś pisać jakiś kod? –

+0

tak, oczywiście., Sprawdź teraz – Xubaer

+0

Znalazłeś sposób? :) –

Odpowiedz

14

Po Magento 2 wytycznych, ty nie powinna korzystać ObjectManager samemu. Zamiast tego należy użyć wtrysku zależności. More info here

W swoim bloku/kontrolerze/pomocniku ... utwórz konstruktora i wstrzyknij klasę \Magento\Catalog\Model\Product\Attribute\Repository. Na przykład:

/** 
* @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; 
} 

następnie w swojej specjalnej metody chcesz zadzwonić (PHPDoc dodana dla jasności):

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

Można teraz uzyskać Opcje Wartości i etykiety w ten sposób:

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

Poniższy kod pomaga znaleźć konkretne wartości atrybutów. Tak jak tutaj, kolor jest na moim atrybucie, a poniższy kod pozwala uzyskać to, co i wszystkie kolory są odwzorowane za pomocą tego atrybutu.

$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

to jest magento 1.x, potrzebuję magento 2.0 – Xubaer

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

$ _product jest przedmiotem Produkt Powyższy kod zwraca wartość atrybutu nazwy atrybutu "move".

0

wstrzyknąć wystąpienie \Magento\Catalog\Model\Product\Attribute\Repository w konstruktorze (w bloku klasy pomocnika lub gdziekolwiek):

/** 
* @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; 
    ... 
} 

Następnie utworzyć metodę w swojej klasie, aby uzyskać atrybut kodem:

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

Możesz wtedy wywołać tę metodę, np wewnątrz pliku .phtml

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

Następnie można wykonywać połączenia w obiekcie atrybutu, np.

  1. opcji otrzymujemy: $attrTest->getOptions()
  2. Wirtualny nakładka etykiety dla każdego Zapisu: $attrTest->getFrontendLabels()
  3. debugowania tablicy danych: echo '> ' . print_r($attrTest->debug(), true);

debugowania: Array ( [attribute_id] => 274 [entity_type_id] => 4 [attribute_code] => product_manual_download_label [backen d_type] => varchar [frontend_input] => tekst [frontend_label] => Product Instrukcja pobierania Label [is_required] => 0 [is_user_defined] => 1 [DEFAULT_VALUE] => Product Instrukcja pobierania [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 [położenia] => 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

Każdy pomysł Jak załadować wszystkie atrybuty za pomocą ProductAttributeRepository? Chcę kolekcji atrybutów, ale ze wszystkimi opcjami, które pokazano. –

Powiązane problemy