#create products with default sales channel

5 messages · Page 1 of 1 (latest)

wispy prawn
#

I have a script to copy any new product from Webflow CMS to medusa product table. The script uses following API to create product

const product = await medusa.admin.products.create(productPayload);

This creates product with default SalesChannel and hence displayed on the storefront by default.

Now, I converting the script to a scheduled job. For that I have to use productService and create products using the following API call

const product = await productService.create(productPayload);

However, this leaves the Sales Channel field blank and hence the product is not displayed on the storefront.

What is the easiest way to have productService.create to create a product with default sales channel?

hollow wave
#

You can use the retrieveDefault method of the salesChannelService to retrieve the Default Sales Channel. You can then pass the sales channel ID to the create product method.

wispy prawn
#

Thanks @hollow wave , I ended up trying something like that to fix this

const salesChannelService = container.resolve("salesChannelService");
const defaultSalesChannel = await salesChannelService.retrieveDefault();
productPayload["sales_channels"] = [defaultSalesChannel.id];

const newProduct = await productService.create(productPayload);

But this gives the following error. Anything wrong I'm doing here?

QueryFailedError: null value in column "sales_channel_id" of relation "product_sales_channel" violates not-null constraint
    at PostgresQueryRunner.query (/Users/somnath/startups/greetwell/medusa-backend/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:219:19)

...

    severity: 'ERROR',
    code: '23502',
    detail: 'Failing row contains (prod_01J0441JJPMVGR9SAKJC5T9CJY, null).',
    hint: undefined,
    position: undefined,
    internalPosition: undefined,
    internalQuery: undefined,
    where: undefined,
    schema: 'public',
    table: 'product_sales_channel',
    column: 'sales_channel_id',
    dataType: undefined,
    constraint: undefined,
    file: 'execMain.c',
    line: '2009',
    routine: 'ExecConstraints'
hollow wave
#

Hi @wispy prawn , the sales_channels key should be an array of objects with the key value pairing of {id: <sales_channel_id>} https://docs.medusajs.com/api/admin#products_postproducts

const salesChannelService = container.resolve("salesChannelService");
const defaultSalesChannel = await salesChannelService.retrieveDefault();
productPayload["sales_channels"] = [
  { id: defaultSalesChannel.id },
];
const newProduct = await productService.create(productPayload);

REST API reference for the Medusa admin API. This reference includes code snippets and examples for Medusa JS Client and cURL.

wispy prawn
#

That's great. It worked. Thanks so much @hollow wave

I wish though that medusa.admin.products.create(productPayload); and productService.create(productPayload); behaved in the same way and assigns defualt saleschannel to the items. Hopefully it will be taken care. Thanks