2

Why Price is zero in response.

I am hitting Url:

https://example.com/index.php/rest/V1/products/sku

Response:

{
    "id": 176,
    "sku": "8544501",
    "name": "BATA-8544501",
    "attribute_set_id": 4,
    "price": 0,
    "status": 1,
    "visibility": 4,
    "type_id": "configurable",
    "created_at": "2018-01-12 07:25:07",
    "updated_at": "2018-01-12 07:25:07",
    "weight": 0.5,
    "extension_attributes": {
        "configurable_product_options": [
            {
                "id": 73,
                "attribute_id": "93",
                "label": "Color",
                "position": 0,
                "values": [
                    {
                        "value_index": 10
                    }
                ],
                "product_id": 176
            },
            {
                "id": 74,
                "attribute_id": "135",
                "label": "Size",
                "position": 1,
                "values": [
                    {
                        "value_index": 4
                    },
                    {
                        "value_index": 5
                    },
                    {
                        "value_index": 6
                    },
                    {
                        "value_index": 7
                    },
                    {
                        "value_index": 8
                    }
                ],
                "product_id": 176
            }
        ],
        "configurable_product_links": [
            171,
            172,
            173,
            174,
            175
        ]
    },
    "product_links": [],
    "options": [],
    "media_gallery_entries": [
        {
            "id": 285,
            "media_type": "image",
            "label": null,
            "position": 1,
            "disabled": false,
            "types": [
                "image",
                "small_image",
                "thumbnail"
            ],
            "file": "/8/5/854-4501-a_1_2.jpg"
        },
        {
            "id": 286,
            "media_type": "image",
            "label": null,
            "position": 2,
            "disabled": false,
            "types": [],
            "file": "/8/5/854-4501-b_2.jpg"
        },
        {
            "id": 287,
            "media_type": "image",
            "label": null,
            "position": 3,
            "disabled": false,
            "types": [],
            "file": "/8/5/854-4501-c_2.jpg"
        },
        {
            "id": 288,
            "media_type": "image",
            "label": null,
            "position": 4,
            "disabled": false,
            "types": [],
            "file": "/8/5/854-4501-d_2.jpg"
        },
        {
            "id": 289,
            "media_type": "image",
            "label": null,
            "position": 5,
            "disabled": false,
            "types": [],
            "file": "/8/5/854-4501-e_2.jpg"
        }
    ],
    "tier_prices": [],
    "custom_attributes": [
        {
            "attribute_code": "meta_title",
            "value": "BATA-8544501"
        },
        {
            "attribute_code": "meta_keyword",
            "value": "BATA-8544501"
        },
        {
            "attribute_code": "meta_description",
            "value": " "
        },
        {
            "attribute_code": "image",
            "value": "/8/5/854-4501-a_1_2.jpg"
        },
        {
            "attribute_code": "small_image",
            "value": "/8/5/854-4501-a_1_2.jpg"
        },
        {
            "attribute_code": "thumbnail",
            "value": "/8/5/854-4501-a_1_2.jpg"
        },
        {
            "attribute_code": "category_ids",
            "value": [
                "83",
                "97",
                "121"
            ]
        },
        {
            "attribute_code": "options_container",
            "value": "container2"
        },
        {
            "attribute_code": "required_options",
            "value": "1"
        },
        {
            "attribute_code": "has_options",
            "value": "1"
        },
        {
            "attribute_code": "url_key",
            "value": "bata-8544501"
        },
        {
            "attribute_code": "tax_class_id",
            "value": "2"
        },
        {
            "attribute_code": "gift_message_available",
            "value": "2"
        },
        {
            "attribute_code": "size",
            "value": "4"
        }
    ]
}

There is zero in price, It should return base price. Is there any solution?

3

1 Answer 1

5

This is a known bug/feature...

This issue is raised in github and the bug and will not be solved as the Magento product team says that this is the expected behaviour as "by design - configurable product doesn't have a price": https://github.com/magento/magento2/issues/10644

The Magento 2 REST API returns 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.

Solution:

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

This is the core code that returns the price as 0:

//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 method needs to be Hooked using Interceptor Plugin Approach.

In the new Plugin function we need to send the final price of the configurable product.

You'll need the following XML in your module's di.xml file:

<?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="namespace_configurableapiprice_plugin_product_type_configurable_price" type="NameSpace\ConfigurableApiPrice\Plugin\Product\Type\Configurable\Price"/>
    </type>
</config>

Here is the needed plugin using the around method to hook in to the existing function, since we need the product object argument to get the final price like this:

<?php

namespace NameSpace\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;
       }      
    }
}

Hope it helps.

1
  • I am using magento rest api for my mobile application, and having same issue. How Can I solve this issue for rest api ? Aug 5, 2020 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.