Vote up!
Vote down!

How do I drill down to a product variant's information?

Working with rules, I'm trying to execute PHP code that allows me to access a product variant's fields so I can snag entity references from the custom fields.

In passing $commerce_product along, I'd originally (and wrongly) thought that the product variant would be encapsulated within $commerce_product.

Just reaching out now. Spelunking code to try to figure this out.

Asked by: knowprose
on December 20, 2012

1 Answer

Vote up!
Vote down!

If all you're passing to your code is a product entity, then you basically are passing it a single product variant. If what you want to see are the related product variants, you'd need to pass it the node (or other entity) that referenced the product to begin with.

In Commerce 1.x (and its distributions), the way we accomplish multiple product variations is through the product reference field from a node to one or more products. We call each product referenced a variant or variation, because you're typically grouping them together through the node so the node functions as the customer facing product page.

Wherever your code is originating, you're going to need to find the parent node your product entity was referenced from to find the others that were visible on the form. Once you have the product, then you'd be able to drill down into its field data using the Entity API or core Field API, but you wouldn't refer to field data on a product entity as "variant information".

That said, without seeing code, it's difficult to help. If this answer isn't enough, please leave a comment on it with a paste of the variable you're talking about and a bit more description of your process so we can get to the bottom of it.

Ryan Szrama
Answer by: Ryan Szrama
Posted: Jan 25, 2013