0

I have a custom attribute that is a drop-down with the brands of my products.

If I query

$query = 'https://example.com/rest/V1/products?'.
"fields=items[id,sku,name,custom_attributes[Brand]]".
'&searchCriteria[pageSize]=1';

I get

{
 "items": [
 {
 "id": 577,
 "sku": "TAC_520UT",
 "name": "Tacometro 520mm Ultra Tunne",
 "custom_attributes": {
 "1": {
 "attribute_code": "Brand",
 "value": "415"
 }
 }
 }
 ]
}

The brand is giving me 415, while at the interface that is not a number, is the text "Kamoha"

How can I query for the text of Brand 415 and get "Kamoha" as a Rest API call?

Thanks in advance.

asked Apr 29, 2021 at 3:20

1 Answer 1

0

As I can see in the request you have a product Id, so you simply load the product with product id and use function getAttributeText to get product brand value

Sample Code:

<?php
namespace Mageprince\Test\Block;
use Magento\Catalog\Model\ProductRepository;
class MyClass
{
 private $orderRepository;
 
 public function __construct(
 \Magento\Catalog\Model\ProductRepository $productRepository
 ) {
 $this->productRepository = $productRepository;
 }
 public function getAttributeValue()
 {
 $productId = 577;
 $product = $this->productRepository->get($productId);
 
 echo $product->getAttributeText('Brand'); //Brand is attribute code
 }
}
answered Apr 29, 2021 at 4:53
1
  • Thanks , but as you can see I am asking to do this from the Rest API and not calling the Magento classes directly in a PHP script. Is not useful for me in this way. If you can translate this into a Rest API query like the one I have posted it may help. Commented Apr 29, 2021 at 9:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.