Skip to content

Magento 2 Product recommendations: How to hide add to cart button for out of stock products

I want to remove the “Add to cart” button for the out-of-stock products in the product recommendations slider.

I have changed the attribute quantity_and_stock_status configuration “Used in Product Listing” to “Yes”. But I can’t get it in the product recommendations response.

{
    "unitId": "unit_id_value",
    "unitName": "Test promotion",
    "unitType": "primary",
    "searchTime": 1,
    "totalProducts": 10,
    "primaryProducts": 10,
    "backupProducts": 0,
    "products": [
        {
            ...
            "sku": "test001",
            "name": "Test",
            ...
            "categories": [
                ...
            ],
            ...
            "addToCartAllowed": true,
            "attributes": {
                "test_attribute": [
                    "false"
                ]
            }
        }
        ...
    ],
    "pageType": "PageBuilder",
    "typeId": "just-for-you",
    "storefrontLabel": "",
    "pagePlacement": "",
    "displayNumber": 10,
    "displayOrder": 0
}

In the attributes, I should receive the attribute quantity_and_stock_status.

Please share your answer if anyone has any other solution to hide the add-to-cart button.