2

Need help making a query which returns the Product ID of all products which have custom options

1
  • You want a SQL query direct from the database or through Magento? Jan 14, 2015 at 21:43

2 Answers 2

3

Magento:

$options = Mage::getModel('catalog/product_option')->getCollection();
$options->addFieldToSelect('product_id');
$options->getSelect()->distinct(true);

foreach ($options as $option) {
    Zend_Debug::dump($option->getProductId());
}

Query:

SELECT DISTINCT `main_table`.`product_id` FROM `catalog_product_option` AS `main_table`
2
  • 1
    you can add a Distinct also in the select to remove duplicates.
    – Marius
    Jan 14, 2015 at 21:37
  • This worked great. Curious if you could advise how to make the query more elaborate so it returns all product id AND product name AND what custom options are available for that product. Thank you!! Jan 26, 2015 at 15:27
0

Hi please use the below query

    echo  $Sql= Mage::getModel('catalog/product_option')->getCollection()
-> addFieldToSelect('product_id')->getSelect()
            ->distinct(true);

echo $Sql->__toString();

use getSelect() function for getting the sql query and distinct to get distinct data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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