How to Load attribute data by attribute code in Magento 2?

We can get attribute data by attribute code by just passing the attribute code in Magento 2.

We have taken entity_type_code  value as Catalog_Product  for a demo in the below code, You can get any entity_type_code as per your requirement.

<?php

public function __construct(
    \Magento\Eav\Model\Entity\Attribute $entityAttribute
) {
    $this->entityAttribute = $entityAttribute;
}

/**
* Load attribute data by code
* @return  \Magento\Eav\Model\Entity\Attribute
*/
public function getAttributeInfo($attributeCode)
{
    return $this->entityAttribute->loadByCode('catalog_product', $attributeCode);
}

Now, Call getAttributeInfo() function from template,

$attributInfo = $this->getAttributeInfo('color');
echo '<pre>';print_r($attributInfo->debug());

The result will be like the below array for color product attributes,

Array
(
    [attribute_id] => 93
    [entity_type_id] => 4
    [attribute_code] => color
    [attribute_model] => Magento\Catalog\Model\ResourceModel\Eav\Attribute
    [backend_model] => Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend
    [backend_type] => int
    [frontend_input] => select
    [frontend_label] => Color
    [is_required] => 0
    [is_user_defined] => 1
    [is_unique] => 0
    [is_global] => 1
    [is_visible] => 1
    [is_searchable] => 1
    [is_filterable] => 1
    [is_comparable] => 1
    [is_visible_on_front] => 0
    [is_html_allowed_on_front] => 0
    [is_used_for_price_rules] => 0
    [is_filterable_in_search] => 1
    [used_in_product_listing] => 0
    [used_for_sort_by] => 0
    [apply_to] => simple,virtual,configurable
    [is_visible_in_advanced_search] => 0
    [position] => 0
    [is_wysiwyg_enabled] => 0
    [is_used_for_promo_rules] => 0
    [is_required_in_admin_store] => 0
    [is_used_in_grid] => 1
    [is_visible_in_grid] => 0
    [is_filterable_in_grid] => 1
    [search_weight] => 1
    [additional_data] => {"update_product_preview_image":"0","use_product_image_for_swatch":"0","swatch_input_type":"visual"}
)

How to get backend Url in Magento 2?

It’s quite easy to fetch Base URL for an Admin panel/Backend in Magento 2 using Backend module Url Class.

Back end Url have base store Url plus admin URI name. like {BASE_URL}/admin/

You can get Magento2 backend URL by just below way,

public function __construct(
    \Magento\Backend\Model\Url $backendUrlManager
) {
    $this->backendUrlManager  = $backendUrlManager;
}

public function getBackendUrl()
{
    return $this->backendUrlManager->getUrl('sales/order/view', ['param1' => 'param1']);
}

We have passed Sales/order/view action with a query string as param1 in URL.

Get URL from an adminhtml template or PHP file,
echo $this->getBackendUrl();

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.