如何在Magento 2的list.phtml中获取每种产品的库存数量?
如何在Magento 2的list.phtml中获取每种产品的库存数量?
Answers:
在list.phtml文件中添加以下代码
<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
    echo $StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
?>要么
<?php
   $stockItem = $product->getExtensionAttributes()->getStockItem();
   print_r($stockItem->getQty()); 
?>就像提到的一些评论一样,您想使用依赖注入。不要使用对象管理器;换句话说,不要执行任何其他响应状态。以下技术可以在任何地方应用。对于“块”,将类设置为布局XML中的类,这样可以扩展原始类并注入正确的信息。
将StockRegistryInterface接口插入需要访问的位置:
/**
 * @var \Magento\CatalogInventory\Api\StockRegistryInterface
 */
private $stockRegistry;
/**
 * Constructor for DI.
 *
 * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
 */
public function __construct(
    \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
) {
    $this->stockRegistry = $stockRegistry;
}
/**
 * Get the product stock data and methods.
 *
 * @return \Magento\CatalogInventory\Api\StockRegistryInterface
 */
public function getStockRegistry()
{
    return $this->stockRegistry;
}在某处使用它:
/** @var \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry */
$stockRegistry = [$this|$block]->getStockRegistry();
/** @var \Magento\Catalog\Model\Product $product */
$product = [Grab Product instance however you want. This is up to you.]
// Get stock data for given product.
$productStock = $stockRegistry->getStockItem($product->getId());
// Get quantity of product.
$productQty = $productStock->getQty();作为参考,Magento2在检索产品库存信息时会在整个目录中使用此确切接口。
请注意,方括号内的所有内容都需要修改。
如何在Magento 2中获取每种产品的库存数量
用于控制器或块注入\ Magento \ CatalogInventory \ Api \ StockStateInterface
 public function __construct(
    \Magento\CatalogInventory\Api\StockStateInterface $stockItem
   )
  {
    $this->stockItem = $stockItem;
  }然后使用getStockQty函数获取数量
 $this->stockItem->getStockQty($product->getId(), $product->getStore()->getWebsiteId());如果您想获取.phtml文件中的数量,请使用
 <?php 
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
 echo $StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
?>如果您想$productobj从后端保存产品之后,那么可以轻松地使用catalog_product_save_after事件。
我假设您已经知道如何在中创建模块M2。
现在,您需要为M2开发新模块
然后events.xml在下面的路径中创建此文件
app\code\YOUR_NAMESPACE\YOURMODULE\etc\adminhtml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="test_name" instance="YOUR_NAMESPACE\YOUR_MODULENAME\Observer\Productsaveafter" />
    </event>
</config>并Productsaveafter.php在下面的路径中创建观察者文件
app \ code \ YOUR_NAMESPACE \ YOURMODULE \ Observer \
<?php
namespace YOURNAMESPACE\YOURMODULENAME\Observer;
use Magento\Framework\Event\ObserverInterface;
class Productsaveafter implements ObserverInterface
{    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
        $id = $product->getId(); //Get Product Id
        //Get Quantity
        $stockItem = $product->getExtensionAttributes()->getStockItem();
        $stockData = $stockItem->getQty();
        // Get new Qty
        $_vendor_qty = $product->getVendorQty();
        $_on_hand_qty = $product->getOnHandQty();
        $totalQty = $_vendor_qty+$_on_hand_qty; //Add New Qty
        $stockItem->setQty($totalQty); //Set New Qty to Main Qty
        $stockItem->save();
    }   
}