Skip to content

Instantly share code, notes, and snippets.

@BBGuy
Last active November 16, 2022 14:08
Show Gist options
  • Save BBGuy/3e1f624eaee2088d8eee to your computer and use it in GitHub Desktop.
Save BBGuy/3e1f624eaee2088d8eee to your computer and use it in GitHub Desktop.
Working with Drupal commerce entities using entity_metadata_wrapper. Just a bunch of exampled copied from the web or added by me. will keep adding over time. now more then just commerce entities
<?php
// order.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// Order id.
$order_id = order_wrapper->order_id->value();
// User detils.
$uid = $order->uid;
$email = $order->mail;
// order total object
$order_total_obj = $order_wrapper->commerce_order_total->value();
// Order total (cents).
$order_total = $order_total_obj['amount'];
// order currency code
$currency_code = $order_wrapper->commerce_order_total->currency_code->value();
// subtotal without vat
$total_ex_vat = commerce_price_component_total($order_total , 'base_price');
// total tax (in this case vat 20%)
$total_vat = commerce_price_component_total($order_total , 'tax|vat_20');
// subtotal with vat
$sub_total = $total_ex_vat['amount'] + $total_vat['amount'];
// formatted price string with currency sign
$sub_total = commerce_currency_format($sub_total, $currency_code);
// Order state
$order_status = commerce_order_status_load($order->status);
$order_state = $order_status['state'];
if ($order_state != 'cart') {
....
}
// Get the customer billing profile (this is not the wrapper object for the wrapper object don't use the ->value())
$customer_billing = $order_wrapper->commerce_customer_billing->value();
// Make sure it exists
if (isset($customer_billing)) {
// Get the customer billing profile wrapper
$customer_billing_wrapper = $order_wrapper->commerce_customer_billing;
// Get the address
$address = $customer_billing_wrapper->commerce_customer_address->value();
// Get organisation name if available.
if (isset($address['organisation_name'])) {
$organisation_name = $address['organisation_name'];
}
}
// Line item
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
// get all line item wrappers from order wrapper
foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
// line item type
$line_item_type = $line_item_wrapper->type->value();
// line item type using the bundle.
$line_item_type = $line_item_wrapper->getBundle();
// check if a product.
if (in_array($line_item_type, commerce_product_line_item_types())) {
// line item title
$line_item_title = $line_item_wrapper->commerce_product->title->value();
// line item unit price
$line_item_unit_price = $line_item_wrapper->commerce_unit_price->amount->value();
// line item formatted unit price
$line_item_unit_price = commerce_currency_format($line_item_unit_price, $currency_code);
// line item quantity
$line_item_quantity = (float)$line_item_wrapper->quantity->value();
// line item total price
$line_item_total_price = $line_item_wrapper->commerce_total->amount->value();
// line item formatted total price
$line_iten_total_price = commerce_currency_format($line_item_total_price, $currency_code);
// Product
// Product ID.
$product_id = $line_item_wrapper->commerce_product->product_id->value();
// Product SKU.
$sku = $line_item_wrapper->commerce_product->sku->value();
// Product image
$line_item_wrapper->commerce_product->field_product_image->value();
// Load the product.
$product = commerce_product_load($product_id);
} else {
// for shipping & discount line items
// shipping/discount price
$line_item_price = $line_item_wrapper->commerce_unit_price->amount->value();
// shipping/discount formatted price
$line_item_price = commerce_currency_format($line_item_price, $currency_code);
// line item TYPE display title
$line_ite_type_title = commerce_line_item_type_get_name($line_item_wrapper->type->value());
// for shipping line items
if ($line_item_wrapper->type->value() == "shipping") {
// shipping data
$shipping_data = $line_item_wrapper->value()->data;
// shipping method display title
$line_item_title = $shipping_data['shipping_service']['title'];
}
if ($line_item_wrapper->type->value() == 'commerce_discount') {
// for discount line items
// get discount display title (I suspect there might be a better way than this)
$discount_data = $line_item_wrapper->commerce_unit_price->data->value();
foreach ($discount_data['components'] as $key => $component) {
if (!empty($component['price']['data']['discount_component_title'])) {
$line_item_title = $component['price']['data']['discount_component_title'];
}
}
}
}
}
// Product.
$product_wrapper = entity_metadata_wrapper('commerce_product', $product);
// Product Price.
$price_amount = $product_wrapper>commerce_price->amount->value();
$product_wrapper>commerce_price->amount->set($new_price);
// Stock
$product_wrapper->commerce_stock_override->value()
$product_wrapper->commerce_stock->value()
// Taxonomy – term referenced
// Get the term object.
$term = $product_wrapper->field_availability->value();
// Get the taxonomy term name
$term_name = term->name;
// One liner of the above.
$term_name = $product_wrapper->field_availability->value()->name;
// Taxonomy – term referenced using a term wrapper
$term_wrapper = $product_w->field_availability;
$term_name = $term_wrapper->name->value());
// Get the user from a line item
$order = commerce_order_load($line_item->order_id);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$uid = $order_wrapper->uid->value();
@JCL324
Copy link

JCL324 commented Oct 20, 2015

Took an hour of Googling to find the answer on how to reference an EMW value() when it's an array - you had the answer I was looking for with your $price_amount example on line 146! I knew there was a way, thanks!

@jimshreds
Copy link

thanks for this tax EMW insights. have been stuck for days.

@DaleTrexel
Copy link

Thank you so much for this! I spent way too much time yesterday poking at EntityFieldQuery and EntityMetadataWrapper configurations to try to get order subtotal data. I wish this page had come up much higher in my Google searches at the time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment