Score:0

How do you programmatically get custom content field data from commerce product?

in flag

Background: I'm creating a rule that runs after an order is submitted that checks all the line items for any backorder and emails the customer. In the PHP function I've been able to use entity_metadata_wrapper(), commerce_line_item_load(), and commerceproduct_load()to successfully get the product information as seen in the var_dump() below. Now I need to get the available stock stored in the Colorway content type.

edit: The Colorway content type is a node with a reference field to commerce products. The function is located inside a custom module we named commerce_overides. it just contains various hooks and functions directly related to commerce. There are no includes.

enter image description here

Question How do I get the fields stored in the Colorway content type? The product has the same sku number as the Colorway as seen below; However, this only made it easy to get the product into the node but not the other direction.

enter image description here

thiokol avatar
cn flag
You havent explained what the Colorway content type is and how its related to Commerce products, is it a node which has a reference field for commerce products? What file/class are you running your code from?
jsank95 avatar
in flag
Thanks for pointing that out. I added it as an edit but the Colorway content type is a node with a reference field to commerce products. The function is located inside a custom module we named commerce_overides. it just contains various hooks and functions directly related to commerce. There are no includes.
thiokol avatar
cn flag
If the Colorway content type has a field referencing the Commerce product then you need to get the parent entity from the product object you already have loaded, maybe something like this: https://drupal.stackexchange.com/questions/293885/how-do-i-get-the-parent-node-of-an-entity-reference-node - replace the entity type with commerce_product and replace the field name with your actual entity reference field name.
jsank95 avatar
in flag
That might work for Drupal 8 but in Drupal 7 it just produces an error.
Score:0
in flag

After trying just about everything I finally figured it out. I needed to use EntityFieldQuery(). this allowed me to use the sku from the commerce product and find the matching fabric_colorway node. then I just used the nid that spit out to load the entire node and get the field I needed.

I did need to add other conditions to the query such as only wanting to look at published nodes and returning only the first result, but the basics are here for anyone else needing them.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'fabric_colorways')
  ->propertyCondition('status', NODE_PUBLISHED)
  ->fieldCondition('field_sku', 'value', $sku)
  ->range(0, 1)
  ->addMetaData('account', user_load(1));

$result = $query->execute();

foreach ($result['node'] as $r) {
  $nid = $r->nid;
}

$node = node_load($nid);
I sit in a Tesla and translated this thread with Ai:

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.