In addition to my previous post, sometimes you also have clients who want to show product bundles on the product detail page that contain the product in question. Once again, Magento doesn’t provide this functionality out-of-the-box, and the only way to do it without direct SQL queries is by loading all bundled products and iterating over them. In this scenario, a custom SQL query is totally justified. The query to fetch all bundled products ID containing a specific child product is as follows:
1 2 |
SELECT parent_product_id FROM catalog_product_bundle_selection WHERE product_id = 8 |
When put in a function you could get something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/** * Get bundled products containing a specific other product * * @param $productId * @param $limit * * @return Mage_Catalog_Model_Resource_Product_Collection */ public function getBundledProductsContainingProductId($productId, $limit = 3) { $connection = Mage::getSingleton('core/resource')->getConnection('core_read'); $productIds = $connection->fetchCol( sprintf(' SELECT parent_product_id FROM catalog_product_bundle_selection WHERE product_id = %1$d ORDER BY RAND() LIMIT %2$d; ', $productId, $limit) ); $collection = Mage::getModel('catalog/product')->getCollection(); $collection->setPageSize($limit); $collection->addIdFilter($productIds); $collection->addAttributeToSelect('*'); return $collection; } |
Happy satisfying your client!
Visitors give this article an average rating of 3.0 out of 5.
How would you rate this article?
★ ★ ★ ★ ★