0

We created custom module and we saved list of product Brands & Models in backend. In below image, Apple, sony, samsung are brands & iphone4, iphone4s...etc are models.

enter image description here

Brands

enter image description here

Models

enter image description here

We want to create dropdown options for both Brands & Models in frontend. Once we select particular brand from 1st drop-down box, then only associated models should display in 2nd Drop-down box.

enter image description here

Company/Brand/Block/Frontend/View.php

class Company_Brand_Block_Frontend_View extends Mage_Catalog_Block_Product_Abstract
{
     protected function _prepareLayout()
    {
        $this->getLayout()->createBlock('catalog/breadcrumbs');
        $headBlock = $this->getLayout()->getBlock('head');
        if ($headBlock) {
            $product = $this->getProduct();
                $title = $product->getMetaTitle();
            if ($title) {
                $headBlock->setTitle($title);
            }
            $keyword = $product->getMetaKeyword();
            $currentCategory = Mage::registry('current_category');
            if ($keyword) {
                $headBlock->setKeywords($keyword);
            } elseif ($currentCategory) {
                $headBlock->setKeywords($product->getName());
            }
            $description = $product->getMetaDescription();
            if ($description) {
                $headBlock->setDescription( ($description) );
            } else {
                $headBlock->setDescription(Mage::helper('core/string')->substr($product->getDescription(), 0, 255));
            }
            if ($this->helper('catalog/product')->canUseCanonicalTag()) {
                $params = array('_ignore_category' => true);
                $headBlock->addLinkRel('canonical', $product->getUrlModel()->getUrl($product, $params));
            }
        }

        return parent::_prepareLayout();
    }
    public function getProduct()
    {
        $collection = Mage::getModel('catalog/category')->load(310)
            ->getProductCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('custom_phone_case', 1);

            $products=$collection->getFirstItem();

            $product =  Mage::getModel('catalog/product')->load($products->getId());
            $p=Mage::registry('product');
        if ($p=='') {Mage::register('product', $product);}
            return  $product;
        }

I will give 50 bounty if it works for me.

3
  • where you want to show these both dropdown. You need to use dynamic code for dropdowns
    – Sourav
    Commented Jul 19, 2016 at 0:59
  • I need ftp detail for this.
    – Sourav
    Commented Jul 19, 2016 at 4:00
  • @Paarth i posted related code, also please let me know if you want to see some other module related code. also i will post screenshots of db tables also. Commented Jul 19, 2016 at 5:23

2 Answers 2

2
+50

Create a select box for Brands like :

<select id="brand_select">
    <option value="">MY BRAND</option>
    <option value="12">Apple</option>
    <option value="123">Nokia</option>
    <option value="655">HTC</option>
    ......
    <option value="BRAND_ID">BRAND_NAME</option>
    .....
</select>

Now create an array for Models (with brand_id), like :

<?php

//Load a model and create an array like this :

/*
$models = array(
            BRAND_ID => array(MODEL_ID => MODEL_NAME,  ..... ),
            BRAND_ID => array(MODEL_ID => MODEL_NAME,  ..... ),
            ...

            );
*/

$models = array(
            12 => array(12 => "iphone4", 45 => "iphone4s", ..... ),
            123 => array(32 => "lorem", 56 => "ipsum", ..... ),
            655 => array(433 => "xyzzz", 546 => "abcd", ..... ),
            ...

            );

//Converting $models array to JSON :

echo "<script>";
echo "var models = ".json_encode($models).";";
echo "</script>";
?>

Now add a jQuery function :

jQuery(document).ready(function(){

    jQuery( "#brand_select" ).change(function() {
        var brandId = jQuery(this).val();
        jQuery('#model_select').empty().append('<option value="">MY MODEL</option>');

        jQuery.each(models[brandId], function(key,value) {
            jQuery('#model_select').append(jQuery('<option>', { 
                value: key,
                text : value
            }));
        });
    });

    //trigger change event first time if required
    //jQuery( "#brand_select" ).change();
});
18
  • Thanks a lot, we already created brands in backend. is we again need to write in code also? Commented Jul 12, 2016 at 13:52
  • first make brands select box as I shown, <option value="BRAND_ID">BRAND_NAME</option>. Keep Models selectbox empty. Commented Jul 12, 2016 at 13:53
  • sorry, i remember what client said, i will try now. Commented Jul 12, 2016 at 13:56
  • please check here Commented Jul 12, 2016 at 13:59
  • I can't find echo "<script>"; echo "var models = ".json_encode($models).";"; echo "</script>"; and jquery script also Commented Jul 12, 2016 at 14:01
1

Solution 1: Faster way to do this would be to get collection of Models when page is loading and create a json string of Models (with their ids and names) with respective Brands.

When user change selection of brand, then use JavaScript or jQuery to read json string and change options of Models drop-down.

Solution 2: Load only Brands drop-down values on page load and send ajax request to find Models for selected Brand every time. Then populate Models drop-down with resultant values of Models.

6
  • can you please update the answer with code. if you want to see any other code part of module, please let me know, i will post it like controlers, model, observer files. Commented Jul 12, 2016 at 12:35
  • Which solution would you like to implement? Commented Jul 12, 2016 at 12:37
  • whichever is easy.... Commented Jul 12, 2016 at 12:39
  • First solution will have faster response in populating option values. So please populate Brands drop-down on page load and create a json string from Models data and post the code. Commented Jul 12, 2016 at 12:43
  • sure than i will go with first one. Commented Jul 12, 2016 at 12:44

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.