How can I get the products' visibility on a loaded product?
<?php
$Product = Mage::getModel('catalog/product');
$Product->load($_item->getId());
var_dump($product_visibility = $Product->getData('visibility'));
?>
I've tried this as well:
var_dump($product_visibility = $Product->getVisibility());
But always just returns NULL
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 which visibility setting each integer translates to can 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’re having trouble, I would suggest checking your call to $_item->getId()
to make sure it’s returning a valid product ID. I can’t tell from your post what sort of object $_item
is, but I seem to recall that there’s a difference between Items and Products. Maybe try one of these:
$_item->getProductId();
$_item->getProduct()->getId();
if you want the visibility attribute in a product collection you should do a join
looking at magento product grind code you can find
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner', $store->getId());
so in your 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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With