0

Within a product list, I would like to access

$product->getVisibility()

for each product of the collection. However, this is always null instead of either of the 4 states. I have already added

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
    <group name="catalog_product">
        <attribute name="visibility" />
    </group>
</config>

to the app/code/<vendor>/<module>/etc/catalog_attributes.xml. While this works fine for other custom properties of the products, it does not seem to have any affect on the visibility property - and I have no idea why.

Magento 2.3.2

  • try this $product->getAttributeText('visibility'); – Sangeeta Chandaliya Nov 1 '19 at 9:49
  • This is also null. – fritzmg Nov 1 '19 at 9:54
  • how to get product collection by object manager or custom module..? – Sangeeta Chandaliya Nov 1 '19 at 10:19
  • if you are using custom module then you should need to use like this in __construct \Magento\Catalog\Model\Product\Visibility $productVisibility, $this->productVisibility = $productVisibility; then fetch like this $this->productVisibility->getVisibleInSiteIds() – Sangeeta Chandaliya Nov 1 '19 at 10:24
  • It is the regular product list from magento/module-catalog, no custom module. – fritzmg Nov 1 '19 at 10:41

Your Answer

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

Browse other questions tagged or ask your own question.