I investigated and dig into the code. Below is the brief of my findings
When we are creating the search indexes by performing catalog search re-indexation
, Magento update the table catalogsearch_fulltext
with the product id
and data_index
.
data_index
contains the values of all the searchable attributes of the product itself and the associated products like brand, name, etc but it contains the sku only of the product and not of the associated product.
So, when a user is searching with a particular text, then magento tries to match this text with the values present in the data_index. If it gets any matching text then it shows that particular product in the result set.
Example values of data_index : AB2180|AB2180 Polished/Brushed|AB2180 Polished Chrome|AB2180 Brushed Nickel|||||1
Brief of above values :
AB2180 : product sku
AB2180 Polished/Brushed : product name
AB2180 Polished Chrome|AB2180 Brushed Nickel : associated product names
Below are the table and files that helped me to understand
Database Table : catalogsearch_fulltext
CodeFiles or classes :
- Mage_CatalogSearch_Model_Resource_Fulltext
- Mage_CatalogSearch_Model_Resource_Fulltext_Engine
Below is the custom module that I have created to achieve my requirement.
config.xml.
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Custom_SkuSearch>
<version>1.0.0</version>
</Custom_SkuSearch>
</modules>
<global>
<models>
<skuSearch>
<class>Custom_SkuSearch_Model</class>
<resourceModel>skuSearch_resource</resourceModel>
</skuSearch>
<skuSearch_resource>
<class>Custom_SkuSearch_Model_Resource</class>
</skuSearch_resource>
</models>
<resources>
<skuSearch_setup>
<setup>
<module>Custom_SkuSearch</module>
<class>Custom_SkuSearch_Model_Resource_Setup</class>
</setup>
</skuSearch_setup>
</resources>
<events>
<catalog_product_save_after>
<observers>
<skuSearch>
<type>model</type>
<class>Custom_SkuSearch_Model_Observer</class>
<method>catalog_product_save_after</method>
</skuSearch>
</observers>
</catalog_product_save_after>
</events>
</global>
</config>
install-1.0.0.php
<?php
// This installer scripts adds a product attribute to Magento programmatically.
// Set data:
$attributeName = 'Child SKUs'; // Name of the attribute
$attributeCode = 'child_sku'; // Code of the attribute
$attributeGroup = 'General'; // Group to add the attribute to
// Configuration:
$data = array(
'type' => 'text', // Attribute type
'input' => 'text', // Input type
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, // Attribute scope
'required' => false, // Is this attribute required?
'user_defined' => false,
'searchable' => true,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'unique' => false,
'used_in_product_listing' => true,
'apply_to' => 'grouped,configurable,bundle',
'label' => $attributeName
);
// Create attribute:
// We create a new installer class here so we can also use this snippet in a non-EAV setup script.
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->startSetup();
$installer->addAttribute('catalog_product', $attributeCode, $data);
// Add the attribute to the attribute sets:
$entityType = Mage::getModel('catalog/product')->getResource()->getEntityType();
$collection = Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($entityType->getId());
foreach ($collection as $attributeSet) {
$attributeGroupId = $installer->getDefaultAttributeGroupId('catalog_product', $attributeSet->getId());
$installer->addAttributeToSet('catalog_product', $attributeSet->getId(), $attributeGroupId, $attributeCode);
}
$installer->endSetup();
Observer.php
<?php
class Custom_SkuSearch_Model_Observer {
public function catalog_product_save_after($observer) {
$product = $observer->getProduct();
$productTypeId = $product->getTypeId();
if (in_array($productTypeId, array("configurable", "grouped", "bundle"))) {
if ($productTypeId == "configurable") {
$skus = array();
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product);
foreach ($childProducts as $child) {
$skus[] = $child->getSku();
}
$skus = implode(', ', $skus);
}
}
$product->setChildSku($skus);
$product->getResource()->saveAttribute($product, 'child_sku');
}
}