2016-07-20 32 views
5

Bir ürün için iki özel seçeneğim var. Renk ve Boyut ve her ikisi de açılır. Ürün detay sayfalarında, o ürünün tüm mevcut renklerini göstermem gerekiyor.Ürünün Özel Seçenek Değerlerini Alın Magento 2

Aşağıdaki kodu denedim ve çalışıyor. Ancak Renk ve Boyut'un tüm değerlerini döndürür. Ama sadece renk değerlerine ihtiyacım var. Bu, özel seçenekleri renk olarak seçmek istiyorum.

$_product = $block->getProduct(); 

foreach($_product->getOptions() as $o){ 
    foreach($o->getValues() as $value){ 
    print_r($value->getData()); 
    } 
} 

cevap

1

Hala ihtiyacınız olup olmadığını bilmiyorum, ama bir çözüm buldum.

foreach($product->getProductOptionsCollection() as $o){ 
    foreach($o->getValues() as $ov){ 
     // do whatever you want to it; 
     var_dump($ov->getData()); 
    } 
} 

dökümü (bu ithal bir ürün olması) tüm boş değerlere olmadan, böyle bir şey dönecektir

array(13) { 
    ["option_type_id"]=> 
    string(5) "23122" 
    ["option_id"]=> 
    string(4) "6045" 
    ["sku"]=> 
    string(1) "2" 
    ["sort_order"]=> 
    string(1) "2" 
    ["default_title"]=> 
    string(33) "Test Option" 
    ["store_title"]=> 
    NULL 
    ["title"]=> 
    string(33) "Test Option" 
    ["default_price"]=> 
    NULL 
    ["default_price_type"]=> 
    NULL 
    ["store_price"]=> 
    NULL 
    ["store_price_type"]=> 
    NULL 
    ["price"]=> 
    NULL 
    ["price_type"]=> 
    NULL 
} 
İlgili konular