7

I have created a custom attribute(multiselect) for product with the same filter type as category selection on Magento 2. Attribute selection is working fine when creating a new product however it is not working on edit form. I have noticed below error from knockout js on console.

knockout.js:1449 Uncaught TypeError: Cannot read property 'apply' of undefined at Function.ko.observableArray.fn.(anonymous function) [as push]

Also I have attached the screenshots of the error I am facing. Add productError

A help to fix this issue would be much appreciated.

2
  • any luck? i had the same after magento upgrade 2.2.2
    – apedic
    Commented Dec 28, 2017 at 14:14
  • @AntonioPedicini any luck ? i had same issue........ Commented Sep 24, 2020 at 9:05

2 Answers 2

2

The reason this probably fails is because you are retrieving data from database as a string.

You should edit your DataProvider ( Namespace/Module/Model/Test/DataProvider.php ) and change the getData() method with something like

foreach ($items as $test) {
    ...
    $this->loadedData[$test->getId()] = $test->getData();
    $customAttributes = ['Test1', 'Test2']; // here you should do your own retrieving of data from the database
    $this->loadedData[$test->getId()]['custom_attribute'] = $customAttributes;
    ...
    return $this->loadedData;
}

This did the trick for me because I add the multiselect in a UI component that relies on a DataProvider.

1
  • This is working fine thanks
    – S.Venky
    Commented Jan 29, 2021 at 5:16
1

Hello I know this is too much late for posting the answer but recently i stuck in the same problem and after too much debugging i found the solution...

when we convert product multi-select attribute to ui-component(like as category) the main issue is occurs at the time of loading the product-edit-form like according the above question at the time of creating of product it will save the data correctly but when we open product-edit-form the attribute gets the data in the from of comma separated string but the multi select ui component need the data in the form of array. So just Convert the String into the Array and your problem has been Solved :)

Here is the Piece of code which is used for solved the problem

In your DataProvider file you need to override getMeta()

public function modifyData(array $data) {
       
        //here data is the array of UI Component
       //we are modifying the data of the array

    $model = $this->locator->getProduct();
    $modelId = $model->getId();
    $path = $modelId . '/' . self::DATA_SOURCE_DEFAULT . '/' . 'your_attribute_id'; 
    $dataString = $data[$modelId][self::DATA_SOURCE_DEFAULT][$path];
    $dataArray = explode(',', $dataString);
    $data = $this->arrayManager->remove($path, $data);
    $data = $this->arrayManager->set($path,$data,$dataArray);     
    
    return $data;
}

Hope It will help :)

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.