Magento 2 API usage

Recently, in my day to day activities I had to figure something out with the Magento API. Magento 2 that is. Which is a beast, to say the least (hey! just made a rhyme!).

Anyway, I posted the question  I had at the time in the Magento dedicated subsite on StackExchage in the hope that someone from the well-formed community can give me an answer. Unfortunately nothing yet (posted about a week ago, at the time of writing this).

Here it is:

I’m trying to add new products to a Magento 2.1.7 and noticed some strange behaviours when it comest to global attributes (special_price for this example), found a workaround (kind of…) and was looking for a confirmation from the community if what I’m seeing in there is correct.

So, scenario: Magento 2.1.7 installation, only 1 store.
The endpoint used is rest/V1/products/, but using it like that, although it assigns the product to the one store I have in the installation, it also creates a row in the >catalog_product_entity_decimal table having store_id as 1, when a global attribute should have only 0 – is that correct?

Now, that attribute ends up in being a rogue row in there because no matter what I’m trying in the admin to do with it, being a global attribute, the admin’s not touching that at all. The problem on the frontend after that is that the product in question will always show the special_price, because it finds an attribute with the current store_id, doesn’t matter if it’s global or not, it just shows it (now this is an assumption, haven’t checked that, must admit, but it kinda makes sense).

If I am changing the endpoint to rest/all/V1/products/ it does not create the store_id=1 attribute in that table, but the product is not assigned to the store front either, and although now the special_price is not an issue anymore, I have to go in the admin for all products created like that and assign them to the only default website in order to be visible on the front-end.

Doing a bit more ‘light reading’ through http://devdocs.magento.com/swagger/index_20.html
I found that I can follow up the initial API call when I create the product with the /all/ endpoint that eliminates the special_price problem with another call for rest/all/V1/products/webites with the following JSON payload:

{
    "productWebsiteLink": {
    "sku": "newly-created-product-sku",
    "websiteId": 1
    }
}

It assignes the product to the only available website, shows in the front-end and all my problems are solved.

Now, the ‘million dollars question’: is this normal? Is that how it should be done? Create the product unassigned to a website and follow that up with another API call to assign it? If so, is there somewhere in the documentation that tells you the exact steps to follow?

The closest thing I could find about this problem (although it states it’s about getting products) is this: https://github.com/magento/magento2/issues/8121 and I got there following https://magento.stackexchange.com/questions/181906/magento2-save-product-data-for-global-scope-using-rest-api

Any opinions/links/advice will be greatly appreciated.

Thanks

I’m not sure if the lack of replies are due to this being a stupid question, but I kinda figured it out in the meantime, that pretty much how you should use it: create the product and after, assign it to a website with a subsequent API call.

Digging a bit deeper, I found another problem though: let’s say you want t create a few simple products that are going to be the part of a configureable product – make sure you reflect that “option” in the product, it’s not enough to change just the SKU in the json payload when you create the product.

The reason for that is that Magento created the value for the url_key attribute at the of creating the product, without checking if the same value already exists in the catalog_product_entity_varchar table. You can create a new product with the same title, you can even update info abut an already existing one, but when you’ll try to assign it to a website, it will throw an error stating that URL key for specified store already exists .

Good enough reason to post on my blog! Yey!

Comments