Search code examples
sqljoinmagento

Magento how get options with some additional information


I am working with options, to add some additional info like image. and I saved this data to my own table with option_type_id and option_id. now on frontend I would like to join my own table data to default options. so these options come with image info.

$_option->getValues() 

this function returns option data, now I have to reach the implementation of this function where it generate the query so I could add join to retrieve my own data with.


Solution

  • Here is what i got success from.

    i overridden the resource collection of product

     class MYC_COPSwatch_Model_Resource_Product_Option_Collection extends Mage_Catalog_Model_Resource_Product_Option_Collection{        
    
         public function addValuesToResult($storeId = null)
    {
        if ($storeId === null) {
            $storeId = Mage::app()->getStore()->getId();
        }
        $optionIds = array();
        foreach ($this as $option) {
            $optionIds[] = $option->getId();
        }
        if (!empty($optionIds)) {
            /** @var $values Mage_Catalog_Model_Option_Value_Collection */
            $values = Mage::getModel('catalog/product_option_value')
                ->getCollection()                
                ->addTitleToResult($storeId)
                ->addPriceToResult($storeId)                
    
                ->addSwatchToResult($storeId)   //USED Join in this function             
    
                ->setOrder('sort_order', self::SORT_ORDER_ASC)
                ->setOrder('title', self::SORT_ORDER_ASC);
    
            foreach ($values as $value) {
                $optionId = $value->getOptionId();
                if($this->getItemById($optionId)) {
                    $this->getItemById($optionId)->addValue($value);
                    $value->setOption($this->getItemById($optionId));
                }
            }
        }
    
        return $this;
    } 
    

    might be save time for someone.