Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

How to change the value of Metafields

Vote:
 

Hi,

I have created few metafields and associate these with a meta class. After that I have created Product and publish it. On Publish event, I want to update these metafield's value of published product. Please suggest how to update. FYI, I am able to capture publish event.

Thanks.

#180144
Edited, Jun 30, 2017 11:27
Vote:
 

There are various ways to do this:

  1. If you have the product as ContentData: newProduct.SetValue("MetaFieldName", MetaValue);
  2. newProduct["MetaFieldName"] = value;
  3. newProduct.Properties["MetaFieldName"] = value; //-for Epi 10+
#180176
Jun 30, 2017 19:20
Vote:
 

The recommended way is to have the strongly typed content type for your metaclass, and set it like this:

yourContent.YourProperty = "new value";

ContentRepository.Save(yourContent, SaveAction.Publish);

this works for Commerce 7.5+

#180177
Jun 30, 2017 20:24
Vote:
 

Thanks Sapna & Quan for prompt response. Can you please help me a bit more. Let me explain further. Here is my code:

 public class InitializationModule : IInitializableModule
{
      public void Initialize(InitializationEngine context)
      {
           var contentEvents = context.Locate.ContentEvents();
           contentEvents.PublishedContent += EventsPublishedContent; 

      }

}

private void EventsPublishedContent(object sender, EventArgs e)
{

//Here I will call a web service. This web method will return me an id. I need to save this id into current published product metafield.  

}

Please suggest if I can update metafield in above event. Does eventargs holds current published product information? FYI, I have created Metafields, Metaclass through UI only.

#180201
Edited, Jul 03, 2017 8:43
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.