2

quantity attribute

I'm using magento 2. I want to get this attribute in products list page. Does anyone show me how ? Thanks !

2

You need define new observer

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_collection_load_after">
        <observer name="Your_Module_Add_Quantity" instance="Your\Module\Observer\AddQuantityToCollectionObserver"/>
    </event>
</config>

With code like

namespace Your\Module\Observer;
class AddQuantityToCollectionObserver implements ObserverInterface
{
    /**
     * Add information about product Quantity to collection
     * Used for product collection after load
     *
     * @param EventObserver $observer
     * @return void
     */
    public function execute(EventObserver $observer)
    {
        $productCollection = $observer->getEvent()->getCollection();
        $websiteId = $this->storeManager->getStore($productCollection->getStoreId())->getWebsiteId();
        foreach ($productCollection as $product) {
            $productId = $product->getId();
            $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $websiteId);
            $product->setQty($stockStatus->getQty());
        }
    }
}
0

try this

   <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
        echo $StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
?>
  • Please consider adding more info on your answer. – Adarsh Khatri Jan 20 '16 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.