Year End Sale -2o% Off On All Products - Coupon: FMADEC2017

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.

Answer

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
            $i=1;
            foreach($rowsArray as $key=>$pdf){              
                $_SESSION['admindbmanufacuter']="yes";
                $name=$pdf['pdfname'];

                $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>'
                    ));
                    $_SESSION['mycustomefield']=$i;$i++;
            }

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); 
                                if($pdfname[1]=='pdf'){ 
                                    $connectionWrite = Mage::getSingleton('core/resource')->getConnection('core_write');
                                    $pdf= str_replace(" ","_",$pdf);
                                    $newdata['pdfname']= time().'_'.$pdf;
                                    $newdata['menufecturer_name']=$data['menufecturer_name'];                       
                                    $connectionWrite->insert('manufacturer_pdf', $newdata);


                                    $path = Mage::getBaseDir('media') . DS ."Manufacturer".DS; 
                                    move_uploaded_file($_FILES['pdfname']['tmp_name'][$key],$path.$newdata['pdfname']);

                                    $connectionWrite->commit();
                                }else{
                                    Mage::getSingleton('adminhtml/session')->addError("Upload PDF Files Only");
                                    Mage::getSingleton('adminhtml/session')->setFormData($data);
                                    $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                                    return;
                                }   
                            }
                        }   
                    }   

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.

Answer

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')
            ->addAttributeToSelect('url')
            ->addAttributeToFilter('entity_id', $currentCatIds)
            ->addIsActiveFilter();
        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.

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

Answer

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

SELECT DISTINCT value
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.

Answer

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.

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

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

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.

<?php
require_once 'app/Mage.php';
umask(0);
Mage::app('default');
$_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){
$i++;
$manufacturers['value']['option_' . $i] = array($_manufacturer);
}
$_attribute->setOption($manufacturers);
try{
$_attribute->save();
echo 'Manufacturer successfully imported';
}catch(Exception $e){
echo 'Import Error::'.$e->getMessage();
}

?> 

Answer

Please use this code to achieve this.

<?php

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


/*
 * Boostrap Magento
 */
$mageFilename = '../app/Mage.php';
require_once $mageFilename;        
Mage::setIsDeveloperMode(true);     
umask(0);

$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')
            ->setAttributeFilter($attribute->getData('attribute_id'))
            ->setStoreFilter(0, false)
            ->getColumnValues('value');

$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;
        continue;
    }

    //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;
    $installer->addAttributeOption($newOption);

    $optionValue = Mage::getResourceModel('eav/entity_attribute_option_collection')
            ->setAttributeFilter($attribute->getId())
            ->setStoreFilter(0, false)
            ->addFilter('value_id', $installer->getConnection()->lastInsertId())   
            ->getColumnValues('option_id');

    $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