Extend Shopify product query

With the latest upgrade of our Shopify integration (now version 2021-10), you can now append any field to the existing query in Product through the ProductAPI decorator (see the decorators article for more information).

Let's look at an example. Say you want to get the publishedAt information as part of the Product query request. The beforeQuery method should add the rawApiInput details, for example:

public function beforeQuery(
    ProductApi $productApi,
  ProductQuery $query,
  string $mode = ProductApi::QUERY_SYNC
): ?array {
    ...
    $query->rawApiInput[ShopifyProductSearchApi::PRODUCT_QUERY_FIELDS_LABEL] = "publishedAt";
    ...
}

Anything added to the rawApiInput there as a string will be appended at the end of the current Product query.

You'll be able to use the after methods to access this data within the dangerousInner fields. On the given example, you could implement afterQuery:

public function afterQuery(ProductApi $productApi, ?Result $result): ?Result
{
    ...
    $publishedAt = $result->items[0]->['dangerousInnerProduct']['publishedAt'];
    ...
    return null;
}

You could use any of the following indexes on rawApiInput:

beforeQuery and beforeGetProduct actions

ShopifyProductSearchApi::PRODUCT_QUERY_FIELDS_LABEL for the Product object

ShopifyProductSearchApi::COLLECTION_QUERY_FIELDS_LABEL for the Collection object

ShopifyProductSearchApi::METAFIELD_QUERY_FIELDS_LABEL for the Metafield object

ShopifyProductSearchApi::SEO_QUERY_FIELDS_LABEL for the SEO object

ShopifyProductSearchApi::VARIANT_QUERY_FIELDS_LABEL for the Variant object

ShopifyProductSearchApi::PRICE_V2_QUERY_FIELDS_LABE for the PriceV2 object

ShopifyProductSearchApi::IMAGE_QUERY_FIELDS_LABEL for the Image object

ShopifyProductSearchApi::SELECTED_OPTION_QUERY_FIELDS_LABEL for the Selected Option object


beforeGetCategories action

ShopifyProductApi::COLLECTION_QUERY_FIELDS_LABEL for the Collection object


beforeGetProductTypes action

ShopifyProductApi::PRODUCT_TYPE_QUERY_FIELDS_LABEL for the ProductType object

It's also possible to add more complex fields, as long as they're specified as a single string, for example:

public function beforeQuery(
    ProductApi $productApi,
  ProductQuery $query,
  string $mode = ProductApi::QUERY_SYNC
): ?array {
    ....
    $query->rawApiInput[ShopifyProductSearchApi::PRODUCT_QUERY_FIELDS_LABEL] = "
    priceRange {
      maxVariantPrice {
        amount
        currencyCode
      }
      minVariantPrice {
        amount
        currencyCode
      }
    }";
    ...
}

Did this page help you?