0

I am trying to show all the products with their custom options in a magento grid in admin panel. I can not figure out how to do this. I have to show products in grid and their custom options , if a product has 2 custom options then another row will show up for both custom options. All custom options are of drop down types. Any help will be appreciated. Thanks Alot

0

Here Below code using custom options get and select option to easily set

$customOptions = $objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($_product);

    foreach($customOptions as $optionKey => $optionVal):
                   foreach($optionVal->getValues() as $valuesKey => $valuesVal) {
                          echo $valuesVal->getId().' '.$valuesVal->getTitle();
                    }
    endforeach;
  • thanks for your answer, but i have to show the collection in the UI grid along with all the products – magento noob Jan 2 at 13:24
  • you can use also grid render webkul.com/blog/… – Rakesh Donga Jan 2 at 13:27
  • yes renderer is an option but then it will be an issue to manage the custom options in multiple rows – magento noob Jan 2 at 13:45
  • if you are used render then easily to manage select option – Rakesh Donga Jan 3 at 9:15
0

Declare \Magento\Catalog\Model\Product\Option in construct of your class.

Try following snippet:

protected $productOption;

public function __construct(
    \Magento\Catalog\Model\Product\Option $productOption
){
    $this->productOption = $productOption;
}

Then you can do following:

/**
 * [$_product Product Object]
 */
$customOptions = $this->productOption->getProductOptionCollection($_product);
if (!empty($customOptions->getData())) {
    foreach ($customOptions->getData() as $option) {
       print_r($option); // You will get option data here

    }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.