1

I want "add to cart" button to be visible for all visitors, but work just for customers. And when visitors (who didn't login) press "Add to Cart" button i want to show them a popup or redirect them into a cms page which is going to tell them that they need to login to use this function.

Is that possible?

1

You can add a checking to see that user is logged in or not on every Add to Cart button.

Like for listing page find this lines:

<?php if($_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button><br>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>

And replace it with:

  <?php if(Mage::getModel('customer/session')->isLoggedIn()): ?>
<?php if($_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button><br>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
<?php else: ?>
    <a href="<?php echo $this->getBaseUrl(); ?>cms_page_link" id="fake_addtocart"><?php echo $this->__('Add to Cart') ?></a>
<?php endif; ?>

<script type="text/javascript">
jQuery('#fake_addtocart').click(function(){
    alert('Please login to add item to Cart');
    return true;
});
</script>

Here I add a if condition to check that user logged in or not with Mage::getModel('customer/session')->isLoggedIn() code.

This will show user a alert to login then redirect user to cms page.

Hope this will help you.

| improve this answer | |
  • Let me know that is this worked or not – Asish Hira Apr 20 '16 at 12:09
  • Thank you for your answer but code doesn't work. Can you check please? – emrenazli Apr 20 '16 at 12:23
  • on which file you replace this code?? – Asish Hira Apr 20 '16 at 12:24
  • 1
    You can upvote the answer for all community users – Asish Hira Apr 20 '16 at 13:02
  • 1
    Anyway welcome to community! Happy learning – Asish Hira Apr 20 '16 at 13:03
1

You can check if customer is logged in or not by

Mage::getSingleton('customer/session')->isLoggedIn()

Then go to addtocart button in addtocart.phtml and in onclick check if user is log in or not and depending on that you can redirect to the page.

onclick="<?php  if(Mage::getSingleton('customer/session')->isLoggedIn()) {?>location.href = 'http://notloggedinpage/'<?php }else {?>productAddToCartForm.submit(this)<?php }?>"

Then can have any massage on notloggedin page

| improve this answer | |

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.