1

I have a category of products that can't be bought online directly but that I want to give the functionality of going to a distributors page.

For that, I need to retrieve the category id of the current product and if it's equal to 13 display a different content for the add to cart button.

So far, the code that I have is:

<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Add to Cart'); ?>

<?php $categoryId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); ?>
<?php if ($categoryId == 13) {?>
    <div class="add-to-cart v-centered-content">
        <button type="button" title="<?php echo $this->__('Find your nearest retailer') ?>" id="product-addtocart-button" class="button btn-cart v-center" onclick="setLocation('<?php echo $this->getUrl('distributors') ?>')"><span><span><?php echo $this->__('Find your nearest retailer') ?></span></span></button>
    </div>
<?php }
    elseif ($_product->isSaleable()) { ?>
    <div class="add-to-cart v-centered-content">
        <button type="button" title="<?php echo $buttonTitle ?>" id="product-addtocart-button" class="button btn-cart v-center" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php if(!$_product->isGrouped()){ ?>
            <div class="qty-wrapper v-center">
                <label for="qty"><?php echo $this->__('Qty:') ?></label>
                <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
            </div>
        <?php } ?>
    </div>
    <div class="paypal-wrapper"><?php echo $this->getChildHtml('', true, true) ?></div>
<?php } ?>

Could you guys help me with getting this to work?

Thanks.

1
  • Can you explain what is the output for the code above, first ? Thanks.
    – Pilou
    Mar 17, 2015 at 15:47

1 Answer 1

1

Don't use Mage::getModel('catalog/layer')->getCurrentCategory()->getId(). This only works if you are on a certain category page, but you can view the product outside the category context.
You can get the category ids like this:

<?php $categoryIds = $_product->getCategoryIds();?>

Then check if (in_array(13, $categoryIds)) {...} and display your custom content.

But in my opinion relying on categories for product functionality is not the best approach.
I would add a new yes/no attribute on the products called from_reseller. set this attribute to be used in product listing, rebuild indexes and then just check if ($_product->getFromReseller()) {...} and display your message. It's cleaner this way.

3
  • That works right away. If I try the second suggestion with the attribute, do I have to put something like if ($_product->getFromReseller(true)) or just as you've suggested without anything?
    – VDD
    Mar 17, 2015 at 17:21
  • You don't need the extra parameter.
    – Marius
    Mar 17, 2015 at 17:43
  • what if I want to add an if statement that combines $_product->getFromReseller() and a specific customer group? I'm creating the variable out the if statement with $_isLoggedIn = $this->helper('customer')->isLoggedIn(); if($_isLoggedIn === true){ $_myGroupId = Mage::getSingleton('customer/session')->getCustomerGroupId(); } and then doing elseif ($_product->getFromReseller() && $_myGroupId == 4) but it's not working properly
    – VDD
    Mar 17, 2015 at 21:52

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.