Last active
August 27, 2019 10:02
-
-
Save St0iK/64f66c0f2cc5f24a361f to your computer and use it in GitHub Desktop.
Useful Drupal Commerce line item wrapper methods & properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$wrapper = entity_metadata_wrapper('commerce_order', $order); | |
// if(isset($wrapper->commerce_customer_shipping->commerce_customer_address)){ | |
$commerce_order_total->currency_code->value(); | |
// order total object | |
$order_total = $order_wrapper->commerce_order_total->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); | |
/** | |
* LINE ITEM WRAPPER | |
*/ | |
$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(); | |
// for product line items: | |
if ($line_item_type == 'product') { | |
// 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); | |
// get product field data (for example field_product_image) | |
$line_item_wrapper->commerce_product->field_product_image->value(); | |
} 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']; | |
} | |
} | |
} | |
} |
You just have everything I was looking for, Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@St0iK
Nice! :-)