Support » Plugin: WooCommerce » Woocommerce product_category AND operator returns no results

  • I’m currently building a Woocommerce integrated custom theme for a client. One requirement is certain pages need to only show products that are in a category AND are in another category.

    After a bit of searching around, including looking at the class-wc-shortcodes.php in the Woocommerce plugin itself I’ve found this can be done easily by using this shortcode:

    [product_category category="hotshop-colors,red" order="name" operator="AND"]

    The only trouble is it returns no results. If the operator="AND" bit is left out I get a list of everything that is red and everything that is hotshop-colors, however some of those red items are not hotshop-colors and those should be excluded.

    There are folks online who have recommended using AND so I’m guessing it’s working for them. Perhaps they are using an older version though.

    So, went and looked under the hood and on line 169 of the class-wc-shortcodes.php there is this code and comment:

    'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.

    But, again, it doesn’t seem to actually do that. I’ve attempted to follow the function calls down to somewhere I might see an exclusion of this operator somewhere and nothing. Seems like it just does a WP_Query and returns an array of products.

    Totally confused here now, and would love to have this working, as it’s a much more elegant solution than creating a new product category called hotshop-colors-red and changing all of my product records.

    Any insight/help would be greatly appreciated.

    https://wordpress.org/plugins/woocommerce/

Viewing 15 replies - 1 through 15 (of 19 total)
  • Plugin Author Mike Jolley

    (@mikejolley)

    AND requires that they are actually in the category. I don’t think it will work if you have hierarchy and don’t assign the term to the product. Also, ‘red’ would have to be a category slug. Is it?

    Hi Mike,

    Thanks for responding and sorry for the late reply.

    The products are added to both the ‘hotshop-colors’ and ‘red’ categories. ‘red’ is a top level category, however ‘hotshop-colors’ is a subcategory of ‘hotshop’.

    It seems to me that hierarchy shouldn’t matter though as the request to WP_Query should just bring back all products that have been given the categories of both ‘red’ and ‘hotshop-color’? Perhaps there’s something I don’t understand about the AND operator in WordPress.

    Plugin Author Mike Jolley

    (@mikejolley)

    WP_Query should just bring back all products that have been given the categories of both ‘red’ and ‘hotshop-color’?

    Just to clarify, AND means they need to be inside BOTH.

    Hi Mike,

    Yes, I have several products in *both*.

    Thanks again.

    Cheers,
    -Joe

    Plugin Author Mike Jolley

    (@mikejolley)

    Thats all I can think of. I did a test locally with https://dl.dropboxusercontent.com/s/trvdenbavl6rqnj/2016-05-26%20at%2010.30.png?dl=0 and it worked fine.

    Weird, wondering if maybe another plugin I have installed could be conflicting.

    You were able to use this shortcode to get it to work?:

    [product_category category="hotshop-colors,red" order="name" operator="AND"]

    ok, just disabled all my other plugins and still no luck.

    Plugin Author Mike Jolley

    (@mikejolley)

    Yes I used the same categories and the shortcode. 1 product showed up, the one I screenshot.

    Ok, cool, thanks so much for the help Mike. Looks like I’ll have to figure out another route. If I figure out what it is about my particular install that makes this fail I’ll let you know.

    Plugin Author Mike Jolley

    (@mikejolley)

    Thanks that would be useful.

    linds

    (@lsterling03)

    Did anyone ever solve this? I’m having the exact same problem. I have product in two different terms, but when I try to use the ‘AND’ operator, it doesn’t show anything. If I use ‘IN’ is shows all products in either of the 2 categories. But I need to work with ‘AND’ so that it will only show products in BOTH categories.

    I too have deactivated all plugins and used the default WP theme — still doesn’t work. I’ve also seen another thread with this same problem, so it seems like a bug to me.

    Plugin Author Mike Jolley

    (@mikejolley)

    No, still been unable to replicate my end.

    linds

    (@lsterling03)

    Mike, can you please share how you’re using the shortcode with the AND operator?

    Here’s what I’m trying:

    THIS WORKS (shows all products that are in EITHER the fall or classic categories)

    [product_category category="fall,classic" operator="IN"]

    THIS DOES NOT WORK (shows nothing, even though I definitely have products that are marked in both of these categories)

    [product_category category="fall,classic" operator="AND"]

    I want to show products only if they are both fall AND classic, not fall OR classic.

    I don’t know why you aren’t able to recreate it. I’ve tried it on two separate WP installs, one totally blank with nothing but WooCommerce and the default theme. I’m using the most recent version of WP and Woo.

    I appreciate any advice you can give.

    Plugin Author Mike Jolley

    (@mikejolley)

    I used this earlier:

    [product_category category="hotshop-colors,red" operator="AND"]

    And I created those top level cats and assigned to a product, which was displayed.

    You need to be certain those slugs are what is used in Products > Categories.

    Yep, I used that short code exactly. Even made sure the two categories were top level for whatever that’s worth, and just now double checked my slugs. Still no luck.

    My guess is there’s simply something different about your install Mike that differs from Beth and mine’s.

    It would be a very useful feature for the site I’m building currently but for now I’ll just have to work around by creating a new sub category like hotshop-colors-red. Now to go and update a thousand or so records…

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Woocommerce product_category AND operator returns no results’ is closed to new replies.