craftcms / shopify

Synchronize and extend product data from your Shopify storefront.
MIT License
45 stars 25 forks source link

Does craft.shopifyProducts.shopifyId() work with fixedOrder? #98

Closed AmandaLutzTO closed 7 months ago

AmandaLutzTO commented 7 months ago

Description

I've successfully used craft.shopify.api.get to get all of the products belonging to a Shopify Collection (https://shopify.dev/docs/api/admin-rest/2023-10/resources/collection#get-collections-collection-id-products). In the Shopify admin, the Collections have the products in order of "Best selling".

I can loop through all the products returned by the api to build an array of ids but I want them displayed in that order, not the current postDate DESC.

I've tried fixedOrder (https://craftcms.com/docs/4.x/entries.html#fixedorder) but returns an empty result set.

Steps to reproduce

  1. craft.shopify.api.get to get all of the products belonging to a Shopify Collection
  2. loop through all the products returned by the api
  3. build an array of ids
  4. are displayed postDate DESC
  5. add fixedOrder to craft.shopifyProducts.shopifyId() query
  6. returns empty result set

Additional info

linear[bot] commented 7 months ago

PT-1390 Does craft.shopifyProducts.shopifyId() work with fixedOrder?

nfourtythree commented 7 months ago

Hi @AmandaLutzTO

This is possible to do, but when using the fixedOrder param you must use the id() criteria to be able to set the order. This id is the element id in Craft.

As you identified you can retrieve the products from the Shopify collection and then query for them using the shopifyId to get the corresponding Craft element ID. Once you have built this array of IDs you can then pass them into the Shopify products query as follows:

{# Where `ids` is a pre-built array of element IDs #}
{% set ids = [123, 456] %}
{% set products = craft.shopifyProducts.id(ids).fixedOrder().all() %}

Hope this helps, thanks!

AmandaLutzTO commented 7 months ago

Exactly what I was missing. Thanks.