Skip to main content
added 796 characters in body
Source Link
Marius
  • 198.5k
  • 55
  • 424
  • 833

you can just inject an instance of Magento\Catalog\Model\ResourceModel\Product in your class and use that one instead of $product->getResource();

/**
 * @var \Magento\Catalog\Model\ResourceModel\Product
 */
private $productResource;

public function __construct(
    ...
    \Magento\Catalog\Model\ResourceModel\Product $productResource,
    ....
) {
    ....
    $this->productResource = $productResource;
    ....
}

And use this inside your methods

$tablecolor = $productResource->getAttribute('xyz'); 

This is how you can use the product resource model, but in your specific case, when you want to get an instance of a certain attribute you can use the product attribute repository

/**
 * @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface
 */
private $attributeRepository;

public function __construct(
    ...
    \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository,
    ....
) {
    ....
    $this->attributeRepository = $attributeRepository;
    ....
}

and later use this

try {
    $tablecolor = $attributeRepository->get('xyz'); 
    //do soemthing with tablecolor
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
    //the attribute does not exist
}

If you want to only get the label of an option of a dropdown attribute for a specific product you can just use this

$product->getAttributeText('xyz');

you can just inject an instance of Magento\Catalog\Model\ResourceModel\Product in your class and use that one instead of $product->getResource();

/**
 * @var \Magento\Catalog\Model\ResourceModel\Product
 */
private $productResource;

public function __construct(
    ...
    \Magento\Catalog\Model\ResourceModel\Product $productResource,
    ....
) {
    ....
    $this->productResource = $productResource;
    ....
}

And use this inside your methods

$tablecolor = $productResource->getAttribute('xyz'); 

you can just inject an instance of Magento\Catalog\Model\ResourceModel\Product in your class and use that one instead of $product->getResource();

/**
 * @var \Magento\Catalog\Model\ResourceModel\Product
 */
private $productResource;

public function __construct(
    ...
    \Magento\Catalog\Model\ResourceModel\Product $productResource,
    ....
) {
    ....
    $this->productResource = $productResource;
    ....
}

And use this inside your methods

$tablecolor = $productResource->getAttribute('xyz'); 

This is how you can use the product resource model, but in your specific case, when you want to get an instance of a certain attribute you can use the product attribute repository

/**
 * @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface
 */
private $attributeRepository;

public function __construct(
    ...
    \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository,
    ....
) {
    ....
    $this->attributeRepository = $attributeRepository;
    ....
}

and later use this

try {
    $tablecolor = $attributeRepository->get('xyz'); 
    //do soemthing with tablecolor
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
    //the attribute does not exist
}

If you want to only get the label of an option of a dropdown attribute for a specific product you can just use this

$product->getAttributeText('xyz');
Source Link
Marius
  • 198.5k
  • 55
  • 424
  • 833

you can just inject an instance of Magento\Catalog\Model\ResourceModel\Product in your class and use that one instead of $product->getResource();

/**
 * @var \Magento\Catalog\Model\ResourceModel\Product
 */
private $productResource;

public function __construct(
    ...
    \Magento\Catalog\Model\ResourceModel\Product $productResource,
    ....
) {
    ....
    $this->productResource = $productResource;
    ....
}

And use this inside your methods

$tablecolor = $productResource->getAttribute('xyz');