1

I am using Magneto2.3.0 I need this adding the products automatically to the parent category and to the subcategories which are on higher level

If anyone idea reply me how to achieve this requirement. I am trying to below answer code than getting this

please check it my admin image

how to remove selected category above c d p b s options

enter image description here

here below my catalog-category -section image there other brand category inside child category assign it automatically like c d p b s. if i assign shop by department->beauty & personal care->hair->seriums/oils but in above this category other parent category inside child category assign it. so this is wrong to our requirement wise.i share you my category structure below image mentioned

enter image description here

please check i have to install other default magento setup inside this code apply after getting this using luma theme by default.

enter image description here

enter image description here

9
  • You are talking about in admin category assignment? Commented May 27, 2019 at 8:08
  • yes i am talking in admin side product edit page
    – Rv Singh
    Commented Jun 3, 2019 at 10:39
  • if you have any idea reply me
    – Rv Singh
    Commented Jun 3, 2019 at 10:39
  • you can override web/js/form/element/ui-select.js js and modify that according to your need. Commented Jun 3, 2019 at 10:50
  • you will get all categories in this objects by which you can select/unselect categories. Commented Jun 3, 2019 at 10:51

1 Answer 1

0

First Override this file in your custom module

vendor/magento/module-ui/view/base/web/js/form/element/ui-select.js

Modify the toggleOptionSelected function to select parent category.

   toggleOptionSelected: function (data) {

    var isSelected = this.isSelected(data.value);

    if (this.lastSelectable && data.hasOwnProperty(this.separator)) {
        return this;
    }

    if (!this.multiple) {
        if (!isSelected) {
            this.value(data.value);
        }
        this.listVisible(false);
    } else {
        if (!isSelected) { /*eslint no-lonely-if: 0*/
            this.value.push(data.value);

            /* to add current category and callled recursive function for selecting parent category*/
            $.each(data.optgroup, function( key, value ) {
                self.value.push(value.value);
                if (typeof value.optgroup !== 'undefined') {
                    self.childToParentIterate(value,'add');
                }
            });
            if (typeof data.optgroup === 'undefined') {
                self.childToParentIterate(data,'add');
            }

        } else {
            this.value(_.without(this.value(), data.value));
        }
    }

    return this;
}

Added new functions to check the parent category.

childToParentIterate: function(data, op){

    var self = this;

    var path = data.path;

    var level = data.level;

    $.each(this.cacheOptions.tree, function( key, value ) {
        var optgroup = value.optgroup;

        /*if you have your root level category with other label then change this label "Default Category" in below condition */
        if(value.label == "Default Category"){
            $.each(optgroup, function( optkey,optval ) {
                var label = optval.label;
                if(path.indexOf(label) != -1){
                    if(op == "add") self.value.push(optval.value);
                }  

                self.childIteration(optval,op,level,path);
            });
        }
    });
}

childIteration: function(data, op,level,path){

    var self = this;           
    if(data.level < level){
        var optgroup = data.optgroup;
        $.each(optgroup, function( optkey,optval ) {
            var label = optval.label;                   
            if(path.indexOf(label) != -1){
                if(op == "add") self.value.push(optval.value);
            }                    
            self.childIteration(optval,op,level,path);
        });
    }
}
25
  • I have to added in this code but in admin side edit product after i selected child cateogry after save than again check it same product not selected parent category automatically
    – Rv Singh
    Commented Jun 3, 2019 at 13:37
  • 1
    I have to use it but getting this error after save it i select child product than automatically selected parent but other display i updated my question check it
    – Rv Singh
    Commented Jun 4, 2019 at 14:58
  • Is c d b p s is your root level categories? Commented Jun 4, 2019 at 15:59
  • 1
    if you have time to tomorrow reply me i am waiting your answer its helpfull
    – Rv Singh
    Commented Jun 4, 2019 at 16:29
  • 1
    In my fresh magento setup i use your code after child wise parent selected but in this label root "Default Category" not showing in admin side when i save the product. can u guide me how to fetch full recursive tree
    – Rv Singh
    Commented Jun 27, 2019 at 7:49

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.