Magento – Best practice to connect Magento 2 to price/product service

apicatalogmagento2priceproduct

I have my own price/product service that I need to connect to Magento.
The service has a variable amount of products with very variable prices.
The price can change with every request a customer makes. So it needs to be fetched every time a customer requests the page at least. But probably even in between, informing him/her of an updated price.

What is the best practice to do this in Magento 2?
Do I completely drag out the price part of Magento to my API?
Do I overwrite prices in the Magento database with my API?
Do I fetch prices on the fly with the API or do I overwrite them after Magento sets the prices?
Or …?

At most I want to prevent being forced to maintain products and prices in two places (Magento, my database).

Thanks for any information, read-ups, books about the topic!

Best Answer

This is just a part of a part of an idea.
For the product view pages and the cart you can simply retrieve the prices from your external source using the vent catalog_product_get_final_price.
But if you have a lot of products you might get into performance problems.

But this approach won't work for listing products.
The product prices are indexed in catalog_product_index_price.
If you have a lot of discount rules and a lot of tier prices I have no idea how you can achieve this, but if you have just simple prices, you can try to rewrite the method \Magento\Catalog\Model\ResourceModel\Product::_productLimitationPrice and retrieve your prices from the third party for the current collection.

but again, if you have a lot of products, this will end up in performance issues.

Related Topic