iamjonesy
iamjonesy

Reputation: 25132

Magento: Get Product Visibility

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

Upvotes: 8

Views: 25821

Answers (5)

Nick
Nick

Reputation: 602

Try this one

 $product->isVisibleInCatalog() && $product->isVisibleInSiteVisibility()

Upvotes: 2

WonderLand
WonderLand

Reputation: 5674

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();
    }

Upvotes: 7

Dane Lowe
Dane Lowe

Reputation: 298

Were you by chance working with a product pulled from a collection? Typical gotcha with Magento in that you have to specifically add the fields to select before loading the collection, otherwise the attribute will return null with no error.

Upvotes: 3

losttime
losttime

Reputation: 1141

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();

Upvotes: 10

Roman Snitko
Roman Snitko

Reputation: 3655

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

Upvotes: 0

Related Questions