3

I have tried to set quantity_and_stock_status attribute to make filterable on the product list page. I have set it from the admin panel(Stores -> Attributes -> Product) but I can not see a stock filter on the front end. What could be the issue? Please provide a solution. Thank you

2

quantity_and_stock_status

is the attribute code name is

Quantity

When changing the settings I have found a bug,it didn't save and showed the loading image,

It was a magento 2 bug same as https://github.com/magento/magento2/issues/8202 ,

for a quick solution you can do is inspect the options and remove the disabled attribute and then save.

It worked for me.

2
  • I have upgraded magento to latest version 2.2.4 and issue fixed. – Ankit8 Jun 2 '18 at 5:41
  • yes, latest versions fixed the lot of bugs – Lasantha Jun 2 '18 at 12:10
0

This worked for me:

As I have a specific function to update inventory that extends \Magento\CatalogInventory\Model\StockRegistry, I called inside it, but if you use Magento itself you should create a plugin to be called after you have run the function Magento\CatalogInventory\Model\StockRegistry::updateStockItemBySku(). At each inventory update, this function updates the is_in_stock status of the parent product by adding the items of the child products:

`

public function syncProductStock(int $productId)
{
    $product = $this->productFactory->create();
    $product->load($productId);

    $productParent= $this->productFactory->create();
    $productParent->load(
        $this->objectManager->get(\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable::class)
            ->getParentIdsByChild($productId)
    );

    $childrenIds = array_values(
        $this->objectManager->get(\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable::class)
            ->getChildrenIds($productParent->getId())[0]
    );
    $sumQuantity = 0;
    foreach ($childrenIds as $childrenId) {
        $productChildren = $this->productFactory->create();
        $productChildren->load($childrenId);
        $sumQuantity += $this->objectManager->get(\Magento\CatalogInventory\Model\StockRegistry::class)
            ->getStockItem($childrenId)->getQty();
    }

    $isInStock = $sumQuantity > 0 ? 1 : 0;
    $product->addAttributeUpdate('quantity_and_stock_status', $this->getStockItem($productId)->getIsInStock(), $product->getStoreId());
    $productParent->addAttributeUpdate('quantity_and_stock_status', $isInStock, $product->getStoreId());

    $parentStockItem = $this->getStockItem($productParent->getId());
    $parentStockItem->setIsInStock($isInStock);
    $this->stockItemRepository->save($parentStockItem);
}`

Hope this helps! Thank you!

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.