3

I added a product custom attribute (type text) through the admin interface.

Now I'm trying to save a product through the api rest but if I include the new attribute in the data passed I get this error:

Property "NeedsSync" does not have corresponding setter in class "Magento\Catalog\Api\Data\ProductInterface"

I've read about extension attributes but I'm not sure if that's the way to do this in particular case.

I've tried to add as extension attribute by adding a etc/extension_attributes.xml with this content:

<extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
    <attribute code="needs_sync" type="string">
    </attribute>
</extension_attributes>

I've also read about the way of definig database table and join in the extension_attribute.xml for this attribute but I'm not sure this is the correct way, as this is a eav attribute for product entity so it is not a new table...

1 Answer 1

5

I figured it by myself... extension attributes are for more complex info.

For this particular case where it is a "simple" custom attribute (of type string), the way for updating via API is just to pass it inside of custom attributes array:

    $headers = array(
        'Content-Type: application/json',
        'Authorization: Bearer ' . $token
    );
    $data = array(
        'product' => array(
            'custom_attributes' => array(
                '0' => array(
                    'attribute_code' => 'needs_sync',
                    'value' => '0'
                )
            )
        )
    );
    $data = json_encode($data);
    $requestUrl = $this->_baseUrl . '/index.php/rest/V1/products/' . $sku;
    $ch = curl_init($requestUrl);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

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.