Get custom magento 2 product parameter values

I have two custom options for the product. Color and size, and both are drop-down menus. On product detail pages, I must display all available colors for this product.

I tried the following code and it works. But it returns all color and size values. But I need only color values. That is, I want to select custom color options.

$_product = $block->getProduct(); foreach($_product->getOptions() as $o){ foreach($o->getValues() as $value){ print_r($value->getData()); } } 
+5
source share
1 answer

I do not know if you need this problem or not, but I found a solution.

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

The dump will return something like this without all NULL (this is an imported product)

 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 } 
+1
source

All Articles