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?

  • Is there a typo in your code? There is one in the error message. – tonytheferg Jun 10 at 4:14
  • @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. – Koala Yeung Jun 10 at 4:16
  • $variation->get($field_name) is legit. So double check the field machine name, I bet it's wrong. – No Sssweat Jun 10 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.