Get product attribute’s option id from label and get option label from id in magento 2.

In Magento 2 We can get product Attributes Option Id from the Label and Option label by Option id with Product  Factory object.

Occasionally when you work with the Configurable Product type you may face situations to get Option Id or Label from the Attribute code.

Lets We have taken a Color attribute to demonstrate the usage, Find a Color Label value based on Color Option id.

<?php
namespace Rbj\ProductAttribute\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Catalog\Model\ProductFactory $productFactory
    ) {
        $this->productFactory = $productFactory;
        parent::__construct($context);
    }

    /* Get Label by option id */
    public function getOptionLabelByValue($attributeCode,$optionId)
    {
        $product = $this->productFactory->create();
        $isAttributeExist = $product->getResource()->getAttribute($attributeCode); 
        $optionText = '';
        if ($isAttributeExist && $isAttributeExist->usesSource()) {
            $optionText = $isAttributeExist->getSource()->getOptionText($optionId);
        }
        return $optionText;
    }

   /* Get Option id by Option Label */
    public function getOptionIdByLabel($attributeCode,$optionLabel)
    {
        $product = $this->productFactory->create();
        $isAttributeExist = $product->getResource()->getAttribute($attributeCode);
        $optionId = '';
        if ($isAttributeExist && $isAttributeExist->usesSource()) {
            $optionId = $isAttributeExist->getSource()->getOptionId($optionLabel);
        }
        return $optionId;
    }

Call Method at the specific file,

$optionValue = $this->getOptionLabelByValue('color','50'); // result is blue
$optionId = $this->getOptionIdByLabel('color','Blue'); // result is 50

Assume that, We have Option id 50 and its value is Blue from Attribute code Color.

getOptionIdByLabel() and getOptionLabelByValue() method will be use to get Option Id By Label value and Option Label by Option Id  respectively.

You need to pass only the correct Product Attribute code you want to fetch the value. The given example is working fine for all the custom product attributes in Magento.

How to get product available qty in Magento 2?

Get Product qty or stock information by Product Object is pretty easy in Magento 2.

We can simply get product qty from the product id and get the first product object. We can get product data from either by id or by SKU.

Please check below code snippet for getting product qty in Magento 2,

<?php
namespace Rbj\Training\Block;

class Product extends \Magento\Framework\View\Element\Template
{
    protected $stockItemRepository;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository,
        array $data = []
    ) {
        $this->stockItemRepository = $stockItemRepository;
        parent::__construct($context, $data);
    }

    public function getProductQty($id)
    {
        return $this->stockItemRepository->get($id);
    }
}

Call template file by below code,

$product_id = 1; //product id
$stock = $block->getProductQty($product_id);
echo $stock->getQty(); // product qty
echo $stock->getMinQty();
echo $stock->getMaxQty();
echo $stock->getIsInStock(); // is in stock return as boolean

You can fetch the Product Stock related data using the above Stock object.

How to get product image url in Magento 2?

In the e-Commerce Site, Each Product contains unique images like Small Images, Base images, and thumbnail images.

Many times in custom development we need to required get the product URL in Magento 2. If you are developing some features related to images and want to retrieve the image URL, You can achieve the image full URL by referring to this article.

If you are dealing with PWA stuff and want to fetch the product image URL by GraphQL, Get Product Image URLs with GraphQl Continue reading “How to get product image url in Magento 2?”