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
2 Answers
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 productsmagento noob– magento noob2019-01-02 13:24:03 +00:00Commented Jan 2, 2019 at 13:24
- you can use also grid render webkul.com/blog/…Rakesh Donga– Rakesh Donga2019-01-02 13:27:35 +00:00Commented Jan 2, 2019 at 13:27
- yes renderer is an option but then it will be an issue to manage the custom options in multiple rowsmagento noob– magento noob2019-01-02 13:45:43 +00:00Commented Jan 2, 2019 at 13:45
- if you are used render then easily to manage select optionRakesh Donga– Rakesh Donga2019-01-03 09:15:06 +00:00Commented Jan 3, 2019 at 9:15
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 } }