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 override Customer form/login.phtml file to custom module in magento 2?

We can override form/login.phtml file by just copy login.phtml file to our theme level easily without any code customization. login.phtml file contains the code of login form of site,They contain email and password field.

For custom module requirement, we need to overwrite login.phtml file in module level so we need to create customer_account_login.xml file.
Create XML file at app/code/Rbj/Customer/view/frontend/layout/customer_account_login.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_form_login">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Rbj_Customer::form/login.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

Now keep our login.phtml file at app/code/Rbj/Customer/view/frontend/templates/form/login.phtml
Keep content from core login.phtml file to our custom file.
Run command,
php bin/magento cache:clean

Open link, {YOUR_SITE_URL}/customer/account/login
You can get your override file changes.

How to override cart/item/default.phtml in Magento 2?

For override default.phtml file in Magento 2, We need to create the plugin for it. We need to override Magento\Checkout\Block\Cart\AbstractCart Block file to override default.phtml in the module.
create the file at the location in your module.

I have created a module with Rbj/CartItem as {Namespace/Modulename},
app/code/Rbj/CartItem/etc/di.xml file,

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- override cart/item/default.phtml file -->
    <type name="Magento\Checkout\Block\Cart\AbstractCart">
        <plugin name="item-test" type="Rbj\CartItem\Plugin\Cart\AbstractCart" sortOrder="1"/>
    </type>
</config>

Create plugin file at below location in your module,
app/code/Rbj/CartItem/Plugin/Cart/AbstractCart.php

<?php
namespace Rbj\CartItem\Plugin\Cart;
class AbstractCart
{
    /*
    *   Override cart/item/default.phtml file
    *   \Magento\Checkout\Block\Cart\AbstractCart $subject
    *   $result
    */
    public function afterGetItemRenderer(\Magento\Checkout\Block\Cart\AbstractCart $subject, $result)
    {
        $result->setTemplate('Rbj_CartItem::cart/item/default.phtml');
        return $result;
    }
}

Create template file in your module view folder,
app/code/Rbj/CartItem/view/frontend/templates/cart/item/default.phtml
Keep your content in default.phtml file to override Cart module default template.