WC_Report_Coupon_Usage::get_main_chart()publicWC 1.0

Get the main chart.

Method of the class: WC_Report_Coupon_Usage{}

No Hooks.

Return

null. Nothing (null).

Usage

$WC_Report_Coupon_Usage = new WC_Report_Coupon_Usage();
$WC_Report_Coupon_Usage->get_main_chart();

WC_Report_Coupon_Usage::get_main_chart() code WC 8.7.0

<?php
public function get_main_chart() {
	global $wp_locale;

	// Get orders and dates in range - we want the SUM of order totals, COUNT of order items, COUNT of orders, and the date.
	$order_coupon_counts_query = array(
		'data'         => array(
			'order_item_name' => array(
				'type'            => 'order_item',
				'order_item_type' => 'coupon',
				'function'        => 'COUNT',
				'name'            => 'order_coupon_count',
			),
			'post_date'       => array(
				'type'     => 'post_data',
				'function' => '',
				'name'     => 'post_date',
			),
		),
		'where'        => array(
			array(
				'key'      => 'order_item_type',
				'value'    => 'coupon',
				'operator' => '=',
			),
		),
		'group_by'     => $this->group_by_query,
		'order_by'     => 'post_date ASC',
		'query_type'   => 'get_results',
		'filter_range' => true,
		'order_types'  => wc_get_order_types( 'order-count' ),
	);

	$order_discount_amounts_query = array(
		'data'         => array(
			'discount_amount' => array(
				'type'            => 'order_item_meta',
				'order_item_type' => 'coupon',
				'function'        => 'SUM',
				'name'            => 'discount_amount',
			),
			'post_date'       => array(
				'type'     => 'post_data',
				'function' => '',
				'name'     => 'post_date',
			),
		),
		'where'        => array(
			array(
				'key'      => 'order_item_type',
				'value'    => 'coupon',
				'operator' => '=',
			),
		),
		'group_by'     => $this->group_by_query . ', order_item_name',
		'order_by'     => 'post_date ASC',
		'query_type'   => 'get_results',
		'filter_range' => true,
		'order_types'  => wc_get_order_types( 'order-count' ),
	);

	if ( ! empty( $this->coupon_codes ) ) {
		$coupon_code_query = array(
			'type'     => 'order_item',
			'key'      => 'order_item_name',
			'value'    => $this->coupon_codes,
			'operator' => 'IN',
		);

		$order_coupon_counts_query['where'][]    = $coupon_code_query;
		$order_discount_amounts_query['where'][] = $coupon_code_query;
	}

	$order_coupon_counts    = $this->get_order_report_data( $order_coupon_counts_query );
	$order_discount_amounts = $this->get_order_report_data( $order_discount_amounts_query );

	// Prepare data for report.
	$order_coupon_counts    = $this->prepare_chart_data( $order_coupon_counts, 'post_date', 'order_coupon_count', $this->chart_interval, $this->start_date, $this->chart_groupby );
	$order_discount_amounts = $this->prepare_chart_data( $order_discount_amounts, 'post_date', 'discount_amount', $this->chart_interval, $this->start_date, $this->chart_groupby );

	// Encode in json format.
	$chart_data = wp_json_encode(
		array(
			'order_coupon_counts'    => array_values( $order_coupon_counts ),
			'order_discount_amounts' => array_values( $order_discount_amounts ),
		)
	);
	?>
	<div class="chart-container">
		<div class="chart-placeholder main"></div>
	</div>
	<script type="text/javascript">
		var main_chart;

		jQuery(function(){
			var order_data = JSON.parse( decodeURIComponent( '<?php echo rawurlencode( $chart_data ); ?>' ) );

			var drawGraph = function( highlight ) {
				var series = [
					{
						label: "<?php echo esc_js( __( 'Number of coupons used', 'woocommerce' ) ); ?>",
						data: order_data.order_coupon_counts,
						color: '<?php echo esc_js( $this->chart_colours['coupon_count'] ); ?>',
						bars: { fillColor: '<?php echo esc_js( $this->chart_colours['coupon_count'] ); ?>', fill: true, show: true, lineWidth: 0, barWidth: <?php echo esc_js( $this->barwidth ); ?> * 0.5, align: 'center' },
						shadowSize: 0,
						hoverable: false
					},
					{
						label: "<?php echo esc_js( __( 'Discount amount', 'woocommerce' ) ); ?>",
						data: order_data.order_discount_amounts,
						yaxis: 2,
						color: '<?php echo esc_js( $this->chart_colours['discount_amount'] ); ?>',
						points: { show: true, radius: 5, lineWidth: 3, fillColor: '#fff', fill: true },
						lines: { show: true, lineWidth: 4, fill: false },
						shadowSize: 0,
						<?php echo $this->get_currency_tooltip(); ?><?php // @codingStandardsIgnoreLine ?>
					}
				];

				if ( highlight !== 'undefined' && series[ highlight ] ) {
					highlight_series = series[ highlight ];

					highlight_series.color = '#9c5d90';

					if ( highlight_series.bars )
						highlight_series.bars.fillColor = '#9c5d90';

					if ( highlight_series.lines ) {
						highlight_series.lines.lineWidth = 5;
					}
				}

				main_chart = jQuery.plot(
					jQuery('.chart-placeholder.main'),
					series,
					{
						legend: {
							show: false
						},
						grid: {
							color: '#aaa',
							borderColor: 'transparent',
							borderWidth: 0,
							hoverable: true
						},
						xaxes: [ {
							color: '#aaa',
							position: "bottom",
							tickColor: 'transparent',
							mode: "time",
							timeformat: "<?php echo ( 'day' === $this->chart_groupby ) ? '%d %b' : '%b'; ?>",
							monthNames: JSON.parse( decodeURIComponent( '<?php echo rawurlencode( wp_json_encode( array_values( $wp_locale->month_abbrev ) ) ); ?>' ) ),
							tickLength: 1,
							minTickSize: [1, "<?php echo esc_js( $this->chart_groupby ); ?>"],
							font: {
								color: "#aaa"
							}
						} ],
						yaxes: [
							{
								min: 0,
								minTickSize: 1,
								tickDecimals: 0,
								color: '#ecf0f1',
								font: { color: "#aaa" }
							},
							{
								position: "right",
								min: 0,
								tickDecimals: 2,
								alignTicksWithAxis: 1,
								color: 'transparent',
								font: { color: "#aaa" }
							}
						],
					}
				);

				jQuery('.chart-placeholder').trigger( 'resize' );
			}

			drawGraph();

			jQuery('.highlight_series').on( 'mouseenter',
				function() {
					drawGraph( jQuery(this).data('series') );
				} ).on( 'mouseleave',
				function() {
					drawGraph();
				}
			);
		});
	</script>
	<?php
}