Magento – Catalog search: Always show configurable instead of simple product in search result

catalogsearchconfigurable-productmagento-1.9simple-product

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.

Best Answer

Can I suggest not touching the code to get this functionality? Perhaps you could do the following:

First up:
1. Change all simple product visibility to "Catalog" or "Not visible individually"
2. Make sure all the configurable products' visibility is set to "Catalog/Search" or "Search"

Option 1
Create a product attribute called "child_skus", make it used for quick search and add a comma separated list of the child SKUs.

Option 2
Add the child skus to the configurable product's meta keywords. Make sure the meta_keywords attribute is included in the quick search