I was trying to update a product with new url-key. But create permanent redirect to the old url
doesnt work from API.
First i am creating product with this JSON data http://domain.com/rest/all/V1/products
$post ='{
"product": {
"sku": "B501-SKU",
"name": "B501",
"price": 30.00,
"status": 1,
"type_id": "simple",
"attribute_set_id":4,
"weight": 1
}
}';
Now if i want to update the url and create a 301 redirect from old url to new url then i am hitting this http://domain.com/rest/all/V1/products/B501-SKU (sku of the product)
$post ='{
"product": {
"name": "B506",
"customAttributes": [
{
"attribute_code":"meta_title",
"value":"test"
},
{
"attribute_code":"save_rewrites_history",
"value":1
},
{
"attribute_code": "url_key",
"value": "b506"
}
]
}
}';
Now if i go to Magento\Catalog\Model\ProductRepository save() function and try to print the product object i will see that it has removed save_rewrites_history
and has kept rest as is. probably because its not attribute.
A solution to the problem
1) created a new attribute and if you set the attribute value then
if($product->getData('new_attribute') == 1 && $this->areaCode->getAreaCode() == 'webapi_rest') {
$product->setData('save_rewrites_history',1);
}
It fixed the problem but can anyone tell me how magento refines the data before the save function? Why only attributes are getting passed.