سؤال

I tried to remove a category from an Product but wasn't successfully with it. It still remains as Product of that Category.

This is the code I'm using:

    $categories = array();
    $categories[] = $this->erp['mastercat'];
    if(isset($this->erp['article']['all_categories'])){
        if(is_array($this->erp['article']['all_categories']) && count($this->erp['shop']['all_categories'])>0){
            foreach($this->erp['article']['all_categories'] as $category){
                if(!in_array($category,$categories)){
                    $categories[] = $category;
                }
            }
        }else if(!empty($this->erp['article']['all_categories'])){
            if(!in_array($this->erp['article']['all_categories'], $categories)){
                $categories[] = $this->erp['article']['all_categories'];
            }
        }
    }
    $this->product->setCategoryIds($categories);

I'm using an blank array and only put the categories I need in it.

I don't understand why previously selected category and the new one (I moved it inside my erp from one to another category) are selected.

هل كانت مفيدة؟

المحلول 2

The link from Keyur Shah helped me. I had a problem in understanding how the system saves the categories. If you want to remove one from the list, you need to load all and unset the no more needed categories and add the new ones with theire own key. Then it works. ^^

Thanks a lot for the help

نصائح أخرى

You need to save also your product:

$this->product->save();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top