Magento – Magento Product Add to Cart after Login for Selected Category

cartcategory

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.

Best Answer

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; 
?>