I have to modify the catalog search to only show the configurable parent product of a simple product in the search result.
It should work like this:
- Simple Product SKU: 123.123
- Configurable Product SKU: 123.100-X
- Search "123.123"
Result: Product 123.100-X is displayed.
I'm pretty sure I have to override the function: prepareProductCollection
in app/code/core/Mage/CatalogSearch/Model/Layer.php
But I'm stuck with this.
As a second step, I want to preselect the configurable options of the showed configurable product based on the searched simple product, if only one exact result is returned.
For this I would redirect to the product view page like described in this thread: Catalog Search: If only one result, show product view page insted of list view
And use this function to get the attribute value to select:
private function getProductUrlWithAttributes($product, $storeId) {
$url = $product->setStoreId($storeId)->getProductUrl();
$urlHash = array();
$parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId());
if(isset($parentIds[0])) {
$parent = Mage::getModel('catalog/product')->load($parentIds[0]);
$url = $parent->setStoreId($storeId)->getProductUrl();
$attrs = $parent->getTypeInstance(true)->getConfigurableAttributes($parent);
foreach ($attrs as $attribute) {
$productAttribute = $attribute->getProductAttribute();
$productAttributeId = $productAttribute->getId();
if($attributeValue = $product->getData($productAttribute->getAttributeCode())) {
$urlHash[$productAttributeId] = $attributeValue;
}
}
}
if(count($urlHash) > 0) {
$url .= '#'.http_build_query($urlHash, null, ',');
}
return substr($url, 0, 2000);
}
(Source: http://www.snip2code.com/Snippet/171609/gets-a-Magento-parent-product-url-with-h)
I would really appreciate any help, I don't know how to continue with this.