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.
1 Answer 1
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
 }
}
- 
 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.FedeKrum– FedeKrum2021年04月29日 09:04:16 +00:00Commented Apr 29, 2021 at 9:04