Magento – Unset custom data from product attribute

customcustom-attributesmagento2product

I am trying to update a product with some custom attributes. I need to unset data that is being passed to set the value to none. I am currently on Magento 2.1

... 
 $updatedProduct->unsetData($customAttributeA);
 $updatedProduct->save();

I added a break-point after unset the data, customAttributeA row is indeed gone from the product data array. However, after calling the save function, customAttributeA comes back into the array, with the value previously set.

Is there a way to persist the unsetData during the save function? I have also tried

$product->setData($customAttributeA , "");
And
$product->setCustomAttribute($customAttributeA , "");

However, none of them seems to work.

The attribute type i am trying to unset is a dropdown type.

Cheers

edit- this is a snapshot of the code i am using to set/unset the data

 foreach ($listTypes as $key => $value) {
    if (isset($params[$value])){
       if(isset($params[$value . '_mid'])) {
           $product->setCustomAttribute($value, $params[$value.'_mid']);
       }else{     
           // unset data here
       }
    }
 }

 $product->save();

Best Answer

I found this one https://stackoverflow.com/a/8672847. It looks like Magento 1 code but it's work for me.

$product->setData('unwanted_attribute',null);        
$resource = $product->getResource();
$resource->saveAttribute($product,'unwanted_attribute');
Related Topic