1

I am currently in the process of building a commerce 2 site with a very detailed level of nesting in the product options so we're looking to list all variants for a product. (Sounds silly, but it works in this case!)

However, while trying to use paginate, I'm getting the following error:

Impossible to invoke a method ("limit") on an array.

Here is my markup:

{% paginate variants.limit(12) as pageInfo, pageEntries %}

Is it even possible to paginate variants?

Thanks

EDIT

We are getting all the product variants as follows:

{% set variants = product.variants %}

But if we use the limit here outside of the pagination, won't that mean we will always get the same first 12 items. Whereas we need to limit it within the paginator so we get the first 12, the next 12 etc etc.

Cheers

3

product.variants returns the variants for the product as an array only. It is not like a element relationship field which returns an element query.

If you want to use a element query of variants for a particular product you can do:

craft.variants.product(product).all

  • 1
    To complete Luke's answer, omit the .all() call here, and pass the main VariantQuery object to the paginate template tag: {% paginate craft.variants({ product: product }) as pageInfo, pageVariants %} (Otherwise, you're back at square one, with a simple array!) – August Miller Dec 17 '18 at 18:40
  • Thanks both. @AugustMiller your tag worked perfectly. Thanks! – Jay Dec 19 '18 at 12:13
  • I ended up going with {% paginate craft.variants({ product: product }).limit(12) as pageInfo, pageVariants %} to get the pagination to work properly by the way. :) – Jay Dec 19 '18 at 16:36
1

I'm interested how you are calling variants - have you tried using craft.variants.limit(12) versus variants.limit(12)? Craft Docs - sample variant query

Here is another example where Brad shares insights on paginating after using the search service.

  • Updated question. – Jay Dec 14 '18 at 12:30

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.