Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/cartforge.co/app/code/CartForge/StockQty/Controller/Index/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //home/corals/cartforge.co/app/code/CartForge/StockQty/Controller/Index/Index.php
<?php
namespace CartForge\StockQty\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\CatalogInventory\Api\StockStateInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Controller\Result\JsonFactory;

class Index extends Action
{
    /**
     * @var StockStateInterface
     */
    protected $stockState;

    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;

    /**
     * @var JsonFactory
     */
    protected $jsonFactory;

    /**
     * Constructor
     *
     * @param Context $context
     * @param StockStateInterface $stockState
     * @param ProductRepositoryInterface $productRepository
     * @param JsonFactory $jsonFactory
     */
    public function __construct(
        Context $context,
        StockStateInterface $stockState,
        ProductRepositoryInterface $productRepository,
        JsonFactory $jsonFactory
    ) {
        parent::__construct($context);
        $this->stockState = $stockState;
        $this->productRepository = $productRepository;
        $this->jsonFactory = $jsonFactory;
    }

    /**
     * Execute action based on request and return result
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $html = '';
        $sku = '';
        $productId = $this->getRequest()->getParam('product_id');

        if ($productId) {
            try {
                // Get stock quantity
                $stockQty = $this->stockState->getStockQty($productId);
                $html = __("Available Qty: ") . $stockQty;

                // Get product SKU
                $product = $this->productRepository->getById($productId);
                $sku = $product->getSku();
            } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
                $html = __("Product not found.");
            } catch (\Exception $e) {
                $html = __("An error occurred while processing your request.");
            }
        }

        /** @var \Magento\Framework\Controller\Result\Json $result */
        $result = $this->jsonFactory->create();
        return $result->setData(['content' => $html, 'sku' => $sku]);
    }
}

Spamworldpro Mini