Page 1 of 1

Hide add to cart if price is negative?

Posted: Sun Apr 04, 2010 5:45 pm
by pvtsales
How to hide Add to cart if price is negative and show cart if price is >= 0.
If it's equal to 0 must show add to cart.

I've tried this in product.tpl:

<?php if ($price < 0 ) { echo "See related products"; } else { ?>
<div class="content"><?php echo $text_qty; ?>
<input type="text" name="quantity" size="3" value="1" />
<a onclick="$('#product').submit();" id="add_to_cart" class="button"><span><?php echo $button_add_to_cart; ?></span></a></div>
<div><input type="hidden" name="product_id" value="<?php echo $product_id; ?>" />
<?php }?>

But don't work. Always show add to cart

If use

<?php if ($price == 0 ) { echo "See related products"; } else { ?>

I get See related products for negative price too.

Seems that negative price becomes equal to zero.

Version: 1.4.6

Re: Hide add to cart if price is negative?

Posted: Sun Apr 04, 2010 11:35 pm
by rph
The problem is $price is a string, not a float number, because it's been formatted with the currency symbol. You can do something quick and dirty like:

Code: Select all

<?php if ((float)str_replace('$', '', $price) < 0) { echo "See related products"; } else { ?>

Re: Hide add to cart if price is negative?

Posted: Mon Apr 05, 2010 12:29 am
by pvtsales
Don't work. I'm using Euro maybe a problem?

Re: Hide add to cart if price is negative?

Posted: Mon Apr 05, 2010 12:30 am
by rph
Well yeah. Just change the dollar sign to a euro.

Re: Hide add to cart if price is negative?

Posted: Mon Apr 05, 2010 2:31 am
by pvtsales
Tried this:

<?php if ((float)str_replace('€', '', $price) < 0) { echo "See related products"; } else { ?>

But don't work.

Price is shown like this: 13,26€

€ is after price. May be a problem?

Re: Hide add to cart if price is negative?

Posted: Mon Apr 05, 2010 2:34 am
by pvtsales
Don't work either I put € on left or right in admin

Re: Hide add to cart if price is negative?

Posted: Mon Apr 05, 2010 3:57 am
by rph
It's because you're using a comma decimal separator.

Re: Hide add to cart if price is negative?

Posted: Wed Aug 10, 2011 10:36 pm
by dirtyharry
Any changes made in view/theme/default/template/product.tpl
are not recommended!

Because any other existing and up coming template would be affected by this modification!!!

So here is my idea of how this bug could be fixed, without changing the template.

May there could be some further adjustments. If so, feel free to let me know.

add to upload/catalog/controller/product/product.php

if ($product_info['quantity'] > 0) {
$this->data['button_cart'] = $this->language->get('button_cart');
} else {
$this->data['text_qty'] = $this->language->get('');
$this->data['minimum'] = $this->language->get('');
$this->data['button_cart'] = $this->language->get('1');
$this->data['text_or'] = $this->language->get('');
$this->data['button_wishlist'] = $this->language->get('');
$this->data['button_compare'] = $this->language->get('');

}

Re: Hide add to cart if price is negative?

Posted: Thu Nov 24, 2011 8:16 am
by GoGo OpenCart
I just released an extension for doing that. This extension will display "Call for Price" or any other text that you like, for products that have 0 (zero) price, but I can modify it to work with negative prices, if you want.

It works independent from different currencies, multiple currencies, left or right currencies, etc. With other words, all you need to do, is to install the extension, and put 0 price, or any negative price, as I mentioned before to make it to work like that, for the products you don't want to have prices.

It will also disable the "Add to Cart" button for 0 priced products, or any negative price, same as before, making it gray, so costumers can't add that product into the cart.

Look at the pictures to see how it looks like.

Extension's page: http://www.opencart.com/index.php?route ... on_id=4067