Skip to content

Instantly share code, notes, and snippets.

@andyg5000
Created December 3, 2014 15:55
Show Gist options
  • Save andyg5000/49042452d7d5abd05a32 to your computer and use it in GitHub Desktop.
Save andyg5000/49042452d7d5abd05a32 to your computer and use it in GitHub Desktop.
Loads an array of product ids from a commerce_shipment entity using the product entity reference field on the shipment
diff --git a/commerce_shipment.module b/commerce_shipment.module
index 8feda95..c950925 100644
--- a/commerce_shipment.module
+++ b/commerce_shipment.module
@@ -445,3 +445,50 @@ function commerce_shipment_commerce_multichannel_migration_info() {
return $items;
}
+
+/**
+ * Helper function to return product id list from shipments on an order.
+ */
+function commerce_shipment_product_id_list_from_order_shipments($order) {
+ $shipments = array();
+ if (!empty($order->commerce_shipments)) {
+ $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
+ $shipments = $order_wrapper->commerce_shipments->value();
+ }
+ return commerce_shipment_product_id_list_from_shipment($shipments);
+}
+
+/**
+ * Returns a non-unique array of product ids for each shipment.
+ */
+function commerce_shipment_product_id_list_from_shipment($shipments) {
+ $product_ids = array();
+ $field_name = commerce_shipment_product_reference_field();
+ foreach ($shipments as $shipment) {
+ if (!empty($shipment->{$field_name})) {
+ $shipment_wrapper = entity_metadata_wrapper('commerce_shipment', $shipment);
+ foreach ($shipment_wrapper->{$field_name}->value() as $product) {
+ $product_ids[] = $product->product_id;
+ }
+ }
+ }
+ return $product_ids;
+}
+
+/**
+ * Defines the product reference field on shipments.
+ */
+function commerce_shipment_product_reference_field() {
+ $field_name = 'field_shipment_products';
+ drupal_alter('commerce_shipment_product_reference_field', $field_name);
+ return $field_name;
+}
+
+/**
+ * Defines the product reference field on shipments.
+ */
+function commerce_shipment_box_reference_field() {
+ $field_name = 'field_box';
+ drupal_alter('commerce_shipment_box_reference_field', $field_name);
+ return $field_name;
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment