Forum

Thread tagged as: Shop

Filtering with perch_shop_product_variants

Hello,

I've noticed that using the filter option with perch_shop_product_variants() filters through variants of all products rather than just those of the product specified in the first parameter.

perch_shop_product_variants(perch_get('s')) only outputs variants belonging to the product specified in the first parameter as expected.

While the below outputs any variant of any active product that meets the filtering criteria (regardless of the slug provided in the first parameter):

perch_shop_product_variants(perch_get('s'), [
  'filter' => 'stock_level',
  'match' => 'gte',
  'value' => 1,
]);

I can use multiple filters to also filter by the product slug so it's not the end of the world. I'm just reporting this in case it is not the intended behaviour.

Hussein Al Hammad

Hussein Al Hammad 86 points

  • 8 months ago
Drew McLellan

Drew McLellan 2603 points
Perch Support

The default options for that function filter by the parent product (as specified by the slug). If you specify your own filter, it's replacing that default.

Hussein Al Hammad

Hussein Al Hammad 86 points
Registered Developer

Ok! I think it makes sense to have that default filter in there all the time even when I add my own filters given the function's name and the documentation:

Display a list of product variants for a given product

Adding a filter at the moment makes it behave like perch_shop_variants() if there was such a function.

Anyway, I rather see you invest your time in other Shop features. I'm not even using the function and was only evaluating my approach when I encountered this ^^!

Thanks Drew.

Drew McLellan

Drew McLellan 2603 points
Perch Support

Yes, I think it would make sense. It's not very intuitive how it is now.