cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Convert Product to GenericVariantProduct

serdar_dere_ex
Discoverer
0 Kudos
441

Hello Community,

I am trying to enable Variants and tried few things which failed:

1. I wanted to snyc existing catalog to new catalog, copy them to baseproducts with suffix _B and change them to GenericVariantProduct while changing the TypePkString in DB, which failed

2. I tried to write groovy Scripts, but they mostly stopped with timeouts from DB.

3. I tried to export the products but it looks like, that not everything was exported.

My Question here is: is there a simple way to deeply copy Products to save them as GenericVariantProducts with small Category changes? Or can I somehow use the CatalogSyncJob to help me out here?

Maybe Groovy was the right answer, but how can I stop the process from running into timeout?

Or ist here a simple way to use flexible Search in impex to add columns from different table?

I hope someon can help me here, the existing two posts in this board were not helpful.

Cheers,

Serdar

Accepted Solutions (0)

Answers (1)

Answers (1)

mansurarisoy
Contributor
0 Kudos

 

We did something similar in Backoffice, where our aim was to convert a GenericVariantProduct to a Product type by clicking a button in Backoffice.

By default, SAP Commerce does not allow changing the type of an item directly. However, it can be achieved via SQL, followed by clearing the cache. I believe something similar to the following approach should work for converting a GenericVariantProduct to a Product type. Although I haven’t tested the code, I rearranged it based on my original code used for converting to GenericVariantProduct, so I believe it should work.

You may need to adjust the part where the supercategories of the product are set. By default, the GenericVariantProductValidateInterceptor ensures that the supercategories of a variant and its base product are consistent. However, if you assign the correct categories before saving, it should not cause any issues.

Additionally, you can programmatically disable the interceptor before saving to bypass the validation.

 

protected ProductModel convertToVariantProduct(final ProductModel product, final ProductModel baseProduct){
    final ComposedTypeModel productType = typeService.getComposedTypeForClass(GenericVariantProductModel.class);
    int rows = jdbcTemplate.update("UPDATE products SET TypePkString = ?, p_baseproduct = ? WHERE PK = ?", productType.getPk().getLong(), baseProduct.getPk().getLong(), product.getPk().getLong());
    if(rows != 1){
        LOG.warn("Could not update TypePK of product {}", product.getCode());
        return null;
    }

    Utilities.invalidateCache(product.getPk());

    final GenericVariantProductModel variantProduct = modelService.get(product.getPk());

    try {
        List<CategoryModel> categories = new ArrayList<>();
        variantProduct.setSupercategories(categories);
        modelService.save(variantProduct);
        return variantProduct;
    } catch (Exception ex){
        LOG.error("An exception occurred while trying to convert variant product {}", product.getCode(), ex);
    }

    return null;
}

 

serdar_dere_ex
Discoverer
0 Kudos

Good Morning,

sorry for my late response, this wsa a very difficult task for me and it took a lot of time and effort.

First of all ,the links you asked: https://community.sap.com/t5/crm-and-cx-q-a/convert-existing-product-item-type-to-genericvariantprod... and https://community.sap.com/t5/crm-and-cx-q-a/converting-a-non-variant-product-to-variant-product/qaq-... 

Actually, the idea of writing a cronjob which synchronize everything from one catalog to another was not a full failure.
I mean I cound not snyc the products to genericvariantproducts, it just created products and I needed to delete them lal, but on the other hand I received all images and other medias which I coud just use.

I could not imagine a full-automated solution but a working one, which took a lot of file-editing time.

1. I exported the products from the old catalog in backoffice using two different impexes. 
- I exported text-based information like name, description, summary...
- I exported all codes for other informaiton like unit, medias etc

because I did not have rel variants now, I just created 1:1 relations like for each product in the old catalog I created a baseproduct and a variantproduct in the new catalog in form as <code>_B for baseproduct and <code> for genericvariantproduct, therefore

2. I just created two (Actually 4) impex files where I put simple information for the baseproduct and added _B to code.

To not get cought in timeouts I separeted the impexes between simple information and additional information like medias

Thank you for your help anyway.

Cheers
- Serdar