skip to Main Content

I want to create prodcut with sales channel ID using Python and GraphQL API on Shopify.

sales channel example

But I don’t know how to know sales channel ID for first.

So, please tell me how to get to know sales channel ID using GQL query and then if possible, 

tell me how to create a product with sales channel using Python and GraphQL.

GQL query

sorry I have no idea

Python code 
maybe its gonna be like this

import requests
import json

# Shopify API credentials
shop_url = "SHOP_URL"
access_token = "ACCESS_TOKEN"

# GraphQL endpoint
graphql_url = f"{shop_url}/admin/api/2023-10/graphql.json"

# Build the GraphQL mutation
mutation = """
GQL query 
"""

headers = {
    "Content-Type": "application/json",
    "X-Shopify-Access-Token": access_token,
}


# Create the request data
data = {
    "query": mutation,
}

response = requests.post(graphql_url, json=data, headers=headers)

if response.status_code == 200:
    result = response.json()
    print("Product with options and option values created:")
    print(json.dumps(result, indent=2))
else:
    print(f"Failed to create the product. Status Code: {response.status_code}")
    print(response.text)

What I have tried;

{
  salesChannels {
    edges {
      node {
        id
        name
        type
      }
    }
  }
}

{
  channels {
    edges {
      node {
        id
        name
        handle
        type
      }
    }
  }
}

2

Answers


  1. There is no salesChannel endpoint. Instead, there are channel and channels. Note that these are also deprecated, and the recommendation is to use publications. Good luck with that. You’ll also find those fairly hard to work with. This is useful only for Plus with scope write_publications.

    Login or Signup to reply.
  2. So I was also looking for this stuff and what i found was, that you need the Publication of a App to make it public automaticly.
    So What you can do is: Get the publication from a sales cahnnel and add your products to this publication.

    Here some TS code to get you started:

    const publishAt = ['Online Store'];
    
    async getValidPublications(client: ShopifyGraphqlClient) {
        const catalogs = await client.query<ShopifyAPI.CatalogConnection>(`
        {
            catalogs(first: 100) {
                nodes {
                    ... on AppCatalog {
                        title
                        apps(first: 1) {
                            nodes {
                                id
                                title
                            }
                        }
                        publication {
                            id
                        }
                    }
                    id
                }
            }
        }
        `)
        const foundCatalogs = catalogs.catalogs.nodes.filter(catalog => {
            const appCatalog = catalog as ShopifyAPI.AppCatalog;
            const app = appCatalog.apps?.nodes?.find(app => publishAt.includes(app.title));
            return app !== undefined && catalog.publication;
        })
        return foundCatalogs.map(catalog => catalog.publication!)
    }
    
    async addProductToPublication(product: ShopifyAPI.Product, publication: ShopifyAPI.Publication){
      await client.query(`
        mutation publishablePublish($id: ID!, $input: [PublicationInput!]!) {
            publishablePublish(id: $id, input: $input) {
                publishable {
                    availablePublicationCount
                    publicationCount
                }
                shop {
                    publicationCount
                }
                userErrors {
                    field
                    message
                }
            }
        }`, {
          id: product.id,
          input: channels.map(channel => {
              return {
                  "publicationId": publication.id
              }
          })
      });
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search