i need to add custom options added to simple product on product list page.
$customOptions = $objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($_product);
print_r($customOptions->getData());
the output i am getting for the product which has custom options is
Array
(
[0] => Array
(
[option_id] => 3
[product_id] => 41
[type] => radio
[is_require] => 1
[sku] =>
[max_characters] => 0
[file_extension] =>
[image_size_x] => 0
[image_size_y] => 0
[sort_order] => 1
[default_title] => size
[store_title] =>
[title] => size
[default_price] =>
[default_price_type] =>
[store_price] =>
[store_price_type] =>
[price] =>
[price_type] =>
)
[1] => Array
(
[option_id] => 4
[product_id] => 41
[type] => field
[is_require] => 0
[sku] => size-custom
[max_characters] => 0
[file_extension] =>
[image_size_x] => 0
[image_size_y] => 0
[sort_order] => 2
[default_title] => custom size
[store_title] =>
[title] => custom size
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 0.0000
[price_type] => fixed
)
)
As you can see i am getting custom options but not the values. can somebody please help. this is on the product list page
Solved! Go to Solution.
With some debugging and effort the thing was discovered, that the following code snippet is the solution to the problem.
$customOptions = $objectManager->get('Magento\Catalog\Model\Product\Option')
->getProductOptionCollection($_product);
if (empty($customOptions)) { //check if product has custom options. If it doesn't go to the next product
continue;
}
foreach($customOptions as $option) {
$values = $option->getValues();
if (empty($values)) { //check if option has values (Option can have values only if option type is checkbox, radio, multiselect or drop-down)
continue;
}
foreach($values as $value) {
$valueData = $value->getData(); //do whatever you want will value data
}
}
In order to get, for instance, option value title use this:
foreach($customOptions as $option) {
$values = $option->getValues();
if (empty($values)) { //check if option has values (Option can have values only if option type is checkbox, radio, multiselect or drop-down)
continue;
}
foreach($values as $value) {
$valueTitle = $value->getTitle();
}
}
Cheers
Hi wester9208, how are you?
Actually you can't get option and values in that format, as you would like to (nested arrays). When you use such method as
getProductOptionCollection
this method calls
$collection->addValuesToResult($product->getStoreId());
The method
addValuesToResult
loops through each option, loads it like an object (\Magento\Catalog\Model\Product\Option) and loads all values of current option like objects (\Magento\Catalog\Model\Product\Option\Value).
So in order to get option values, use the next code
//$this->optionFactory \Magento\Catalog\Model\Product\OptionFactory $customOptions = $this->optionFactory->create() ->getProductOptionCollection($product); foreach ($customOptions as $customOption) { $values = $customOption->getValues(); foreach ($values as $value) { $valueData = $value->getData(); } }
Actually I can't understand, what is product list page? Can you give me full action name or url part with route, controller and action?
The true thing is, that the product can have already preloaded options with it's values, so you don't need to call
getProductOptionCollection
method, cause it loads data from database again. It's not necessary.
Just try
$options = $_product->getOptions() ?: []; foreach ($options as $option) { $values = $option->getValues(); if (empty($values)) { throw new \Exception(__('Option has no values')); } foreach ($values as $value) { $valueData = $value->getData(); } }
You can get the list of options from the product by, Get Custom Option from product
$customOptions = $objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($_product); print_r($customOptions->getData()); foreach($customOptions as $optionKey => $optionVal): foreach($optionVal->getValues() as $valuesKey => $valuesVal) { echo $valuesVal->getId().' '.$valuesVal->getTitle(); } endforeach;
@Rakesh Jesadiya I tried your code, i am getting1 exception(s): Exception #0 (Exception): Warning: Invalid argument supplied for foreach() in /var/www/0/185847/www/app/design/frontend/Smartwave/porto/Magento_Catalog/templates/product/list.phtml on line 170 Exception #0 (Exception): Warning: Invalid argument supplied for foreach() in /var/www/0/185847/www/app/design/frontend/Smartwave/porto/Magento_Catalog/templates/product/list.phtml on line 170
@dmoisej product listing as in I am referring to products list on the category page. I want to display my custom options there for the single product which are entered in the backend. Can you please help me figure this out?
@wester9208 sure. Where do you retrieve your products? In some observer?
Oh I am sorry. I can see now from the error message, that you are trying to retrieve product custom options in the template. Let me debug this one on my local environment.
@dmoisej thanks, yes i was trying to retrieve the data on my product list template. I am getting the custom option data but not able to get the options value .
@wester9208 so you couldn't see option values and after that you have tried the suggestion from @Rakesh Jesadiya and it gave you the exception. If it is this scenario I think I know what the problem could be. One second, let me check one thing.
@wester9208 this one should work for you
$customOptions = $objectManager->get('Magento\Catalog\Model\Product\Option')
->getProductOptionCollection($_product);
if (empty($customOptions)) { //check if product has custom options. If it doesn't go to the next product
continue;
}
foreach($customOptions as $option) {
$values = $option->getValues();
if (empty($values)) { //check if option has values (Option can have values only if option type is checkbox, radio, multiselect or drop-down)
continue;
}
foreach($values as $value) {
$valueData = $value->getData(); //do whatever you want will value data
}
}
Thanks for your quick reply, I tried this
$customOptions = $objectManager->get('Magento\Catalog\Model\Product\Option') ->getProductOptionCollection($_product); if (empty($customOptions)) { //check if product has custom options. If it doesn't go to the next product echo "empty 1 "; continue; } foreach($customOptions as $option) { $values = $option->getValues(); if (empty($values)) { //check if option has values (Option can have values only if option type is checkbox, radio, multiselect or drop-down) echo "empty 2 "; continue; } foreach($values as $value) { $valueData = $value->getData(); //do whatever you want will value data } } ?>
I was getting "empty 2".
Am i doing something wrong?