Tax Inclusive Order Summary Markup
Displays the order summary with tax inclusive calculations.
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Code
Filename: gpecf-tax-inclusive-order-summary-markup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Gravity Perks // GP eCommerce Fields // Tax Inclusive Order Summary Markup
* https://gravitywiz.com/documentation/gravity-forms-ecommerce-fields/
*
* Displays the order summary with tax inclusive calculations.
*
* Plugin Name: GP eCommerce Fields - Tax Inclusive Order Summary Markup
* Plugin URI: https://gravitywiz.com/documentation/gravity-forms-ecommerce-fields/
* Description: Displays the order summary with tax inclusive calculations.
* Author: Gravity Wiz
* Version: 0.1
* Author URI: https://gravitywiz.com
*/
class GW_Tax_Inclusive_Order_Summary {
private $_args = array();
public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'tax_label' => '',
'tax_rate' => 0,
'apply_to_all_forms' => false,
) );
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
if ( ! function_exists( 'gp_ecommerce_fields' ) ) {
return;
}
add_filter( 'gpecf_order_summary', array( $this, 'insert_custom_tax' ), 10, 3 );
add_filter( 'gpecf_order_sumary_markup', array( $this, 'get_custom_order_summary' ), 10, 6 );
}
function insert_custom_tax( $order_summary, $form, $entry ) {
if ( ! $this->is_applicable_form( $form ) ) {
return $order_summary;
}
$tax_incl['tax_incl'] = array(
array(
'name' => $this->_args['tax_label'],
'price' => 0,
'quantity' => 1,
'class' => 'incltax',
),
);
array_splice( $order_summary, 1, 0, $tax_incl );
return $order_summary;
}
function get_custom_order_summary( $markup, $order, $form, $entry, $order_summary, $labels ) {
if ( ! $this->is_applicable_form( $form ) ) {
return $markup;
}
ob_start();
?>
<table class="gpecf-order-summary" cellspacing="0" width="100%" style="<?php gp_ecommerce_fields()->style( '.order-summary' ); ?>">
<thead>
<tr>
<th scope="col" style="<?php gp_ecommerce_fields()->style( '.order-summary/thead/th.column-1' ); ?>"><?php echo $labels['product']; ?></th>
<th scope="col" style="<?php gp_ecommerce_fields()->style( '.order-summary/thead/th.column-2' ); ?>"><?php echo $labels['quantity']; ?></th>
<th scope="col" style="<?php gp_ecommerce_fields()->style( '.order-summary/thead/th.column-3' ); ?>"><?php echo $labels['unit_price']; ?></th>
<th scope="col" style="<?php gp_ecommerce_fields()->style( '.order-summary/thead/th.column-4' ); ?>"><?php echo $labels['price']; ?></th>
</tr>
</thead>
<tbody>
<?php
$tax = 0;
$total_excl_tax = 0;
foreach ( $order['products'] as $product ) :
if ( empty( $product['name'] ) || gp_ecommerce_fields()->is_ecommerce_product( $product ) ) {
continue;
}
?>
<tr style="<?php gp_ecommerce_fields()->style( '.order-summary/tbody/tr' ); ?>">
<td style="<?php gp_ecommerce_fields()->style( '.order-summary/tbody/tr/td.column-1' ); ?>">
<div style="<?php gp_ecommerce_fields()->style( '.order-summary/.custom-product' ); ?>">
<?php echo esc_html( $product['name'] ); ?>
</div>
<ul style="<?php gp_ecommerce_fields()->style( '.order-summary/.product-options' ); ?>">
<?php
$price = GFCommon::to_number( $product['price'] );
if ( is_array( rgar( $product, 'options' ) ) ) :
foreach ( $product['options'] as $index => $option ) :
$price += GFCommon::to_number( $option['price'] );
$class = $index == count( $product['options'] ) - 1 ? '.last-child' : '';
?>
<li style="<?php gp_ecommerce_fields()->style( ".order-summary/.product-options/li{$class}" ); ?>"><?php echo $option['option_label']; ?></li>
<?php
endforeach;
endif;
$price_excl_tax = ( 100 * $price ) / ( 100 + $this->_args['tax_rate'] );
$field_total = floatval( $product['quantity'] ) * $price_excl_tax;
$tax += ( ( $price * $this->_args['tax_rate'] ) / ( 100 + $this->_args['tax_rate'] ) ) * floatval( $product['quantity'] );
$total_excl_tax += $field_total;
?>
</ul>
</td>
<td style="<?php gp_ecommerce_fields()->style( '.order-summary/tbody/tr/td.column-2' ); ?>"><?php echo esc_html( $product['quantity'] ); ?></td>
<td style="<?php gp_ecommerce_fields()->style( '.order-summary/tbody/tr/td.column-3' ); ?>"><?php echo GFCommon::to_money( $price_excl_tax, $entry['currency'] ); ?></td>
<td style="<?php gp_ecommerce_fields()->style( '.order-summary/tbody/tr/td.column-4' ); ?>"><?php echo GFCommon::to_money( $field_total, $entry['currency'] ); ?></td>
</tr>
<?php
endforeach;
?>
</tbody>
<tfoot style="<?php gp_ecommerce_fields()->style( '.order-summary/tfoot' ); ?>">
<?php foreach ( gp_ecommerce_fields()->get_order_summary( $order, $form, $entry ) as $index => $group ) : ?>
<?php
foreach ( $group as $item ) :
$class = rgar( $item, 'class' ) ? '.' . rgar( $item, 'class' ) : '';
?>
<tr style="<?php gp_ecommerce_fields()->style( '.order-summary/tfoot/tr' . $class ); ?>">
<?php if ( $index === 0 ) : ?>
<td style="<?php gp_ecommerce_fields()->style( '.order-summary/tfoot/tr/td.empty' ); ?>" colspan="2" rowspan="<?php echo gp_ecommerce_fields()->get_order_summary_item_count( $order_summary ); ?>"></td>
<?php endif; ?>
<td style="<?php gp_ecommerce_fields()->style( ".order-summary/tfoot/{$class}/td.column-3" ); ?>"><?php echo $item['name']; ?></td>
<td style="<?php gp_ecommerce_fields()->style( ".order-summary/tfoot/{$class}/td.column-4" ); ?>">
<?php
if ( $item['name'] == 'Subtotal' || $item['class'] == 'subtotal' ) {
$item['price'] = $total_excl_tax;
echo GFCommon::to_money( $item['price'], $entry['currency'] );
} elseif ( $item['class'] == 'incltax' ) {
echo GFCommon::to_money( $tax, $entry['currency'] );
} else {
echo GFCommon::to_money( $item['price'], $entry['currency'] );
}
?>
</td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</tfoot>
</table>
<?php
return ob_get_clean();
}
function is_applicable_form( $form ) {
if ( $this->_args['apply_to_all_forms'] == true ) {
return true;
}
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['form_id'] ) || (int) $form_id === (int) $this->_args['form_id'];
}
}
# Configuration
new GW_Tax_Inclusive_Order_Summary( array(
// ID of the form to use this customized order summary markup on.
'form_id' => 123,
// Custom tax label to show on the order summary.
'tax_label' => 'Sale Tax (20%)',
// Tax percentage amount.
'tax_rate' => 20,
// Set this to true to apply this customized order summary markup to all forms.
'apply_to_all_forms' => false,
) );