Developer forum

Forum » CMS - Standard features » ItemCreator API and Add/Update to Product Field type

ItemCreator API and Add/Update to Product Field type

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Guys,

I have a situation where I need to create ItemType based pages and Paragraphs using API. What wouls the best place to find out about how to do that? I mean in the API documentation.

The second part is about adding/removing a product in a Product field of an ItemType.

For example, asuming the Itemtype that I create with the API, has a Product field type, I want to be able to add products to that field and also remove products from that field.

Is this possible using the API? If yes, how?

Thank you,

Adrian


Replies

 
Victor Letavin
Reply
This post has been marked as an answer

Hi Adrian,

For ItemCreator related documentation you asked you can look here - https://doc.dynamicweb.com/api/html/fa78dc65-fe4b-192f-bf93-db50d205750d.htm

For Api example to modify selected product in item field type of product you can use something like this:


using System.Collections.Generic;
using System.Linq;

class ItemWithProductFieldExample
    {
        public ItemWithProductFieldExample() { }

        private const string productItemPrefix = "p_";
        private const string productVariantSeparator = ":";

        public void addProduct(List<string> productItems, string productId, string variantId)
        {
            if (!string.IsNullOrWhiteSpace(productId))
            {
                productItems.Add($"{productItemPrefix}{productId}:{variantId}");
            }
        }

        public void deleteProduct(List<string> productItems, string productId, string variantId)
        {
            if (!string.IsNullOrWhiteSpace(productId) && productItems.Count > 0)
            {
                var productItemToDelete = $"{productItemPrefix}{productId}";
                if (!string.IsNullOrWhiteSpace(variantId))
                {
                    productItemToDelete = $"{productItemToDelete}:{variantId}";
                }

                for(var i = productItems.Count-1; i>=0;i--) {
                    if (productItems[i] == productItemToDelete || productItems[i] == $"{productItemToDelete}:") {
                        productItems.RemoveAt(i);
                    }
                }
            }
        }

        public void main()
        {
            var item = Services.Items.GetItem("SelectorFields", "1"); //"SelectorFields" - itemtype system name;  "1" - item id
            string fieldValue = item["Product"].ToString(); //"Product" - field system name

            var productFieldItems = fieldValue.Split(',').ToList();

            //add product to item field
            addProduct(productFieldItems, "PROD34", "");

            //delete product to item field
            deleteProduct(productFieldItems, "PROD34", "");

            item["Product"] = string.Join(",", productFieldItems);

            item.Save();
        }
    }


BR, Viktor.

Votes for this answer: 1
 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Thank you very much Victor for the comprehensive answer.

I will give it a try.

Thank you,
Adrian

 

You must be logged in to post in the forum