How to set product recommendations with the API?

How to set product recommendations with the API?

slf92637
Tourist
6 0 5

I run a shop that sells 20.000+ thousands of unique products (think antiques).

We recently migrated to a 2.0 theme and are now looking to use the Search & Discovery's new recommendation system for products. Sadly it seems like the automated recommendations generated by Shopify are very lackluster. They just don't make much sense.

 

Thing is, we have an ERP-System that contains all the data needed to tell Shopify "Recommend these ten products for people that look at this product"... but I can't seem to find an API or some bulk editing tool to do that. The user interface provided by Search and Discovery expects me to manually specify all the recommended products, which is just not possible for the size of our inventory.

Replies 11 (11)

ricardoceci
Shopify Partner
14 0 3

Hi:

 

Are you open to use 3rd party tools, like Amazon Personalize?. Do you have any specific criteria for recommending products? Or are you just leveraging in similarity?

 

Thanks,

Ricardo | Founder @ VN Studios
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
slf92637
Tourist
6 0 5

I am asking for a way to tell Shopify over API which products to recommend. I would still need that way if I were to use a 3rd party tool like Amazon Personalize.

 

That being said, the recommendation that I want would be based on Shopify tags. e.g. When the customer looks at 18th century furniture, just recommend other products that are tagged with '18th century' and 'furniture'.

DeyanTodorov
Shopify Partner
2 0 1

Hey, I'm in same situation. Do u find a solution for that?

slf92637
Tourist
6 0 5

Hey,

 

I have an idea that might work, but I have not gotten around to trying it myself yet.

Apparently the Product Recommendations are controlled by a product metafield definition called product_recommendation.related_products

 

While there seems to be no direct API to set product recommendations, there is an API to assign metafield values. My plan is to try and fill a product's metafield product_recommendation.related_products with ids for the products I want recommended.

 

Like I said though, this is just a plan right now, I can not verify that it works. If you get around to trying it sooner than me, I'd appreciate it if you let me know your results.

slf92637
Tourist
6 0 5

Heya, I can now verify that this works. You need to fill the metafields from search & discovery through the metafields api.

 

 

RichardRaygun
Shopify Partner
4 0 0

Hey @slf92637, how exactly did you set the product_recommendation.related_products metafield? Is that metafield an array of ids? I can't seem to push any data to it. What am I missing?

Here is my mechanic script for reference.

mutation {
  productUpdate(
    input: {
      id: "gid://shopify/Product/8876709642516"
      metafields: [
        {
          key: "related_products"
          namespace: "product_recommendation"
          value: "gid://shopify/Product/8876709642516"
        }
      ]
    }
  ) {
    product {
      id
      metafield(namespace: "product_recommendation", key: "related_products") {
        value
      }
    }
  }
}

Result:

{
  "data": {
    "productUpdate": {
      "product": {
        "id": "gid://shopify/Product/8876709642516",
        "metafield": null
      }
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 11,
      "actualQueryCost": 11,
      "throttleStatus": {
        "maximumAvailable": 2000,
        "currentlyAvailable": 1989,
        "restoreRate": 100
      }
    }
  }
}

 

slf92637
Tourist
6 0 5

I am not super familiar with GraphQL, but looking at your request, the value

"gid://shopify/Product/8876709642516"

seems correct. Maybe try wrapping that inside an array i.e. ["gid://..."]

 

Other than that, I think your namespace might be off. On my end the namespace is

shopify--discovery--product_recommendation

 

 

RichardRaygun
Shopify Partner
4 0 0

Thanks for the quick response @slf92637 , it's definitely the namespace, it works now. Where is this said in the documentation? I couldn't find it.

slf92637
Tourist
6 0 5

Sadly it seems like this explicit use case "How do I set recommended products in the API" is not documented anywhere. That's how I wound up creating this thread.

 

I more or less puzzled together how to do this after I noticed:

- You can edit Metafields over the API

- Recommended Products are a Metafield.

 

As far as to how I figured out the namespace thing: I looked that up the metafield definitions in the Admin Backend:

slf92637_0-1706252518886.png

 

RichardRaygun
Shopify Partner
4 0 0

Do you know why this isnt updating the metafield? It updates other metafields fine, this sends a success code but it doesnt add my desired metafield? Any ideas?

const PRODUCT_UPDATE_MUTATION = `
  mutation updateProduct($input: ProductInput!) {
    productUpdate(input: $input) {
      product {
        id
        metafield(namespace: "shopify--discovery--product_recommendation", key: "related_products") {
          value
          type
        }
      }
    }
  }
`;
async function updateProductMetafield(productId, metafieldValue, client) {
  // Log for debugging
  console.log(`Attempting to Update product metafield for ID ${productId}`);

  var metaDataList = [
    metafieldValue
  ];
  
  var metaJSON = JSON.stringify(metaDataList);

  // GraphQL mutation to update the product metafield
  const mutation = {
    query: PRODUCT_UPDATE_MUTATION,
    variables: {
      input: {
        id: productId,
        metafields: [
          {
            key: "product_recommendation",  // Specify your metafield key
            value:  metaJSON,
            namespace: "shopify--discovery--product_recommendation",  // Specify your namespace
            type: "list.product_reference"
          },
        ],
      },
    },
  };

 
Output:

| GraphQL Mutation: {
2024-01-30 23:00:45 | backend  |   "query": "\n  mutation updateProduct($input: ProductInput!) {\n    productUpdate(input: $input) {\n      product {\n        id\n        metafield(namespace:
                                 \"shopify--discovery--product_recommendation\", key: \"related_products\") {\n          value\n          type\n        }\n      }\n    }\n  }\n",
2024-01-30 23:00:45 | backend  |   "variables": {
2024-01-30 23:00:45 | backend  |     "input": {
2024-01-30 23:00:45 | backend  |       "id": "gid://shopify/Product/8954136690964",
2024-01-30 23:00:45 | backend  |       "metafields": [
2024-01-30 23:00:45 | backend  |         {
2024-01-30 23:00:45 | backend  |           "key": "product_recommendation",
2024-01-30 23:00:45 | backend  |           "value": "[\"gid://shopify/Product/8876710691092\"]",
2024-01-30 23:00:45 | backend  |           "namespace": "shopify--discovery--product_recommendation",
2024-01-30 23:00:45 | backend  |           "type": "list.product_reference"
2024-01-30 23:00:45 | backend  |         }
2024-01-30 23:00:45 | backend  |       ]
2024-01-30 23:00:45 | backend  |     }
2024-01-30 23:00:45 | backend  |   }
2024-01-30 23:00:45 | backend  | }
2024-01-30 23:00:45 | backend  | GraphQL Response: {
2024-01-30 23:00:45 | backend  |   "data": {
2024-01-30 23:00:45 | backend  |     "productUpdate": {
2024-01-30 23:00:45 | backend  |       "product": {
2024-01-30 23:00:45 | backend  |         "id": "gid://shopify/Product/8954136690964",
2024-01-30 23:00:45 | backend  |         "metafield": {
2024-01-30 23:00:45 | backend  |           "value": "[\"gid://shopify/Product/8861507551508\"]",
2024-01-30 23:00:45 | backend  |           "type": "list.product_reference"
2024-01-30 23:00:45 | backend  |         }
2024-01-30 23:00:45 | backend  |       }
2024-01-30 23:00:45 | backend  |     }
2024-01-30 23:00:45 | backend  |   },
2024-01-30 23:00:45 | backend  |   "extensions": {
2024-01-30 23:00:45 | backend  |     "cost": {
2024-01-30 23:00:45 | backend  |       "requestedQueryCost": 11,
2024-01-30 23:00:45 | backend  |       "actualQueryCost": 11,
2024-01-30 23:00:45 | backend  |       "throttleStatus": {
2024-01-30 23:00:45 | backend  |         "maximumAvailable": 2000,
2024-01-30 23:00:45 | backend  |         "currentlyAvailable": 1198,
2024-01-30 23:00:45 | backend  |         "restoreRate": 100
2024-01-30 23:00:45 | backend  |       }
2024-01-30 23:00:45 | backend  |     }
2024-01-30 23:00:45 | backend  |   }
2024-01-30 23:00:45 | backend  | }
2024-01-30 23:00:45 | backend  | Mutation successful!
2024-01-30 23:00:45 | backend  | Updated product ID: gid://shopify/Product/8954136690964




RichardRaygun
Shopify Partner
4 0 0

I realized I was using the wrong key, sadly it still doesnt work with the key related_products, not sure why this doesnt work, as it works with every other metafield