Hide Add to Cart on Specific Categories

  • Posts: 303
  • Thank you received: 18
8 years 10 months ago #233852

-- url of the page with the problem -- : new.mojooutdoors.com/index.php/products/...mojo-scoot-and-shoot
-- HikaShop version -- : 2.6.1
-- Joomla version -- : 3.4.8

I have successfully hidden the quantity and add to cart on a particular category using:

<?php $found = true; foreach($this->categories as $c ) {if($c->category_id==166) $found = false;} if($found){ ?>

However, how can I apply this to multiple categories? I tried

category_id=='166','22'

But that did not work. Tried a few other variations, but no luck. I'm sure you guys know exactly what I'm missing.

Any help would be greatly appreciated!

Please Log in or Create an account to join the conversation.

  • Posts: 193
  • Thank you received: 76
8 years 10 months ago #233874

$hide = [166,22];  // comma separated list of category ids to hide add to cart on
if ( !in_array($c->category_id, $hide) ) {
  // add to cart button
}

Please Log in or Create an account to join the conversation.

  • Posts: 83296
  • Thank you received: 13466
  • MODERATOR
8 years 10 months ago #233870

Like that:
if(in_array($c->category_id, array(166,22))) $found = false;

Please Log in or Create an account to join the conversation.

  • Posts: 113
  • Thank you received: 18
8 years 6 months ago #247367

I realize the post is a bit aged but I'm trying to accomplish something similar, though in the product listing, not the product page.
I'm able to hide the "Add To Cart" by editing the view "add_to_cart_listing" with
foreach($this->row->categories as $c )
...
works perfectly, but need to hide the price as well.

With a similar edit to the view "listing_price", the price is hidden, but I'm getting entries in the error log:
PHP Notice: Undefined property: stdClass::$categories
using the same code.

I'm not adept enough at PHP to examine the structure to determine how to hide the price in this manner without generating the error log entries. I assumed the product's categories array would be accessible in this view the same as in the add_to_cart_listing view - apparently not. And odd that it works.

Any guidance would be appreciated.

Please Log in or Create an account to join the conversation.

  • Posts: 83296
  • Thank you received: 13466
  • MODERATOR
8 years 6 months ago #247370

Hi,

The thing is that the listing_price view file is used not only on the product page or the products listing but also in the cart. And in the cart, $this->row->categories doesn't exist.
So it's normal you get that notice logged.
You should add an if to only do your code when $this->row->categories exits.

Please Log in or Create an account to join the conversation.

  • Posts: 113
  • Thank you received: 18
8 years 6 months ago #247391

Perfect, thank you.

That explains both, why it worked (hiding the price for category product listing) , yet with errors (which I now understand came from someplace else).

If others are trying to do the same thing, the working code for the view is below.
I chose to use a single unpublished category to control the display.
It's easier to add any number of products to this category to hide the price, then to edit the view each time I need hide something else.

$displayit = true;
if(isset($this->row->categories))
foreach($this->row->categories as $c )
{
// Category 662 is product control "Hide Price"
if($c->category_id == 662)
$displayit = false;
}
if($displayit)
{ ?>

// (add a closing brace "}" at the end of the view)

Please Log in or Create an account to join the conversation.

  • Posts: 238
  • Thank you received: 29
  • Hikashop Business
2 days 9 hours ago #365749

Hi
I was able to hide the "add to cart" button on the product page based on specific category and the sub-categories of this.
So I edited the view: product / quantity.php
On line 133 is this section <!-- ADD TO CART BUTTON --> where I added my code

$class = hikashop_get('class.category');
$categories = $class->getChildren(16, true, array(), '', 0, 500); //Category: Services (id:16)
foreach($categories as $category) {
if($category->category_id == $this->row->category_id){
	$add_to_cart = false;
  }
}
This is working, but I have a small problem with the products that have variants.
When showing the first variant it's all good, but when I change the characteristic to display the price of the second variant, a new "add to cart" button show up, in a section called <!-- PRICE WITH OPTIONS -->
Where is this button, in which view? Is this in a js file?

Please Log in or Create an account to join the conversation.

  • Posts: 83296
  • Thank you received: 13466
  • MODERATOR
2 days 3 hours ago #365754

Hi,

It's actually the same view file. The problem is that $this->row contains the current element for which the add to cart button is displayed. So normally, $this->row is indeed the current product. But if it is for a product with variants, then $this->row is the current variant and thus your check doesn't work since variants are not linked to categories.

Instead of $this->row->category_id I would rather recommend you to do a foreach on $this->categories which contains an array of categories.
Something like this:

$class = hikashop_get('class.category');
$categories = $class->getChildren(16, true, array(), '', 0, 500); //Category: Services (id:16)
foreach($categories as $category) {
 foreach($this->categories as $product_category) {
  if($category->category_id == $product_category->category_id){
	$add_to_cart = false;
  }
 }
}

The following user(s) said Thank You: oxido

Please Log in or Create an account to join the conversation.

Time to create page: 0.097 seconds
Powered by Kunena Forum