Score:0

How to get a product variant field (not attribute) value?

au flag

I have created a product variant with 1 attribute (say "Subscription Plan" or attribute_sub_plan) and 1 field (say "Number of Issues" or field_num_issues). The attribute was for human readability while the field was an integer for internal use.

The attribute was added to the variation type when created, while the field was added to the variation type on the "Add field" button after creation. I can see both fields in the Product Variant interface.

I can easily get the attribute value like this:

$plan_name = $variation->getAttributeValue('attribute_sub_plan')->getName();

However, I cannot get the field value like I usually can with other entities:

$num_issues = $variation->get('field_num_issues')->value;

As a matter of fact, I cannot even get the field "field_num_issues". It gives me this error:

InvalidArgumentException with message 'Field field_num_issues is unknown.'

Do you have any idea how I can get the value?

tonytheferg avatar
cn flag
Is there a typo in your code? There is one in the error message.
au flag
@tonytheferg; Thanks. But no. The typo was only in my question. I slightly changed the field names here from my original code. In the real code, I use copy-and-paste to make sure I did not mis-type anything.
No Sssweat avatar
ua flag
`$variation->get($field_name)` is legit. So double check the field machine name, I bet it's wrong.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.