In Magento, is it possible to add the product to cart after login for the selected category.

The selected category products, which can be view for all user while add this product to cart it should check whether the user is login, if the user is login product should be added to cart else it should go to my account page after user login that product should added to cart and the user also redirect to the cart page.

Any Ideas? Thanks in advance.

1 Answer 1


You can accomplish this with an extension that uses an observer. You can observer the controller_action_predispatch_checkout_cart_add event in this case.


and your observer would look something like this. Before the product is added to cart the function checks if the current customer is logged in. If not it redirects to the login page.

class Namespace_Module_Model_Observer
   public function checkLogin($observer)
      if (!Mage::helper('customer')->isLoggedIn())

[EDIT] - another, little les techincal, option.

An other option would be to hide the add to cart button whenever a customer is not logged in yet. In the file app/design/frontend/[template]/[package]/template/catalog/product/view/addtocart.phtml you can wrap the add to cart in an if statement. Your file would look something like this.

$_product = $this->getProduct();
$requirelogincategory = [ID of the category that requires login];
$buttonTitle = $this->__('Add to Cart');

if(Mage::helper('customer')->isLoggedIn() || Mage::registry('current_category')!=$requirelogincategory): 
    <div class="add-to-cart">
        <?php if(!$_product->isGrouped()): ?>
        <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" />
        <?php endif; ?>
        <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php echo $this->getChildHtml('', true, true) ?>
    <p><?php echo $this->__('Please <a href="%s">login</a> to buy this product', Mage::getUrl('customer/account'));?></p>
  • After changing 'Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account'));' to 'echo Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account')); exit();' it redirect, but after login it not goes to cart page, and i want to set it for selected category only
    – Helphin
    Feb 18, 2014 at 6:50
  • I've updated my answer with another option that might be more suited to what you require Feb 18, 2014 at 8:57
  • sander, for selected categories this works well,now my problem is after the user login it not goes to cart page. eg. if the user not login and click addtocart it goes to customer account page, after login it should redirect to cart page with adding that product
    – Helphin
    Feb 18, 2014 at 9:26
  • That's strange. it should redirect to the last page. If not you might have to edit the customer controller that handles the login request Feb 18, 2014 at 9:29
  • i have tried but the product is not added to the cart, just it redirect to cart page
    – Helphin
    Feb 18, 2014 at 9:34

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.