class OrderItemsReport in Commerce Reporting 8
Provides the Order Items Report.
Plugin annotation
@CommerceReportType(
id = "order_items_report",
label = @Translation("Order Items Report"),
description = @Translation("Order items.")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\commerce_reports\Plugin\Commerce\ReportType\ReportTypeBase implements ReportTypeInterface, ContainerFactoryPluginInterface
- class \Drupal\commerce_reports\Plugin\Commerce\ReportType\OrderItemsReport
- class \Drupal\commerce_reports\Plugin\Commerce\ReportType\ReportTypeBase implements ReportTypeInterface, ContainerFactoryPluginInterface
Expanded class hierarchy of OrderItemsReport
File
- src/
Plugin/ Commerce/ ReportType/ OrderItemsReport.php, line 19
Namespace
Drupal\commerce_reports\Plugin\Commerce\ReportTypeView source
class OrderItemsReport extends ReportTypeBase {
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = [];
$fields['order_item_type_id'] = BundleFieldDefinition::create('entity_reference')
->setLabel(t('Order item type'))
->setDescription(t('The order item type.'))
->setSetting('target_type', 'commerce_order_item_type')
->setRequired(TRUE);
$fields['order_item_id'] = BundleFieldDefinition::create('entity_reference')
->setLabel(t('Order item'))
->setDescription(t('The parent order item.'))
->setSetting('target_type', 'commerce_order_item')
->setRequired(TRUE);
$fields['title'] = BundleFieldDefinition::create('string')
->setLabel(t('Title'))
->setDescription(t('The purchased item title.'))
->setSettings([
'default_value' => '',
'max_length' => 512,
]);
$fields['quantity'] = BundleFieldDefinition::create('decimal')
->setLabel(t('Quantity'))
->setDescription(t('The number of purchased units.'))
->setRequired(TRUE)
->setSetting('unsigned', TRUE)
->setSetting('min', 0)
->setDisplayConfigurable('view', TRUE);
$fields['unit_price'] = BundleFieldDefinition::create('commerce_price')
->setLabel(t('Unit price'))
->setDescription(t('The unit price for the purchased item'))
->setCardinality(1)
->setRequired(TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['total_price'] = BundleFieldDefinition::create('commerce_price')
->setLabel(t('Total price'))
->setDescription(t('The total price for the purchased item'))
->setCardinality(1)
->setRequired(TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['adjusted_unit_price'] = BundleFieldDefinition::create('commerce_price')
->setLabel(t('Adjusted unit price'))
->setDescription(t('The adjusted unit price for the purchased item'))
->setCardinality(1)
->setRequired(TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['adjusted_total_price'] = BundleFieldDefinition::create('commerce_price')
->setLabel(t('Adjusted total price'))
->setDescription(t('The adjusted total price for the purchased item'))
->setCardinality(1)
->setRequired(TRUE)
->setDisplayConfigurable('view', TRUE);
return $fields;
}
/**
* {@inheritdoc}
*/
protected function doBuildReportTableHeaders() {
return [
'formatted_date' => t('Date'),
'order_id_count' => t('# Orders'),
'quantity_sum' => t('# Sold'),
'adjusted_unit_price_sum' => t('Total revenue'),
'adjusted_total_price_sum' => t('Average revenue'),
'unit_price_currency_code' => t('Currency'),
];
}
/**
* {@inheritdoc}
*/
protected function doBuildReportTableRow(array $result) {
$currency_code = $result['unit_price_currency_code'];
$row = [
$result['formatted_date'],
$result['order_id_count'],
$result['quantity_sum'],
[
'data' => [
'#type' => 'inline_template',
'#template' => '{{price|commerce_price_format}}',
'#context' => [
'price' => new Price($result['adjusted_unit_price_sum'], $currency_code),
],
],
],
[
'data' => [
'#type' => 'inline_template',
'#template' => '{{price|commerce_price_format}}',
'#context' => [
'price' => new Price($result['adjusted_total_price_sum'], $currency_code),
],
],
],
$currency_code,
];
return $row;
}
/**
* {@inheritdoc}
*/
public function generateReports(OrderInterface $order) {
foreach ($order
->getItems() as $order_item) {
$values = [
'order_item_type_id' => $order_item
->bundle(),
'order_item_id' => $order_item
->id(),
'title' => $order_item
->label(),
'quantity' => $order_item
->getQuantity(),
'unit_price' => $order_item
->getUnitPrice(),
'total_price' => $order_item
->getTotalPrice(),
'adjusted_unit_price' => $order_item
->getAdjustedUnitPrice(),
'adjusted_total_price' => $order_item
->getAdjustedTotalPrice(),
];
$this
->createFromOrder($order, $values);
}
}
/**
* {@inheritdoc}
*/
public function buildQuery(QueryAggregateInterface $query) {
$query
->aggregate('title', 'COUNT');
$query
->aggregate('quantity', 'SUM');
$query
->aggregate('unit_price.number', 'SUM');
$query
->aggregate('total_price.number', 'SUM');
$query
->aggregate('adjusted_unit_price.number', 'SUM');
$query
->aggregate('adjusted_total_price.number', 'SUM');
$query
->groupBy('title');
$query
->groupBy('unit_price.currency_code');
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OrderItemsReport:: |
public | function |
Builds the field definitions for entities of this bundle. Overrides BundlePluginInterface:: |
|
OrderItemsReport:: |
public | function |
Builds the aggregate query. Overrides ReportTypeInterface:: |
|
OrderItemsReport:: |
protected | function |
Builds the report table headers. Overrides ReportTypeBase:: |
|
OrderItemsReport:: |
protected | function |
Build the report table row. Overrides ReportTypeBase:: |
|
OrderItemsReport:: |
public | function |
Generates order reports for an order. Overrides ReportTypeInterface:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ReportTypeBase:: |
protected | property | The order report storage. | |
ReportTypeBase:: |
public | function |
Build a report table from query results. Overrides ReportTypeInterface:: |
|
ReportTypeBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
2 |
ReportTypeBase:: |
public | function |
Creates a new order report using the given order and values. Overrides ReportTypeInterface:: |
|
ReportTypeBase:: |
public | function |
Gets the order report type description. Overrides ReportTypeInterface:: |
|
ReportTypeBase:: |
public | function |
Gets the order report type label. Overrides ReportTypeInterface:: |
|
ReportTypeBase:: |
public | function |
Constructs a new ReportTypeBase object. Overrides PluginBase:: |
2 |