forked from stuartduff/woocommerce-dynamic-pricing-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-dynamic-pricing-table.php
293 lines (233 loc) · 9.25 KB
/
woocommerce-dynamic-pricing-table.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* Plugin Name: WooCommerce Dynamic Pricing Table
* Plugin URI: https://github.com/stuartduff/woocommerce-dynamic-pricing-table
* Description: Adds a pricing discount table to WooCommerce products that are offering bulk discounts or special offer discounts via the WooCommerce Dynamic Pricing plugin.
* Version: 1.0.0
* Author: Stuart Duff
* Author URI: http://stuartduff.com
* Requires at least: 4.5.0
* Tested up to: 4.6.0
*
* Text Domain: woocommerce-dynamic-pricing-table
* Domain Path: /languages/
*
* @package WC_Dynamic_Pricing_Table
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Returns the main instance of WC_Dynamic_Pricing_Table to prevent the need to use globals.
*
* @since 1.0.0
* @return object WC_Dynamic_Pricing_Table
*/
function WC_Dynamic_Pricing_Table() {
return WC_Dynamic_Pricing_Table::instance();
} // End WC_Dynamic_Pricing_Table()
WC_Dynamic_Pricing_Table();
/**
* Main WC_Dynamic_Pricing_Table Class
*
* @class WC_Dynamic_Pricing_Table
* @version 1.0.0
* @since 1.0.0
* @package WC_Dynamic_Pricing_Table
*/
final class WC_Dynamic_Pricing_Table {
/**
* WC_Dynamic_Pricing_Table The single instance of WC_Dynamic_Pricing_Table.
* @var object
* @access private
* @since 1.0.0
*/
private static $_instance = null;
/**
* The token.
* @var string
* @access public
* @since 1.0.0
*/
public $token;
/**
* The version number.
* @var string
* @access public
* @since 1.0.0
*/
public $version;
/**
* Constructor function.
* @access public
* @since 1.0.0
* @return void
*/
public function __construct() {
$this->token = 'woocommerce-dynamic-pricing-table';
$this->plugin_url = plugin_dir_url( __FILE__ );
$this->plugin_path = plugin_dir_path( __FILE__ );
$this->version = '1.0.0';
register_activation_hook( __FILE__, array( $this, 'install' ) );
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
add_action( 'init', array( $this, 'plugin_setup' ) );
}
/**
* Main WC_Dynamic_Pricing_Table Instance
*
* Ensures only one instance of WC_Dynamic_Pricing_Table is loaded or can be loaded.
*
* @since 1.0.0
* @static
* @see WC_Dynamic_Pricing_Table()
* @return Main WC_Dynamic_Pricing_Table instance
*/
public static function instance() {
if ( is_null( self::$_instance ) )
self::$_instance = new self();
return self::$_instance;
} // End instance()
/**
* Load the localisation file.
* @access public
* @since 1.0.0
* @return void
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'woocommerce-dynamic-pricing-table', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Installation.
* Runs on activation. Logs the version number.
* @access public
* @since 1.0.0
* @return void
*/
public function install() {
$this->log_plugin_version_number();
}
/**
* Log the plugin version number.
* @access private
* @since 1.0.0
* @return void
*/
private function log_plugin_version_number() {
// Log the version number.
update_option( $this->token . '-version', $this->version );
}
/**
* Setup all the things.
* Only executes if WooCommerce Dynamic Pricing is active.
* If WooCommerce Dynamic Pricing is inactive an admin notice is displayed.
* @return void
*/
public function plugin_setup() {
if ( class_exists( 'WC_Dynamic_Pricing' ) ) {
add_action( 'woocommerce_before_add_to_cart_button', array( $this, 'output_dynamic_pricing_table' ) );
} else {
add_action( 'admin_notices', array( $this, 'install_wc_dynamic_pricing_notice' ) );
}
}
/**
* WooCommerce Dynamic Pricing plugin install notice.
* If the user activates this plugin while not having the WooCommerce Dynamic Pricing plugin installed or activated, prompt them to install WooCommerce Dynamic Pricing.
* @since 1.0.0
* @return void
*/
public function install_wc_dynamic_pricing_notice() {
echo '<div class="notice is-dismissible updated">
<p>' . __( 'The WooCommerce Dynamic Pricing Table extension requires that you have the WooCommerce Dynamic Pricing plugin installed and activated.', 'woocommerce-dynamic-pricing-table' ) . ' <a href="https://www.woothemes.com/products/dynamic-pricing/">' . __( 'Get WooCommerce Dynamic Pricing now', 'woocommerce-dynamic-pricing-table' ) . '</a></p>
</div>';
}
/**
* Gets the dynamic pricing rules sets from the post meta.
* @access public
* @since 1.0.0
* @return get_post_meta()
*/
public function get_pricing_array_rule_sets() {
return get_post_meta( get_the_ID(), '_pricing_rules', true );
}
/**
* Outputs the dynamic bulk pricing table.
* @access public
* @since 1.0.0
* @return $output
*/
public function bulk_pricing_table_output() {
$array_rule_sets = $this->get_pricing_array_rule_sets();
$output = '<table class="dynamic-pricing-table">';
$output .= '<th>' . __( 'Quantity' , 'woocommerce-dynamic-pricing-table' ) . '</th><th>' . __( 'Price' , 'woocommerce-dynamic-pricing-table' ) . '</th>';
foreach( $array_rule_sets as $pricing_rule_sets ) {
foreach ( $pricing_rule_sets['rules'] as $key => $value ) {
$second_half = ' - ' . number_format ( intval( $pricing_rule_sets['rules'][$key]['to'] ) );
if ( intval( $pricing_rule_sets['rules'][$key]['to'] ) == 0 ) {
$second_half = ' or more';
}
$output .= '<tr>';
$output .= '<td><span class="discount-quantity">' . number_format ( intval( $pricing_rule_sets['rules'][$key]['from'] ) ) . $second_half . '</span></td>';
switch ( $pricing_rule_sets['rules'][$key]['type'] ) {
case 'price_discount':
$output .= '<td><span class="discount-amount">' . get_woocommerce_currency_symbol() . sprintf( __( '%1$s Discount Per Item', 'woocommerce-dynamic-pricing-table' ), floatval( $pricing_rule_sets['rules'][$key]['amount'] ) ) . '</span></td>';
break;
case 'percentage_discount':
$output .= '<td><span class="discount-amount">' . intval( $pricing_rule_sets['rules'][$key]['amount'] ) . __( '% Discount', 'woocommerce-dynamic-pricing-table' ) . '</span></td>';
break;
case 'fixed_price':
$output .= '<td><span class="discount-amount">' . get_woocommerce_currency_symbol() . sprintf( __( '%1$.2f each', 'woocommerce-dynamic-pricing-table' ), floatval( $pricing_rule_sets['rules'][$key]['amount'] ) ) . '</span></td>';
break;
}
$output .= '</tr>';
}
}
$output .= '</table>';
echo $output;
}
/**
* Outputs the dynamic special offer pricing table.
* @access public
* @since 1.0.0
* @return $output
*/
public function special_offer_pricing_table_output() {
$array_rule_sets = $this->get_pricing_array_rule_sets();
$output = '<table class="dynamic-pricing-table">';
$output .= '<th>' . __( 'Quantity', 'woocommerce-dynamic-pricing-table' ) . '</th><th>' . __( 'Special Offer Pricing', 'woocommerce-dynamic-pricing-table' ) . '</th>';
foreach( $array_rule_sets as $pricing_rule_sets ) {
foreach ( $pricing_rule_sets['blockrules'] as $key => $value ) {
$output .= '<tr>';
$output .= '<td><span class="discount-quantity">' . sprintf( __( 'Buy %1$s get %2$s more discounted', 'woocommerce-dynamic-pricing-table' ), intval( $pricing_rule_sets['blockrules'][$key]['from'] ) , intval( $pricing_rule_sets['blockrules'][$key]['adjust'] ) ) . '</span></td>';
switch ( $pricing_rule_sets['blockrules'][$key]['type'] ) {
case 'fixed_adjustment':
$output .= '<td><span class="discount-amount">' . get_woocommerce_currency_symbol() . sprintf( __( '%1$s Discount Per Item', 'woocommerce-dynamic-pricing-table' ), intval( $pricing_rule_sets['blockrules'][$key]['amount'] ) ) . '</span></td>';
break;
case 'percent_adjustment':
$output .= '<td><span class="discount-amount">' . intval( $pricing_rule_sets['blockrules'][$key]['amount'] ) . __( '% Discount', 'woocommerce-dynamic-pricing-table' ) . '</span></td>';
break;
case 'fixed_price':
$output .= '<td><span class="discount-amount">' . get_woocommerce_currency_symbol() . sprintf( __( '%1$s Price Per Item', 'woocommerce-dynamic-pricing-table' ), intval( $pricing_rule_sets['blockrules'][$key]['amount'] ) ) . '</span></td>';
break;
}
$output .= '</tr>';
}
}
$output .= '</table>';
echo $output;
}
/**
* Outputs the dynamic pricing table.
* @access public
* @since 1.0.0
*/
public function output_dynamic_pricing_table() {
$array_rule_sets = $this->get_pricing_array_rule_sets();
if ( $array_rule_sets && is_array( $array_rule_sets ) && sizeof( $array_rule_sets ) == 1 ) {
foreach( $array_rule_sets as $pricing_rule_sets ) {
if ( $pricing_rule_sets['mode'] == 'continuous' ) :
$this->bulk_pricing_table_output();
elseif ( $pricing_rule_sets['mode'] == 'block' ) :
$this->special_offer_pricing_table_output();
endif;
}
}
}
} // End Class