I have a message "Call for Best Price" on my Magento store if a product has the attribute priceless set to yes.

Currently this is applied to all products as yes. I'd like for it to show only on products in a specified category (e.g. Shoes [ID:10]).

My current code in catalog/product/view.phtml is as follows

<?php if($_product->getAttributeText('priceless') == $this->__('Yes')): ?>
 <p>Please call for best price</p>
<?php endif; // PriceLess Product ?>

Can anyone suggest an edit to get this working for only the specified category (ID: 10)?

4 Answers 4


it is too easy you need to get categories ids from product and Current category id then check your category is exit in this list

/* get product categories ids */
$CategoryIds = array();
$CategoryIds =$_product->getCategoryIds();
    $CategoryIds[] = Mage::registry('current_category')->getId();
$checkCategory = 10;
if($_product->getAttributeText('priceless') == $this->__('Yes') 
    && in_array($checkCategory , $CategoryIds )
): ?>
<p>Please call for best price</p>
<?php endif;?>
  • And if there were multiple values Amit, presumably '= array (10,11,12);' minus the quotes. Dec 22, 2016 at 12:51
  • it so simple, just use a loop
    – Amit Bera
    Dec 22, 2016 at 12:59

Magento product can be assigned to multiple categories. So you need to get all categories id and check.

    $productObj = Mage::getModel('catalog/category')->load($_product->getId());
    $categoryIds = $productObj->getCategoryIds();
    if(in_array(10, $categoryIds)) {
        // your code
$ids = $_product->getCategoryId();
$configId = 10; //You can make it dynamic by set it in Custom Configuration.

<?php if($_product->getAttributeText('priceless') == $this->__('Yes')): 
 if (in_array($configId , $ids )) { ?>

 <p>Please call for best price</p>

 <?php }
 endif; // PriceLess Product 

If the category is an anchor category, it is not enough to check if the id (10) is assigned to the product, you also have to check if a child category is assigned to the product.

But we do not need to do this manually, we can use the product collection which will determine which products belong to the category based on anchor settings:

$shoeCategory = Mage::getModel('catalog/category')->load(10);

$productCollection = $shoeCategory->getProductCollection()

if ($productCollection->getSize() === 1) {
    ?> Please call for best price <?php

By using getSize() the collection is not loaded but only a single SELECT COUNT query is executed.

On an unrelated note: the following line is unnecessarily complicated and error-prone:

<?php if($_product->getAttributeText('priceless') == $this->__('Yes')): ?>

Compare the internal value instead:

<?php if($_product->getData('priceless')): ?>

I assume that "priceless" has the source model eav/entity_attribute_source_boolean, which has the values 0 and 1 for "yes" and "no". If not, I recommend to change it, it's not too late as long as all values are "yes".

  • Priceless is 'yes' or 'no' text based on dropdown options chosen by the user. Presume getData would not work in this case? Dec 22, 2016 at 13:27
  • It does. Look how the value is stored internally Dec 22, 2016 at 13:35
  • I see - not all values in the category will be 'yes'. Dec 22, 2016 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.