How to get product stock qty and stock information by product id in Magento 2?

You can get Product stock related information using the Catalog Inventory module of Magento 2.

Magento\CatalogInventory\Api\StockRegistryInterface is used for getting Stock Information from the Product.

If you want to only get Product Quantity and Product status might be it’s in stock or not then refer Get Product quantity from product object

Let’ create Block file and call function from a block,

<?php
namespace Rbj\Stockinfo\Block;

class Stockinfo extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\CatalogInventory\Api\StockRegistryInterface $stockItemRepository,
        array $data = []
    )
    {
        $this->stockItemRepository = $stockItemRepository;
        parent::__construct($context, $data);
    }
    /* Using Product id */
    public function getStockItem($productId)
    {
        return $this->stockItemRepository->getStockItem($productId);
    }
    /* Using Product SKU */
    public function getStockItemBySku($productSku)
    {
        return $this->stockItemRepository->getStockItemBySku($productSku);
    }
}

Now, We can get the product stock information by id and SKU in a template file using the below way.

$id = 1; //Product id
/* by Product id */
$productStockById = $block->getStockItem($id);

/* by Product SKU */
$sku = '24-MB01';
$productStockBySku = $block->getStockItemBySku($sku);
echo "<pre>";print_r($productStockBySku->debug());

The output will be something like below,

Array
(
    [item_id] => 1
    [product_id] => 1
    [stock_id] => 1
    [qty] => 100.0000
    [min_qty] => 0.0000
    [use_config_min_qty] => 1
    [is_qty_decimal] => 0
    [backorders] => 0
    [use_config_backorders] => 1
    [min_sale_qty] => 1.0000
    [use_config_min_sale_qty] => 1
    [max_sale_qty] => 10000.0000
    [use_config_max_sale_qty] => 1
    [is_in_stock] => 1
    [notify_stock_qty] => 1.0000
    [use_config_notify_stock_qty] => 1
    [manage_stock] => 1
    [use_config_manage_stock] => 1
    [stock_status_changed_auto] => 0
    [use_config_qty_increments] => 1
    [qty_increments] => 1.0000
    [use_config_enable_qty_inc] => 1
    [enable_qty_increments] => 0
    [is_decimal_divided] => 0
    [website_id] => 0
    [type_id] => simple
)

You can get qty, min_sale_qty, is_in_stock, backorder, stock_id, website_id related data with ease.

How to remove all special characters from a string with magento 2 as a best practice?

Magento Contains Match.php file they contain CONSTANT like SPECIAL_CHARACTERS and with that constant all the special character are available.
File path is Magento\Framework\Search\Adapter\Mysql\Query\Builder\Match.php

When we need to remove or replace special character with some other character you can use Magento default constant.

Match.php file with a constant declaration like as below,
const SPECIAL_CHARACTERS = ‘-+~/\\<>\'”:*$#@()!,.?`=%&^’;
When we need to modify a string with special character to some custom value or blank you can do as below way,

<?php
 $originalString = "Test1$23?45&789^1";

 $replaceSymbols =  str_split( \Magento\Framework\Search\Adapter\Mysql\Query\Builder\Match::SPECIAL_CHARACTERS, 1);
 $string = str_replace($replaceSymbols, ' ', $originalString));
 echo $string;

The result will be, Replace all special character with one space.
“Test1 23 45 789 1”

Magento 2 How to get child item id of configurable product by super attribute details?

Let’s assume you know configurable product id and used child product super attribute details.
Example from Magento Sample data configurable product name Chaz Kangeroo Hoodie,  Configurable product id is 67.
Super attribute Array details,

[super_attribute] => Array
(
    [93] => 49
    [254] => 167
)

Using the bove details how we can get details of child item.

Refer below code snippet for Get child item id of a Configurable product by child’s Attribute details.

<?php
public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable
) {
    $this->productRepository = $productRepository;
    $this->configurable = $configurable;
}
/* Pass Configurable product id as $configId 
 * Pass array of super attribute of child item
 */
public function getChildFromProductAttribute($configId,$superAttribute) {
	$_configProduct = $this->productRepository->getById($configId);
	$usedChild = $this->configurable->getProductByAttributes($superAttribute ,$_configProduct);
	$childProductId = $usedChild->getId();
	return $childProductId;
}

Call function,

$configId = 67; //Configurable Product id
$superAttribute = Array(93 => 49,254 => 167); //childs super attribute details
$childId = $this->getChildFromProductAttribute($configId,$superAttribute);

Result is 52(Product name Chaz Kangeroo Hoodie-XS-Black). Using above way we got the child item data.