Post by crawford » Fri May 07, 2010 4:52 pm

Hi all,

I'm fighting with OC and I learned a lot - but it's a lot of new stuff for a non-programmer. So I hope you guys can help me once more (and yes, I searched and found a lot of posts, but without a solution):

It's easy to hide the "add to cart" part in product.tpl in general. It would be easy to put this in an if-statement - if i would know how and where to ask for the category_id for a given product_id. It must be something like

$this->db->query("SELECT category_id FROM" . DB_PREFIX . ..........

in model/catalog/product.php, right? How to create the right select statement and define a variable with the answer, so that I can use it as a condition in products.tpl

Thank you for hints,

Michael

New member

Posts

Joined
Mon Dec 28, 2009 8:10 pm

Post by fido-x » Fri May 07, 2010 8:57 pm

Create a new function for the purpose.

Add the following function to "catalog/model/catalog/product.php":-

Code: Select all

public function getProductCategory($product_id) {
   $query = $this->db->query("SELECT DISTINCT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
   return $query->row['category_id'];
}
Then, in "catalog/controller/product.php", somewhere around line 165, insert the following:-

Code: Select all

$this->data['product_category'] = $this->model_catalog_product->getProductCategory($this->request->get['product_id']);
Then you can use a conditional in "catalog/view/theme/YOUR_TEMPLATE/product/product.tpl" (lines 93 to 97), eg.:

Code: Select all

<?php if ($product_category != '999') { ?>
<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; ?>" />
  <input type="hidden" name="redirect" value="<?php echo str_replace('&', '&', $redirect); ?>" /></div>
<?php } ?>

Image
Modules for OpenCart 2.3.0.2
Homepage Module [Free - since OpenCart 0.7.7]
Multistore Extensions
Store Manager Multi-Vendor/Multi-Store management tool

If you're not living on the edge ... you're taking up too much space!


User avatar
Expert Member

Posts

Joined
Sat Jun 28, 2008 1:09 am
Location - Tasmania, Australia

Post by crawford » Fri May 07, 2010 11:22 pm

Hi fido-x,

I asked for hints and you served the complete code, wow...

Thank you very much!

With your solution I also understand the MCV-model much better.

Thank you again for now,

Michael

New member

Posts

Joined
Mon Dec 28, 2009 8:10 pm

Post by GL@collectiveus.com » Fri Jan 18, 2019 12:09 pm

Hi, I'm trying to do the exact same thing, but in Open cart 3.0.2.0, I've tried following these instructions but I've a feeling I'm missing something due to the changes in OC 3x

Any chance someone could provide the same code as above but for version 3.0.2.0

Cheers


Posts

Joined
Wed Oct 31, 2018 8:00 am

Post by kestas » Fri Jan 18, 2019 4:16 pm

if you need hide "add to cart" button in product page in OC3.0.2 you can try this...
In catalog/controller/product/product.php finde:

Code: Select all

$this->load->model('catalog/product');   
(line approx. 157) after add:

Code: Select all

$data['product_category']  =  $this->model_catalog_product->getProductCategory($product_id);
in In catalog/model/product/product.php add new function:

Code: Select all

public function getProductCategory($product_id) {
   $query  =  $this->db->query("SELECT DISTINCT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
   return $query->row['category_id'];
}
in catalog/view/theme/YOUR-THEME/template/product/product.twig find:

Code: Select all

		  <button type="button" id="button-cart" data-loading-text="{{ text_loading }}" class="btn btn-primary btn-lg btn-block">{{ button_cart }}</button>
replace with:

Code: Select all

{%  if (product_category  != ' 999' )  %} 
		  <button type="button" id="button-cart" data-loading-text="{{ text_loading }}" class="btn btn-primary btn-lg btn-block">{{ button_cart }}</button>
{% endif %}
this should hide add to cart button for products in category_id=999
My suggestion to make this using OCMOD
Cheers

Custom OpenCart modules and solutions. You can write PM with additional questions... Extensions you can find here


Active Member

Posts

Joined
Tue Oct 12, 2010 2:23 am
Who is online

Users browsing this forum: No registered users and 18 guests