nozzlegear / ShopifySharp

ShopifySharp is a .NET library that helps developers easily authenticate with and manage Shopify stores.
https://nozzlegear.com/shopify-development-handbook
MIT License
747 stars 309 forks source link

Add product into Exising Channels #1039

Closed shafaqat-ali-cms365 closed 6 months ago

shafaqat-ali-cms365 commented 7 months ago

Hi When adding/updating a product, is there any option to provide a list of sale channels? I have 4 sales channels but by default product is being added into 1 channel. I am currently manually adding products into different channels.

mbaugus commented 6 months ago

The only option in the Product API involving a Sales Channel is the "published_scope" property of Product, and it only accepts two values.

web: The product isn't published to the Point of Sale channel. global: The product is published to the Point of Sale channel.

I would try setting the Product.PublishedScope to "global" and see what happens.

If that doesn't work, an upload file will add them to all channels.

shafaqat-ali-cms365 commented 6 months ago

What does "upload file" means?

mbaugus commented 6 months ago

What does "upload file" means?

Importing the products using the Shopify website, uploading them via a .CSV file.

nozzlegear commented 6 months ago

Sorry for the delay. I believe @mbaugus is correct; as far as I've understood it, the Product API has only ever been able to publish products to the "web" sales channel or the "global" sales channel. Web just means the online store, and Global just means online store + point of sale.

That said, I was about to close this issue but I recall that Shopify is doing some major refactoring of their product APIs right now so I did a little research. It looks like they've deprecated the concept of "sales channels" in the GraphQL API, and they've introduced "publications" to replace it. They honestly look to be the same thing to me, I can't really tell the difference, but the important bit is that they have a Graph mutation to let you publish products to specific sales channels:

https://shopify.dev/docs/api/admin-graphql/2024-04/mutations/productPublish

Here's what the GraphQL call would look like:

var service = new GraphService(domain, accessToken);
var graphMutation = 
"""
mutation publishProduct($input: ProductPublishInput!) {
  publishProduct(input: $input) {
    userErrors {
      field
      message
    }
  }
}
""";
var input = new
{
  id = "gid://shopify/product/123456",
  productPublications = 
  [
    new
    {
      channelHandle = "<your-channelHandle>",
      channelId = "gid://shopify/<objectName>/123456",
      publicationId = "gid://shopify/<objectName>/123456",
      publishDate = DateTimeOffset.Now.ToString()
    }
  ]
}
var response = await service.PostAsync<ShopifySharp.GraphQL.ProductPublishPayload>(new GraphRequest
{
  query = graphMutation,
  variables = input
});

if (response.userErrors.Any())
{
  // TODO: handle user errors
}

Let me know if you use that and how it works for you!