Magento: Get Product Visibility

How can I get product visibility for a downloaded product?

<?php $Product = Mage::getModel('catalog/product'); $Product->load($_item->getId()); var_dump($product_visibility = $Product->getData('visibility')); ?> 

I also tried this:

 var_dump($product_visibility = $Product->getVisibility()); 

But always just returns NULL

+6
source share
5 answers

This is the code I used and it worked on Magento version 1.5.0.1:

 $pr2test = Mage::getModel('catalog/product'); $pr2test->load($product->getId()); echo 'Visibility: '.$pr2test->getVisibility(); 

The visibility value is an integer (1-4). You can find out what visibility settings for each integer can be translated by checking the constants defined in the Mage_Catalog_Model_Product_Visibility class found here: /app/code/core/Mage/Catalog/Model/Product/Visibility.php

If you are having problems, I would suggest checking your call to $_item->getId() to make sure that it returns a valid product identifier. I can’t say from your message what the $_item object is, but it seems that I remember the difference between the elements and the products. Perhaps try one of them:

 $_item->getProductId(); $_item->getProduct()->getId(); 
+10
source

if you need the visibility attribute in the product collection, you must make a connection

looking at the magento product shredding code you can find

  $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner', $store->getId()); 

so in code you can do

  $prodColl = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('name') ->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner', 1); foreach ($prodColl as $prod) { $v = $prod->getVisibility(); } 
+4
source

Have you accidentally worked with a product pulled from a collection? A typical mask with Magento, in which you need to specifically add fields for selection before loading the collection, otherwise the attribute will return null without errors.

+3
source

Try this one

  $product->isVisibleInCatalog() && $product->isVisibleInSiteVisibility() 
+1
source

You should use the Mage_Catalog_Model_Product::getStatus (there is also a useful Mage_Catalog_Model_Product::isVisibleInCatalog ).

0
source

Source: https://habr.com/ru/post/898760/


All Articles