Get parent category in admin product upload

zerdnem

New member
XNullUser
Joined
Jan 30, 2022
Messages
15
Reaction score
0
Points
1
NullCash
12
This is a simple extension that I made to get the parent category in admin page product upload.

chrome_KA62L3Vo82.gif
Source code
XML:
<?xml version="1.0" encoding="utf-8"?>
<modification>
    <name>Get Parent Categories</name>
    <version>1.0</version>
    <author>Jay Mendrez</author>
    <link>https://github.com/zerdnem</link>
    <code>get_parent_categories</code>
    <description>Get main categories in admin product form category autocomplete</description>
    <file path="admin/controller/catalog/category.php">
        <operation>
            <search>
            <![CDATA[
            public function autocomplete() {
            ]]>
            </search>
            <add position="before">
                <![CDATA[
    public function getParentCategories() {
        $json = array();
        if (isset($this->request->get['filter_category_id'])) {
            $this->load->model('catalog/category');

            $category_paths = $this->model_catalog_category->getCategoryPath($this->request->get['filter_category_id']);
            $categories = array();
            foreach ($category_paths as $path) {
                if ($this->request->get['filter_category_id'] != $path['path_id']) {
                    $category_descriptions = $this->model_catalog_category->getCategoryDescriptions($path['path_id']);

                    $filter_data = array(
                        'filter_name' => $category_descriptions[1]['name'],
                        'sort'        => 'name',
                        'order'       => 'ASC',
                        'start'       => 0,
                        'limit'       => 5
                    );

                    $categories[] = $this->model_catalog_category->getCategories($filter_data);
                }
            }
            foreach ($categories as $category) {
                $json[] = array(
                    'category_id' => $category[0]['category_id'],
                    'name'        => strip_tags(html_entity_decode($category[0]['name'], ENT_QUOTES, 'UTF-8'))
                );
            }
        }
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }
                ]]></add>
        </operation>
    </file>

    <file path="admin/view/template/catalog/product_form.twig">
        <operation>
            <search>
            <![CDATA[
            $('#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>');
            ]]>
            </search>
            <add position="after">
                <![CDATA[
            console.log(item)
            if (item) {
              $.ajax({
                url: 'index.php?route=catalog/category/getParentCategories&user_token={{ user_token }}&filter_category_id=' +  encodeURIComponent(item['value']),
                dataType: 'json',
                success: function(json) {
                  console.log(json)
                  $.map(json, function(category) {
                  $('input[name=\'category\']').val('');
                  $('#product-category' + category['category_id']).remove();
                  $('#product-category').append('<div id="product-category' + category['category_id'] + '"><i class="fa fa-minus-circle"></i> ' + category['name'] + '<input type="hidden" name="product_category[]" value="' + category['category_id'] + '" /></div>');
                  });
                }
              });     
            }
                ]]></add>
        </operation>
    </file>
</modification>
 

Attachments

  • chrome_KA62L3Vo82.gif
    chrome_KA62L3Vo82.gif
    68.1 KB · Views: 0
Top