2015-12-23 6 views
5

में विशेषता कोड द्वारा उत्पाद विशेषता विकल्पों को कैसे प्राप्त करें मैं ड्रॉपडाउन विशेषताओं की सूची पुनर्प्राप्त करने का प्रयास कर रहा हूं और यह जांचता हूं कि मूल्य मौजूद है (यदि मुझे मूल्य प्राप्त करने और उत्पाद को असाइन करने की आवश्यकता है) और यदि ऐसा नहीं है कि मुझे इसे बनाना होगा और इसे उत्पाद को सौंपने के लिए अपना मूल्य प्राप्त करना होगा।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

आप कुछ कोड पोस्ट कर सकते हैं? –

+0

हाँ यकीन है।, कृपया जांचें – Xubaer

+0

कोई रास्ता नहीं मिला? :) –

उत्तर

14

Magento 2 दिशानिर्देशों का पालन करने के लिए, आप ObjectManager खुद के द्वारा प्रयोग नहीं करना चाहिए। इसके बजाय, आपको निर्भरता इंजेक्शन का उपयोग करना होगा। More info here

अपने ब्लॉक/नियंत्रक/सहायक में ..., एक कन्स्ट्रक्टर बनाएं और \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 
} 
-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()); 
+3

कि Magento 1.x है, मुझे Magento 2.0 – Xubaer

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

$ _product उत्पादकी वस्तु है 210 उपरोक्त कोड विशेषता नाम "आंदोलन" के गुण मान देता है।

0

अपने निर्माता में \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->getOptions()
  2. प्राप्त दृश्यपटल लेबल: $attrTest->getFrontendLabels()
  3. डीबग डेटा सरणी: echo '> ' . print_r($attrTest->debug(), true);

डिबग: सरणी ( [attribute_id] => 274 [entity_type_id] => 4 [attribute_code] => product_manual_download_label [बैकन d_type] => varchar [frontend_input] => पाठ [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,210 [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 [search_weight] => 1)

+0

कोई विचार है उत्पाद AttributeRepository का उपयोग कर सभी विशेषताओं को कैसे लोड करें? मैं विशेषताओं का संग्रह चाहता हूं लेकिन आपके द्वारा दिखाए गए सभी विकल्पों के साथ। –