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
The first step in resolving this problem is to find the code which returns 0. The below core code returns the price as 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Classname Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price public function getPrice($product) { if (!empty($product)) { $simpleProductOption = $product->getCustomOption('simple_product'); if (!empty($simpleProductOption)) { $simpleProduct = $simpleProductOption->getProduct(); if (!empty($simpleProduct)) { return $simpleProduct->getPrice(); } } } return 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
1 2 3 4 5 6 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price"> <plugin name="mydons_configurableapiprice_plugin_product_type_configurable_price" type="Mydons\ConfigurableApiPrice\Plugin\Product\Type\Configurable\Price"/> </type> </config> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php namespace Mydons\ConfigurableApiPrice\Plugin\Product\Type\Configurable; class Price { public function aroundGetPrice($subject, $proceed,$product) { if($product->getTypeId()=="configurable"){ return $product->getFinalPrice(); } else { $returnValue = $proceed($product); return $returnValue; } } } |
Screenshot of POST MAN API Response After the above workaround
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.
Hi can you please provide the code.
Please find the source code Github link below
https://github.com/rameshpushparaj/magento2ConfigurableProductApiPrice
Thanks, it worked perfectly