Latest Articles
So far this was never an issue, there were problems with uploading modules via the installer because of the ftp connection but you could allways install ocmods without any issue. However, some .. Read More
People had this issue so many times I thought I would write a bit about it. The  types of issues with opencart's 2.x modification system that I identified so far are: 1. Old Vqmod .. Read More
This small tutorial will show you how to manually add custom links to the default top menu in opencart. If you dont want to go trough all the hustle and would like to be able to completly customize.. Read More
To enable seo urls in opencart navigate in admin to system > settings , edit the store and on the server tab of the store settings you will find "Use SEO urls", set it to yes. So far e.. Read More
All Articles

Hide add to cart button if customer is not logged in (for openca

If you chose to only display your prices on store , only if the customer is logged in, then you might also want to hide the add to cart button, because even if the prices are not beeing displayed in the opencart product modules and in the opencart product listings, if a visitor adds a product to cart, he will be able to see the price on the shopping cart page.

So , to solve that, you just don't allow the visitor to add a product to cart unless he is logged in, and to do that, you hide the add to cart button like it follows:

1.Edit the files: featured.tpl, latest.tpl, special.tpl, bestseller.tpl located in catalog/view/theme/yourtheme/template/module and in each file , make the following modification:

move the code:

<div class="cart"><input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" /></div>

right after the code:

<span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></ <span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span>
<?php } ?>
</div>

2. Now edit the files: category.tpl, search.tpl, special.tpl and manufacturer_info.tpl located in catalog/view/theme/yourtheme/template/product and make the following modifications:

2.1: find the code:

<div class="cart">
<input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" />
</div>

and replace it all with:

<?php if ($product['price']) { ?>
<div class="cart">
<input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" />
</div>
<?php } ?>

2.2: now find the code:

html += '<div class="cart">' + $(element).find('.cart').html() + '</div>';

and replace it with:

if (price != null) {
html += '<div class="cart">' + $(element).find('.cart').html() + '</div>';
}

2.3: and last , find the code:

html += ' <div class="cart">' + $(element).find('.cart').html() + '</div>';

and replace it with:

if (price != null) {
html += '<div class="cart">' + $(element).find('.cart').html() + '</div>';
}

3. Edit the file catalog/view/theme/yourtheme/template/product/product.tpl and find the code:

<div class="cart">
<div><?php echo $text_qty; ?>
<input type="text" name="quantity" size="2" value="<?php echo $minimum; ?>" />
<input type="hidden" name="product_id" size="2" value="<?php echo $product_id; ?>" />
&nbsp;<input type="button" value="<?php echo $button_cart; ?>" id="button-cart" class="button" />
</div>
<div><span>&nbsp;&nbsp;&nbsp;<?php echo $text_or; ?>&nbsp;&nbsp;&nbsp;</span></div>
<div><a onclick="addToWishList('<?php echo $product_id; ?>');"><?php echo $button_wishlist; ?></a><br />
<a onclick="addToCompare('<?php echo $product_id; ?>');"><?php echo $button_compare; ?></a></div>
<?php if ($minimum > 1) { ?>
<div class="minimum"><?php echo $text_minimum; ?></div>
<?php } ?>
</div>

and replace it with:

<?php if ($price) { ?>
<div class="cart">
<div><?php echo $text_qty; ?>
<input type="text" name="quantity" size="2" value="<?php echo $minimum; ?>" />
<input type="hidden" name="product_id" size="2" value="<?php echo $product_id; ?>" />
&nbsp;<input type="button" value="<?php echo $button_cart; ?>" id="button-cart" class="button" />
</div>
<div><span>&nbsp;&nbsp;&nbsp;<?php echo $text_or; ?>&nbsp;&nbsp;&nbsp;</span></div>
<div><a onclick="addToWishList('<?php echo $product_id; ?>');"><?php echo $button_wishlist; ?></a><br />
<a onclick="addToCompare('<?php echo $product_id; ?>');"><?php echo $button_compare; ?></a></div>
<?php if ($minimum > 1) { ?>
<div class="minimum"><?php echo $text_minimum; ?></div>
<?php } ?>
</div> <?php } ?>
Posted by Flo

4 Comments To "Hide add to cart button if customer is not logged in (for openca"

circey On 11 Mar 2013
Hi, I'm wondering how to do the same for the "Add to cart" button in the Related Products area. Reply to this comment
Flo On 10 Sep 2012
2.2 and 2.3 are not the same. Notice there is a small difference in the code (in one of them is an extra space). That code is twice in that file (once for grid view and once for list) but there is a difference of a space inside them, that is why they are both added. I will edit the mistake on the last modification now. Reply to this comment
Richard On 10 Sep 2012
This does not work. You put twice the same modification (2.2 and 2.3) and the last replace has an unclosed <div> Reply to this comment
edna On 10 Sep 2012
Hi! Want to do this but noticed that edit code for section 2.3 is a duplicate of 2.2? Is there something else I need to edit? Thanks! Edna Reply to this comment

Write a comment

Your Name:


Enter the code in the box below:

Your Comment:
Note: HTML is not translated!