Thursday 14 May 2015











STEP 1;- Create Table. "oc_category_multiparen"

CREATE TABLE IF NOT EXISTS `oc_category_multiparent` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `category_id` int(11) NOT NULL,
  `parent_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


STEP 2:-  update oc_category set parent_id=0;


STEP 3:- Change follwoing files as below
   I admin/model/catalog/category.php
      Add new method. as follwing.

 -------------------------------------------
         public function getParentCategories($category_id) {
              //echo " inside getParentCategories".$category_id;
$parent_category_data = array();

$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_multiparent WHERE category_id = '" . (int)$category_id . "'");

foreach ($query->rows as $result) {
$parent_category_data[] = $result['parent_id'];
}

return $parent_category_data;
}
     
  -------------------------------------
public function getMultipleParentCategories($category_id) {

$query = $this->db->query("SELECT CONCAT( CONCAT( '(', GROUP_CONCAT( cd1.name ORDER BY cp.level SEPARATOR ')(' ) ) , ') >' ) AS parentcategories FROM " . DB_PREFIX . "category_multiparent cmprt LEFT JOIN " . DB_PREFIX . "category_description cd1 ON ( cmprt.parent_id = cd1.category_id ) LEFT JOIN " . DB_PREFIX . "category_path cp ON ( cp.category_id = cmprt.parent_id ) WHERE cmprt.category_id='" . (int)$category_id . "'");            

             
return $query->rows;
}

--------------------------------
 Inside existing function public function editCategory($category_id, $data) { add follwing code

$this->db->query("DELETE FROM " . DB_PREFIX . "category_multiparent WHERE category_id = '" . (int)$category_id . "'");
                if (isset($data['product_category'])) {
foreach ($data['product_category'] as $parent_id) {
                            echo "Category parentid". $parent_id;
$this->db->query("INSERT INTO " . DB_PREFIX . "category_multiparent SET category_id = '" . (int)$category_id . "', parent_id = '" . (int)$parent_id . "'");
}
}

================================================
  2. admin/language/english/catalog/category.php
      Add  follwing Lines

    $_['entry_category']         = 'Parent Categories';
    $_['help_category']          = '(Autocomplete)';
==============================================
  3. admin/view/template/catalog/category_form.tpl
      Add  follwing Lines
   ---------------------------------------------------AT html section------------------------
<div class="form-group">
               <label class="col-sm-2 control-label" for="input-category"><span data-toggle="tooltip" title="<?php echo $help_category; ?>"><?php echo $entry_category; ?></span></label>
               <div class="col-sm-10">
                  <input type="text" name="category" value="" placeholder="<?php echo $entry_category; ?>" id="input-category" class="form-control" />
                 <div id="product-category" class="well well-sm" style="height: 150px; overflow: auto;">
                    <?php foreach ($product_categories as $product_category) { ?>
                    <div id="product-category<?php echo $product_category['category_id']; ?>"><i class="fa fa-minus-circle"></i> <?php echo $product_category['name']; ?>
                      <input type="hidden" name="product_category[]" value="<?php echo $product_category['category_id']; ?>" />
                    </div>
                    <?php } ?>
                  </div>
               </div>

              </div>
--------------------------------------------------------following in script section-------------


// Category
$('input[name=\'category\']').autocomplete({
'source': function(request, response) {
$.ajax({
url: 'index.php?route=catalog/category/autocomplete&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request),
dataType: 'json',
success: function(json) {
response($.map(json, function(item) {
return {
label: item['name'],
value: item['category_id']
}
}));
}
});
},
'select': function(item) {
$('input[name=\'category\']').val('');


// Category
$('input[name=\'category\']').autocomplete({
'source': function(request, response) {
$.ajax({
url: 'index.php?route=catalog/category/autocomplete&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request),
dataType: 'json',
success: function(json) {
response($.map(json, function(item) {
return {
label: item['name'],
value: item['category_id']
}
}));
}
});
},
'select': function(item) {
$('input[name=\'category\']').val('');

$('#product-category' + item['value']).remove();

$('#product-category').append('<div id="product-category' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="product_category[]" value="' + item['value'] + '" /></div>');
}
});
$('#product-category').delegate('.fa-minus-circle', 'click', function() {
$(this).parent().remove();
});
$('#product-category' + item['value']).remove();

$('#product-category').append('<div id="product-category' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="product_category[]" value="' + item['value'] + '" /></div>');
}
});
$('#product-category').delegate('.fa-minus-circle', 'click', function() {
$(this).parent().remove();
});
=======================================================
 4. admin/controller/catalog/category.php
      Add  follwing Lines in existing method getForm() {
  -----------------------------------------------
$data['entry_category'] = $this->language->get('entry_category');
$data['help_category'] = $this->language->get('help_category');
-----------------------------------------------

 if (isset($this->request->post['product_category'])) {
$categories = $this->request->post['product_category'];
} elseif (isset($this->request->get['category_id'])) {
$categories = $this->model_catalog_category->getParentCategories($this->request->get['category_id']);
} else {
$categories = array();
                }
             
                $data['product_categories'] = array();
               foreach ($categories as $category_id) {
                        $category_info = $this->model_catalog_category->getCategory($category_id);

if ($category_info) {
$data['product_categories'][] = array(
'category_id' => $category_info['category_id'],
'name' => ($category_info['path']) ? $category_info['path'] . ' &gt; ' . $category_info['name'] : $category_info['name']
);
}
}

------------------------ in Existing method protected function getList() { update following code -------------------------------------foreach ($results as $result) {
                         $multipleCategories = $this->model_catalog_category->getMultipleParentCategories($result['category_id']);
                        // echo $multipleCategories;
$data['categories'][] = array(
'category_id' => $result['category_id'],
'name'        => $multipleCategories[0]['parentcategories'].$result['name'],
'sort_order'  => $result['sort_order'],
'edit'        => $this->url->link('catalog/category/edit', 'token=' . $this->session->data['token'] . '&category_id=' . $result['category_id'] . $url, 'SSL'),
'delete'      => $this->url->link('catalog/category/delete', 'token=' . $this->session->data['token'] . '&category_id=' . $result['category_id'] . $url, 'SSL')
);
}
==================================
5. catalog/controller/common/header.php
        replace the following line with the following

 //$children = $this->model_catalog_category->getCategories($category['category_id']);
                      Replace above with the below line In ControllerCommonHeader's  loop section foreach ($categories as $category) {
if ($category['top']) {
  $children = $this->model_catalog_category->getMultiParentCategories($category['category_id']);
}
------------------------------------
6. catalog/model/catalog/category.php
      Add following new method

   public function getMultiParentCategories($parent_id = 0) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) LEFT JOIN " . DB_PREFIX . "category_multiparent cmprt ON (c.category_id = cmprt.category_id) WHERE cmprt.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "'  AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");

return $query->rows;

}
====================================
 :) MULTI PARENT CATEGORY OPENCART Interation is Done Enjoy :)