开发者

Magento: Get Product Visibility

开发者 https://www.devze.com 2023-04-11 08:29 出处:网络
How can I get the products\' visibility on a loaded product? <?php $Product = Mage::getModel(\'catalog/product\');

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->get开发者_如何学运维Data('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();
    }


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.


Try this one

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号