Top 5 Programming Questions and Answers to Integrate Products Brands in Magento Part - 1

Integration of brands on eCommerce website increase the worth of products and boost the conversion rate. Integration of brands on the website needs complex coding which is not an easy task for programmers. That is why a lot of developers face issues while coding and they ask for help on various programming platforms.  So, this article contains 5 top most asked questions by programmers to integrate brands and the answers provided in this article are given by community members, not represent us. Following are the 5 top questions and answers regarding Magento shop by brand integration.

Question No. 1

A user has uploaded the brand's logo with the manufacturer but failed to upload multiple pdf files.


Please use this code in the form.php file.

$connection = Mage::getSingleton('core/resource')->getConnection('core_read'); 
            $select = $connection->select()
                ->from('manufacturer_pdf', array('*')) // select * from tablename or use array('id','title') selected values
                ->where('menufecturer_name=?',Mage::registry('manufacturer_data')->getData('menufecturer_name'))               // where id =1
                ->group('pdfname');               // group by title

            $_SESSION['adminmanufacuterquery']=$rowsArray = $connection->fetchAll($select); // return all rows
            $rowArray =$connection->fetchRow($select);   //return row
            foreach($rowsArray as $key=>$pdf){              

                $fieldset->addField('pdfnameload'.$key, 'hidden', array(                       
                        'required'  => false,
                        'name'      => 'hidden',                        
                        'after_element_html' => '<span id="pdfrow'.$key.'" class="hint"><a href="'.Mage::getBaseUrl('media')."Manufacturer/".$name.'">'.$name.'</a>&nbsp;&nbsp;&nbsp;<span><input type="hidden" name="oldpdf'.$key.'" value="'.$pdf['manufacturer_id'].'" /></span><span style="cursor: pointer;" onclick="test('.$key.');">Delete</span></span>'

To save it in database use this code.

if(isset($_FILES['pdfname']['name']) && $_FILES['pdfname']['name'] != '') {                 
                        foreach($_FILES['pdfname']['name'] as $key=>$pdf){                          
                            if(isset($pdf) && $pdf != '') {
                                $pdfname=explode(".", $pdf); 
                                    $connectionWrite = Mage::getSingleton('core/resource')->getConnection('core_write');
                                    $pdf= str_replace(" ","_",$pdf);
                                    $newdata['pdfname']= time().'_'.$pdf;
                                    $connectionWrite->insert('manufacturer_pdf', $newdata);

                                    $path = Mage::getBaseDir('media') . DS ."Manufacturer".DS; 

                                    Mage::getSingleton('adminhtml/session')->addError("Upload PDF Files Only");
                                    $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));

Question No. 2

A user developing an e-store for his client and wants to create the brand pages. He also wants to display links to brand pages in product view and get the list of category or subcategory for that product.


You should allow the template to display the link. Use this script in Magento to show URL/List and categories that belong to products.

public function getProductUrl($productId){
    $product = Mage::getModel('catalog/product')->load(productId);
    $currentCatIds = $product->getCategoryIds();
    if ($currentCatIds) {
        $categoryCollection = Mage::getResourceModel('catalog/category_collection')->addAttributeToSelect('name')
            ->addAttributeToFilter('entity_id', $currentCatIds)
        foreach ($categoryCollection->getItems() as $item) {
            /*echo $item->getName();
            echo $item->getUrl();
            echo '<br>';*/
            if($item->getUrl()) return $item->getUrl();

Question No. 3

A user wants to create an array which contains all products manufacturers without going through the collection of items. He is also willing to create dynamic manufacturer page with links to brands. He has used the following script.

<?foreach ($brands as $brand):?>
<a href="<?=$helper->getBrandUrl($brand)?>"><?=$helper->getBrandName($brand)?></a>


Use this code to have all the brands and attribute numbers.

FROM eav_attribute_option o, eav_attribute_option_value v
WHERE attribute_id = '66'
AND o.option_id = v.option_id

Please use this code instead of the above code if you need in OOP.

$attribute = Mage::getModel('catalog/resource_eav_attribute')->load(66);
$attributeOptions = $attribute->getSource()->getAllOptions();  ?>
            <?php foreach ($attributeOptions as $option) {
            if ($option['value']) echo $option['value']." > ".$option['label']."<br/>";
            } ?>

Question No. 4

A user needs help to create category list and linked each category to its own page.


You have to edit your theme and use the below to script which will create the sub category of current category. You can use the code in app/design/frontend/default/theme_name/template/catalog/category/view.phtml.

$_category  = $this->getCurrentCategory(); 
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper     = Mage::helper('catalog/category');

    <?php foreach ($collection as $cat):?>
            <?php if($_category->getIsActive()):?>
                     $cur_category = Mage::getModel('catalog/category')->load($cat->getId());
                     $_img = $cur_category->getImageUrl();  
                    <a href="<?php echo $helper->getCategoryUrl($cat);?>">
                         <img src="<?php echo $_img?>" title="<?php echo $cat->getName();?>"/>
                         <cite><?php echo $cat->getName();?></cite>
            <?php endif?>
    <?php endforeach;?>

Question 5

A user wants to import brands list with pre-verification from the specific store, that brands which to be imported do not already occur and it gets new brands ID if imported. He has used this script to check existing brand by new ID and by name.

require_once 'app/Mage.php';
$_manufacturers = file('manufacturers.txt');
$_attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', 'manufacturer');
$manufacturers = array('value' => array(), 'order' => array(), 'delete' => array());
$i = 0;
foreach($_manufacturers as $_manufacturer){
$manufacturers['value']['option_' . $i] = array($_manufacturer);
echo 'Manufacturer successfully imported';
}catch(Exception $e){
echo 'Import Error::'.$e->getMessage();



Please use this code to achieve this.


error_reporting(E_ALL | E_STRICT);    
ini_set('display_errors', 1); 

 * Boostrap Magento
$mageFilename = '../app/Mage.php';
require_once $mageFilename;        

$mageRunCode = '';
$mageRunType = 'store';
Mage::init($mageRunCode, $mageRunType);

 * Set up required data
$newManufacturers = file('manufacturers.txt', FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
$newManufacturers = array_unique($newManufacturers);

$attribute = Mage::getModel('eav/entity_attribute')
                ->loadByCode('catalog_product', 'manufacturer');

$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
            ->setStoreFilter(0, false)

$installer = new Mage_Eav_Model_Entity_Setup('core_setup'); 

 * Add new attributes
$addedManufacturers      = array();
$skippedManufacturers    = array();

$i = count($valuesCollection);
foreach($newManufacturers as $manufacturer) {

    // If the value already exists then skip to next  
    if (in_array($manufacturer, $valuesCollection)) {
        $skippedManufacturers[] = $manufacturer;

    //If we have reached here then lets add the new attribute option
    $newOption = array();
    $newOption['attribute_id'] = $attribute->getData('attribute_id');
    $newOption['value']['option_'.++$i][0] = $manufacturer;

    $optionValue = Mage::getResourceModel('eav/entity_attribute_option_collection')
            ->setStoreFilter(0, false)
            ->addFilter('value_id', $installer->getConnection()->lastInsertId())   

    $addedManufacturers[] = $optionValue[0];

if (count($addedManufacturers)) {
    echo "<h2>Manufacturers Added</h2><ul>";
    foreach($addedManufacturers as $added) {
        echo "<li>" . $added . "</li>";
    echo "</ul>";

if (count($skippedManufacturers)) {
    echo "<h2>Manufacturers Skipped</h2><ul>";
    foreach($skippedManufacturers as $skipped) {
        echo "<li>" . $skipped . "</li>";
    echo "</ul>";

Download Magento Shop by Brand Extension 

shop by brand magento

Leave a Reply