#Import product with categories

22 messages · Page 1 of 1 (latest)

timid skiff
#

Hi I am using product categories to categorize the product, and there is category column in my CSV file. Is it possible to import products with category right now?

candid echo
#

I don't know if there have been recent fixes/changes, but personally I have never gotten the product export/import feature to work. I think the team is aware, but there are significant architectural improvements being made right now, so I think unfortunately it is not a high priority right at the moment. With the new admin extensibility in beta right now, I'm hoping someone in the community will seize the opportunity to make a really awesome export/import function that is JSON-based. Not sure why the need to create and then try to parse the csv files.

It's probably not the answer you want to hear, and maybe someone will come along who has tried it more recently and has a more positive report.

timid skiff
# candid echo I don't know if there have been recent fixes/changes, but personally I have neve...

Hi thanks for your comment, I have another question if you dont mind. I am building a site for a bookstore. So in the CSV file, I would add some extra fields such as isbn, numberOfPages, etc. I extended the Product Entity and Repository to reflect this. But when I import using the CSV file, I got the error . Is there any work around rather than importing the CSV file? I saw your comment about sql dumpfile. Would you mind sharing a little more detail about how to do it

#

Thank you

candid echo
#

I tend to use pg_dump and create a dump file of the entire db. It’s best to do this early, before adding a bunch of users or transactions or other data. Then, you can always create a new copy of your db for testing purposes that has all of your products, at least the initial ones. We really need an export/import function. But that is the clunky way I work around it for now. Tomorrow when I’m back at my desk, I can copy and paste some of the commands I use. I just got them from googling.

timid skiff
candid echo
#

Here are some examples from my postgres recipes:

// change next order number in database to 210215
SELECT setval('order_display_id_seq', 210215, true);

// backup db to file
sudo -u postgres pg_dump dbname > dumpfile.sql

// backup db directly to new db
sudo -u postgres pg_dump existing_db | sudo -u postgres psql backup_db
  
  
//import dump file
sudo -u postgres psql -d dbname -f dumpfile.sql
#

Because you run the commands as the postgres user for db access, you may have some quirks with permissions. I sometimes write and import the dumpfile to/from the /tmp folder to not worry about permissions and chmodding /chowning everything.

timid skiff
candid echo
#

It starts out something like this:

--
-- PostgreSQL database dump
--

-- Dumped from database version 15.3 (Ubuntu 15.3-1.pgdg22.04+1)
-- Dumped by pg_dump version 15.3 (Ubuntu 15.3-1.pgdg22.04+1)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: PAYMENT_COLLECTION_STATUS_ENUM; Type: TYPE; Schema: public; Owner: postgres
--

CREATE TYPE public."PAYMENT_COLLECTION_STATUS_ENUM" AS ENUM (
    'not_paid',
    'awaiting',
    'authorized',
    'partially_authorized',
    'canceled'
);

It's really long

#

It is all the commands needed to create the database structure and then insert all the data. So, essentially, it is a backup of the database because it is all the commands needed to recreate it.

visual sand
#

We've added extra fields by extending the Product with new fields and then modifying the import/export batch job to include those new fields. This will allow you to use and control those fields in the CSV spreadsheet and import it.

We've also built an Admin Widget which allows us to control those fields, but for that we had to update to the beta Admin.

timid skiff
visual sand
#

I've basically found the batch jobs in the medusa admin repository, copied them and created my own types for them:

I have import.ts and export.ts in src/strategies. As well as columns-definition.ts in src/strategies/types. All you need to do is copy import/export into strategies and then modify columns-definition.ts with your new fields (See my Custom Attribute in there) and it will work.

timid skiff
visual sand
#

yeah, you also need to extend the admin product fields using this loader:

export default async function () {
  const imports2 = (await import(
    "@medusajs/medusa/dist/api/routes/admin/products/index"
  )) as any;
  imports2.defaultAdminProductFields = [
    ...imports2.defaultAdminProductFields,
    "isbn",
  ];
}
#

then that should allow you to access these fields in the admin

#

Have you got your validator setup as well?

timid skiff
visual sand
#

These are the loaders I have. The first two are extending product and admin product. And this is the validator:

To use 'registerOverriddenValidators' make sure you have the latest medusa version.

import {
  AdminPostProductsProductReq as MedusaAdminPostProductsProductReq,
  registerOverriddenValidators,
} from "@medusajs/medusa";
import { IsString, IsOptional } from "class-validator";

export class AdminPostProductsProductReq extends MedusaAdminPostProductsProductReq {
  @IsString()
  @IsOptional()
  customAttribute: string;
}

export default async function () {
  registerOverriddenValidators(AdminPostProductsProductReq);
}
timid skiff
visual sand
#

No worries, took me a few days to figure out as well haha. Yeah, I use Digitalocean Spaces as well, seems to work pretty good so far