Solution for Magento 2 Configurable product has price 0 in REST API Response

By

August 6, 2018MagentoNo comments

Magento 2 REST API is returning price zero for configurable products. Conceptually a Configurable Product doesn’t have a price associated with it, only the child products associated with it will have the price. But for rendering a configurable product on a Mobile App with Magento 2 REST API this will look odd. This issue is raised in github and the bug seems to be open.

https://github.com/magento/magento2/issues/10644

Screenshot taken from the POST Man API Response in Magento 2.2.5.

API End Points Used on sample Data:
1. V1/products/WJ01
2. V1/products/?searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=23&searchCriteria[filter_groups][0][filters][0][condition_type]=eq&searchCriteria[filter_groups][0][filters][1][field]=visibility&searchCriteria[filter_groups][0][filters][1][value]=4&searchCriteria[filter_groups][0][filters][1][condition_type]=eq&searchCriteria[pageSize]=10

Magento2 REST API Response with Price 0 for configurable products

The first step in resolving this problem is to find the code which returns 0. The below core code returns the price as 0.

The Above core code function needs to be Hooked using “Plugin” Approach. In the new Plugin function we need to send the final price of the configurable product. If you are new to the Plugin Concept please refer the below official guide

https://devdocs.magento.com/guides/v2.2/extension-dev-guide/plugins.html

Lets create a di.xml to hook the class Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price

For this illustration i am going to use the aroundMethod to hook in to the existing function, since i need the product object argument to get the final price.

Screenshot of POST MAN API Response After the above workaround

POST MAN Screenshot after Price 0 Issue Fixed

For this illustration i have presented only the main files, if anyone is interested in the full module leave a comment, so that i will provide the full module link.

Leave a Reply