0

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

4
  • 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? Aug 31, 2023 at 11:35
  • 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.
    – jsank95
    Aug 31, 2023 at 12:56
  • 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: drupal.stackexchange.com/questions/293885/… - replace the entity type with commerce_product and replace the field name with your actual entity reference field name. Aug 31, 2023 at 13:22
  • That might work for Drupal 8 but in Drupal 7 it just produces an error.
    – jsank95
    Aug 31, 2023 at 14:07

1 Answer 1

0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.