1

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.

2

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.

<config>
    [...]
    <frontend>
        <events>
            <controller_action_predispatch_checkout_cart_add>
                <observers>
                    <namespace_module_add_product>
                        <class>namespace_module/observer</class>
                        <method>checkLogin</method>
                    </namespace_module_add_product>
                </observers>
            </controller_action_predispatch_checkout_cart_add>
        </events>
    </frontend>
    [...]
</config>

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())
      {
         Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account'));
         exit;
      }
   }
}

[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.

<?php
$_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): 
    if($_product->isSaleable()): 
?>
    <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) ?>
    </div>
<?php 
    endif;
else:
?>
    <p><?php echo $this->__('Please <a href="%s">login</a> to buy this product', Mage::getUrl('customer/account'));?></p>
<?php
endif; 
?>
  • 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 '14 at 6:50
  • I've updated my answer with another option that might be more suited to what you require – Sander Mangel Feb 18 '14 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 '14 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 – Sander Mangel Feb 18 '14 at 9:29
  • i have tried but the product is not added to the cart, just it redirect to cart page – Helphin Feb 18 '14 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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