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

February 22, 2017,
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' => '.$key.'" class="hint">.Mage::getBaseUrl('media')."Manufacturer/".$name.'">'.$name.'   .$key.'" value="'.$pdf['manufacturer_id'].'" />.$key.');">Delete'
                    ));
                    $_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 '
';*/
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.
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']."
"
; } ?>
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');
?>

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 "

Manufacturers Added

    "; foreach($addedManufacturers as $added) { echo "
  • " . $added . "
  • "
    ; } echo "
"
; } if (count($skippedManufacturers)) { echo "

Manufacturers Skipped

    "; foreach($skippedManufacturers as $skipped) { echo "
  • " . $skipped . "
  • "
    ; } echo "
"
; }

Download Magento Shop by Brand Extension 

shop by brand magento