Joseph Anderson
Joseph Anderson

Reputation: 4144

Post Meta Product Attributes

I am creating a bulk import tool to sync product variants to WooCommerce.

I need to create product attributes. In Woo, I created a product attribute called color with a value of red. I cannot use the Woo API because I am performing a bulk copy of 25,000 records. What logic does Woo use to create this phrase? I use C#, so any similar code would be helpful.

a:1:{s:5:"color";a:6:{s:4:"name";s:5:"Color";s:5:"value";s:3:"Red";s:8:"position";s:1:"0";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:0;}}

Here is the record in the post_meta table:

enter image description here

Upvotes: 0

Views: 326

Answers (1)

Joseph Anderson
Joseph Anderson

Reputation: 4144

I figured it out. Here is my code:

        public string SerializeAttribute(List<WooInsertProductAttribute> attrs)
    {
        Dictionary<string, Hashtable> it = new Dictionary<string, Hashtable>();
        Dictionary<string, object> items = new Dictionary<string, object>();

        foreach (WooInsertProductAttribute attr in attrs)
        {
            items = new Dictionary<string, object>();
            items.Add("name", attr.name);
            items.Add("value", attr.value);
            items.Add("position", attr.position);
            items.Add("is_visible", attr.is_visible);
            items.Add("is_variation", attr.is_variation);
            items.Add("is_taxonomy", attr.is_taxonomy);
            it.Add(attr. name, new Hashtable(items));
        }


        Conversive.PHPSerializationLibrary.Serializer serializer = new Conversive.PHPSerializationLibrary.Serializer();
        return serializer.Serialize(new Hashtable(it));
    }

Upvotes: 0

Related Questions