2

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.

0

As still now i didnt found a solution here is what i am doing right now to achieve it.

1) created a new attribute with 1 or 0 value. Now go to Magento\Catalog\Model\ProductRepository save function. And added the below piece of code in the beginning.(I would advice to override it and add the code as i did this only for development purposes.)

if($product->getData('new_attribute') == 1 && $this->areaCode->getAreaCode() == 'webapi_rest') {
        $product->setData('save_rewrites_history',1);
    }

Now if you send an updated url then it will create a 301 redirect from the new url to the old one.

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.