Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Get Product Visibility

Tags:

magento

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

like image 449
iamjonesy Avatar asked Oct 06 '11 11:10

iamjonesy


2 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 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();
like image 64
losttime Avatar answered Oct 31 '22 16:10

losttime


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();
    }
like image 27
WonderLand Avatar answered Oct 31 '22 15:10

WonderLand