0

In my store, I have a product attribute UPC.
Suppose a configurable product A(sku - parentA, upc attribute blank) is having two child products A1(sku - childA1, upc - 101) and A2(sku - childA2, upc - 102).
Parent products visibility is Catalog, Search and childs are not visible individually.

Now when I am search upc 101 or 102 in frontend, Magento search is showing me correct result with the configurable product A.

My first question is how magento is picking here the correct parent product?
Second question is how I can achieve same functionality for the sku attribute, because on searching with sku childA1 or childA2, I am not getting the parent product.

0

4 Answers 4

0

Search result coming from this file app/code/core/Mage/CatalogSearch/Block/Autocomplete.php

Admin setting:- Admin > System >Configuration > Catalog > Catalog Search > Search Type

0

Relation tables search criteria configured in Mage_CatalogSearch_Model_Resource_Indexer_Fulltext class Fulltext.php in Indexer directory.

You will find this method in it.

public function getRelationsByChild($childIds)
{
    $write = $this->_getWriteAdapter();
    $select = $write->select()
        ->from($this->getTable('catalog/product_relation'), 'parent_id')
        ->where('child_id IN(?)', $childIds);

    return $write->fetchCol($select);
}
0

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');
    }

}
0

An easy work around for this is to add in a new attribute for grouped products only "related SKU's" and add in the SKU codes of the simple products applied to the group product individually. Just be sure to allow this attribute in search - it has worked fine for us.

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.