BE SMART, hand over your headache to Rootways and FOCUS ON YOUR BUSINESS.

Magento 2 get products by ID using REST API

Posted in: Magento REST API

Magento 2 get products by ID using REST API

This Blog instructs you that how to get the products by ID using REST API. In Magento 2, You can get the corresponding product details by using following commands.


$magento_url = 'YOUR_MAGENTO_BASE_URL';
$magento_token = 'TOKEN';

If you don’t know how to get token then you must see this blog post Magento 2 GET REST API


$id = '2'; //PRODUCT_ID
$ch = curl_init($magento_url . "/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][condition_type]=eq&searchCriteria[filterGroups][0][filters][0][value]=".$id);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $magento_token));

$product = curl_exec($ch);
$product = json_decode($product, 1);

print_r($product);
exit;

OutPut


Array
(
    [items] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [sku] => test123_2
                    [name] => Test Product 2
                    [attribute_set_id] => 4
                    [price] => 5
                    [status] => 1
                    [visibility] => 4
                    [type_id] => simple
                    [created_at] => 2020-06-02 12:26:54
                    [updated_at] => 2021-01-16 11:35:52
                    [weight] => 3
                    [extension_attributes] => Array
                        (
                            [website_ids] => Array
                                (
                                    [0] => 1
                                )

                            [category_links] => Array
                                (
                                    [0] => Array
                                        (
                                            [position] => 0
                                            [category_id] => 3
                                        )

                                    [1] => Array
                                        (
                                            [position] => 0
                                            [category_id] => 8
                                        )

                                )

                        )

                    [product_links] => Array
                        (
                        )

                    [options] => Array
                        (
                        )

                    [media_gallery_entries] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 29
                                    [media_type] => image
                                    [label] => 
                                    [position] => 1
                                    [disabled] => 
                                    [types] => Array
                                        (
                                            [0] => image
                                            [1] => small_image
                                            [2] => thumbnail
                                            [3] => swatch_image
                                        )

                                    [file] => /w/s/ws06-purple_main.jpg
                                )

                            [1] => Array
                                (
                                    [id] => 30
                                    [media_type] => image
                                    [label] => 
                                    [position] => 2
                                    [disabled] => 
                                    [types] => Array
                                        (
                                        )

                                    [file] => /m/h/mh03-black_main.jpg
                                )

                        )

                    [tier_prices] => Array
                        (
                        )

                    [custom_attributes] => Array
                        (
                            [0] => Array
                                (
                                    [attribute_code] => image
                                    [value] => /w/s/ws06-purple_main.jpg
                                )

                            [1] => Array
                                (
                                    [attribute_code] => url_key
                                    [value] => test-product-2
                                )

                            [2] => Array
                                (
                                    [attribute_code] => gift_message_available
                                    [value] => 2
                                )

                            [3] => Array
                                (
                                    [attribute_code] => conv_enable_subscription
                                    [value] => 0
                                )
                            .
                            .
                            .
                            .
                            .

                            [27] => Array
                                (
                                    [attribute_code] => TestAttributVish
                                    [value] => 5435
                                )

                            [28] => Array
                                (
                                    [attribute_code] => pid
                                    [value] => 1.2
                                )

                        )

                )

        )

    [search_criteria] => Array
        (
            [filter_groups] => Array
                (
                    [0] => Array
                        (
                            [filters] => Array
                                (
                                    [0] => Array
                                        (
                                            [field] => entity_id
                                            [value] => 2
                                            [condition_type] => eq
                                        )

                                )

                        )

                )

        )

    [total_count] => 1
)


You can get the Products by SKU, Click on Magento 2 get products by sku using REST API

February 20, 2021
Did you like this post?
0
0