Hide a product

Doris Fleming asked on January 9, 2020 05:43

I am performing CRUD operations for products of e-commerce site in kentico 10.I can add and update products using below API

SKUInfoProvider.SetSKUInfo(updateProduct);

But I do not wish to delete the product from database,rather just disable them so that they do not show up to the end users and still stay in the database .

I tried to set SKUEnabled as false but still user can see the product.So, I used below code to hide the products

ProductNode.SetValue("DocumentPublishTo", DateTime.Now.AddDays(-1));

But my code setup adds a new product with above disabled property.Here is my code

foreach (DataRow dr in dt.Rows)
    {                
        manufacturer = GetManufacturer(Convert.ToString(dr["MANUFACTURER_NAME"]));
        department = GetDepartment(Convert.ToString(dr["CATEGORY_OF_PRODUCT_1"]));

        var sku = new SKUInfo
        {
            SKUNumber = Convert.ToString(dr["MANUFACTURER_PART_NUMBER"]),
            SKUName = Convert.ToString(dr["MANUFACTURER_PART_NUMBER"]),
            SKUDescription = Convert.ToString(dr["TECHNICAL_SPECIFICATIONS"]).Trim('"'),
            SKUShortDescription = Convert.ToString(dr["SHORT_DESCRIPTION"]).Trim('"'),
            SKUPrice = ValidationHelper.GetDouble(dr["RESELLER_BUY_INC"], 0),
            SKURetailPrice = ValidationHelper.GetDouble(dr["RRP_INC"], 0),
            SKUEnabled = false,
            SKUSiteID = siteId,
            SKUProductType = SKUProductTypeEnum.Product,
            SKUManufacturerID = manufacturer.ManufacturerID,
            SKUDepartmentID = department.DepartmentID,
            SKUHeight = ValidationHelper.GetDouble(dr["HEIGHT"], 0),
            SKUWidth = ValidationHelper.GetDouble(dr["WIDTH"], 0),
            SKUWeight = ValidationHelper.GetDouble(dr["WEIGHT"], 0),
            SKUDepth = ValidationHelper.GetDouble(dr["LENGTH"], 0),
            SKUAvailableItems = 1,
            SKUSellOnlyAvailable = true
        };

        try
        {

            SKUInfo updateProduct = SKUInfoProvider.GetSKUs()
                            .WhereEquals("SKUNumber", sku.SKUNumber)
                            .FirstObject;

            sku.SKUPrice += sku.SKUPrice * 0.015;


            if (updateProduct != null)
            {
                updateProduct.SKUNumber = sku.SKUNumber; updateProduct.SKUName = sku.SKUName;
                SKUInfoProvider.SetSKUInfo(updateProduct);
            }

            else
            {
                SKUInfoProvider.SetSKUInfo(sku);
            }

            if (!sku.SKUEnabled)
            {
                SKUTreeNode productDoc = (SKUTreeNode)SKUTreeNode.New(productDocumentType.ClassName, tree);
                if (sku.SKUEnabled == false)
                {
                    productDoc.DocumentPublishTo = DateTime.Now.AddDays(-1);
                }

                productDoc.DocumentSKUName = Convert.ToString(dr["MANUFACTURER_PART_NUMBER"]);
                productDoc.DocumentSKUDescription = sku.SKUDescription;
                productDoc.NodeSKUID = sku.SKUID;
                productDoc.DocumentCulture = cultureCode;
                productDoc.DocumentName = Convert.ToString(dr["MANUFACTURER_PART_NUMBER"]);

            }
        }                

        catch (Exception ex)
        {
            error += "error";
        }

    }

Please provide the possible solution.There ain't no property such as DocumentPublishTo in SKUInfo,hence I used it with SKUTreeNode which you can find in code setup.

SKUTreeNode productDoc = (SKUTreeNode)SKUTreeNode.New(productDocumentType.ClassName, tree);
            if (sku.SKUEnabled == false)
            {
                productDoc.DocumentPublishTo = DateTime.Now.AddDays(-1);
            }

Recent Answers


Roman Hutnyk answered on January 9, 2020 13:36

I can't see any code that would save a product/page after you set DocumentPublishTo.

1 votesVote for this answer Mark as a Correct answer

Keio Kwan answered on January 9, 2020 14:53

If you are not using standalone SKU, which should have a page with SKU (product page type). Please see the e-commence API reference at https://docs.kentico.com/api10/e-commerce/products.

As Roman said, your create/update function is incompleted. see the create product and update product section to find out more :)

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.