6

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.

  • any luck? i had the same after magento upgrade 2.2.2 – Antonio Pedicini Dec 28 '17 at 14:14
  • @AntonioPedicini any luck ? i had same issue........ – Akash Agrawal Sep 24 '20 at 9:05
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 :)

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.