WooCommerce Bulk Discount - Version 2.0.11

Version Description

  • (16 Oct 2013) Redesigning input of discounts on Bulk Discount tab to be more user friendly.
  • Minor code formatting changes.
Download this release

Release Info

Developer Rene Puchinger
Plugin Icon 128x128 WooCommerce Bulk Discount
Version 2.0.11
Comparing to
See all releases

Code changes from version 2.0.10 to 2.0.11

Files changed (3) hide show
  1. css/admin.css +8 -0
  2. readme.txt +8 -1
  3. woocommerce-bulk-discount.php +249 -190
css/admin.css CHANGED
@@ -5,3 +5,11 @@
5
  #bulkdiscount_product_data textarea {
6
  width: 60% !important;
7
  }
 
 
 
 
 
 
 
 
5
  #bulkdiscount_product_data textarea {
6
  width: 60% !important;
7
  }
8
+
9
+ #bulkdiscount_product_data .even {
10
+ background-color: rgba(0,0,0,0.1);
11
+ }
12
+
13
+ #bulkdiscount_product_data .odd {
14
+ background-color: rgba(0,0,0,0.0);
15
+ }
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_butt
4
  Tags: woocommerce, bulk, discount
5
  Requires at least: 3.5
6
  Tested up to: 3.6
7
- Stable tag: 2.0.10
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
@@ -135,6 +135,10 @@ Setting bulk discounts couldn't have been easier.
135
 
136
  == Changelog ==
137
 
 
 
 
 
138
  = 2.0.10 =
139
  * (10 Oct 2013) Fixing a bug which might have resulted in collision with few other WooCommerce plugins.
140
 
@@ -206,6 +210,9 @@ Setting bulk discounts couldn't have been easier.
206
 
207
  == Upgrade Notice ==
208
 
 
 
 
209
  = 2.0.10 =
210
  Bugfix release.
211
 
4
  Tags: woocommerce, bulk, discount
5
  Requires at least: 3.5
6
  Tested up to: 3.6
7
+ Stable tag: 2.0.11
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
135
 
136
  == Changelog ==
137
 
138
+ = 2.0.11 =
139
+ * (16 Oct 2013) Redesigning input of discounts on Bulk Discount tab to be more user friendly.
140
+ * Minor code formatting changes.
141
+
142
  = 2.0.10 =
143
  * (10 Oct 2013) Fixing a bug which might have resulted in collision with few other WooCommerce plugins.
144
 
210
 
211
  == Upgrade Notice ==
212
 
213
+ = 2.0.11 =
214
+ More friendly UI.
215
+
216
  = 2.0.10 =
217
  Bugfix release.
218
 
woocommerce-bulk-discount.php CHANGED
@@ -4,11 +4,10 @@ Plugin Name: WooCommerce Bulk Discount
4
  Plugin URI: http://www.tools4me.net/wordpress/woocommerce-bulk-discount-plugin
5
  Description: Apply fine-grained bulk discounts to items in the shopping cart.
6
  Author: Rene Puchinger
7
- Version: 2.0.10
8
  Author URI: http://www.renepuchinger.com
9
  License: GPL3
10
 
11
-
12
  Copyright (C) 2013 Rene Puchinger
13
 
14
  This program is free software: you can redistribute it and/or modify
@@ -26,11 +25,11 @@ License: GPL3
26
 
27
  */
28
 
29
- if (!defined('ABSPATH')) exit; // Exit if accessed directly
30
 
31
- if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) return; // Check if WooCommerce is active
32
 
33
- if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
34
 
35
  class Woo_Bulk_Discount_Plugin_t4m {
36
 
@@ -38,48 +37,48 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
38
 
39
  public function __construct() {
40
 
41
- load_plugin_textdomain('wc_bulk_discount', false, dirname(plugin_basename(__FILE__)) . '/lang/');
42
 
43
- $this->current_tab = (isset($_GET['tab'])) ? $_GET['tab'] : 'general';
44
 
45
  $this->settings_tabs = array(
46
- 'bulk_discount' => __('Bulk Discount', 'wc_bulk_discount')
47
  );
48
 
49
- add_action('admin_enqueue_scripts', array($this, 'action_enqueue_dependencies_admin'));
50
- add_action('wp_head', array($this, 'action_enqueue_dependencies'));
51
 
52
- add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'action_links'));
53
 
54
- add_action('woocommerce_settings_tabs', array($this, 'add_tab'), 10);
55
 
56
  // Run these actions when generating the settings tabs.
57
- foreach ($this->settings_tabs as $name => $label) {
58
- add_action('woocommerce_settings_tabs_' . $name, array($this, 'settings_tab_action'), 10);
59
- add_action('woocommerce_update_options_' . $name, array($this, 'save_settings'), 10);
60
  }
61
 
62
  // Add the settings fields to each tab.
63
- add_action('woocommerce_bulk_discount_settings', array($this, 'add_settings_fields'), 10);
64
-
65
- if (get_option('woocommerce_t4m_enable_bulk_discounts', 'yes') == 'yes') {
66
- add_action('woocommerce_before_calculate_totals', array($this, 'action_before_calculate'), 10, 1);
67
- add_action('woocommerce_calculate_totals', array($this, 'action_after_calculate'), 10, 1);
68
- add_action('woocommerce_before_cart_table', array($this, 'before_cart_table'));
69
- add_action('woocommerce_single_product_summary', array($this, 'single_product_summary'), 45);
70
- if (class_exists('WC_Inline_Javascript_Helper')) { // is WooCommerce 2.1 installed?
71
- add_filter('woocommerce_cart_item_price', array($this, 'filter_item_price'), 10, 2);
72
  } else {
73
- add_filter('woocommerce_cart_item_price_html', array($this, 'filter_item_price'), 10, 2);
74
  }
75
- add_filter('woocommerce_cart_item_subtotal', array($this, 'filter_subtotal_price'), 10, 2);
76
- add_filter('woocommerce_checkout_item_subtotal', array($this, 'filter_subtotal_price'), 10, 2);
77
- add_filter('woocommerce_order_formatted_line_subtotal', array($this, 'filter_subtotal_order_price'), 10, 3);
78
- add_filter('woocommerce_product_write_panel_tabs', array($this, 'action_product_write_panel_tabs'));
79
- add_filter('woocommerce_product_write_panels', array($this, 'action_product_write_panels'));
80
- add_action('woocommerce_process_product_meta', array($this, 'action_process_meta'));
81
- add_filter('woocommerce_cart_product_subtotal', array($this, 'filter_cart_product_subtotal'), 10, 3);
82
- add_action('woocommerce_checkout_update_order_meta', array($this, 'order_update_meta'));
83
  }
84
 
85
  }
@@ -90,13 +89,13 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
90
  * @param $links
91
  * @return array
92
  */
93
- public function action_links($links) {
94
 
95
  $plugin_links = array(
96
- '<a href="' . admin_url('admin.php?page=woocommerce&tab=bulk_discount') . '">' . __('Settings', 'woocommerce') . '</a>',
97
  );
98
 
99
- return array_merge($plugin_links, $links);
100
  }
101
 
102
  /**
@@ -107,24 +106,24 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
107
  * @param $order
108
  * @return float
109
  */
110
- protected function get_discounted_coeff($product_id, $quantity) {
111
  $q = array(0.0);
112
  $d = array(0.0);
113
  /* Find the appropriate discount coefficient by looping through up to the five discount settings */
114
- for ($i = 1; $i <= 5; $i++) {
115
- array_push($q, get_post_meta($product_id, "_bulkdiscount_quantity_$i", true));
116
- if (get_option('woocommerce_t4m_discount_type', '') == 'flat') {
117
- array_push($d, get_post_meta($product_id, "_bulkdiscount_discount_flat_$i", true) ? get_post_meta($product_id, "_bulkdiscount_discount_flat_$i", true) : 0.0);
118
  } else {
119
- array_push($d, get_post_meta($product_id, "_bulkdiscount_discount_$i", true) ? get_post_meta($product_id, "_bulkdiscount_discount_$i", true) : 0.0);
120
  }
121
- if ($quantity >= $q[$i] && $q[$i] > $q[0]) {
122
  $q[0] = $q[$i];
123
  $d[0] = $d[$i];
124
  }
125
  }
126
  // for percentage discount convert the resulting discount from % to the multiplying coefficient
127
- return (get_option('woocommerce_t4m_discount_type', '') == 'flat') ? max(0, $d[0]) : min(1.0, max(0, (100.0 - round($d[0], 2)) / 100.0));
128
  }
129
 
130
  /**
@@ -134,32 +133,32 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
134
  * @param $values
135
  * @return string
136
  */
137
- public function filter_item_price($price, $values) {
138
  global $woocommerce;
139
- if (!$values || @!$values['data']) {
140
  return $price;
141
  }
142
- if ((get_option('woocommerce_t4m_show_on_item', 'yes') == 'no')) {
143
  return $price;
144
  }
145
- if ((get_option('woocommerce_t4m_discount_type', '') == 'flat')) {
146
  return $price; // for flat discount this filter has no meaning
147
  }
148
  $_product = $values['data'];
149
- if (empty($this->discount_coeffs) || !isset($this->discount_coeffs[$this->get_actual_id($_product)])
150
- || !isset($this->discount_coeffs[$this->get_actual_id($_product)]['orig_price']) || !isset($this->discount_coeffs[$this->get_actual_id($_product)]['coeff'])
151
  ) {
152
  $this->gather_discount_coeffs();
153
  }
154
- $coeff = $this->discount_coeffs[$this->get_actual_id($_product)]['coeff'];
155
- if ($coeff == 1.0) {
156
  return $price; // no price modification
157
  }
158
- $discprice = woocommerce_price($_product->get_price() * $coeff);
159
- $oldprice = woocommerce_price($this->discount_coeffs[$this->get_actual_id($_product)]['orig_price']);
160
- $old_css = esc_attr(get_option('woocommerce_t4m_css_old_price', 'color: #777; text-decoration: line-through; margin-right: 4px;'));
161
- $new_css = esc_attr(get_option('woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;'));
162
- return "<span class='discount-info' title='" . sprintf(__('%s%% bulk discount applied!', 'wc_bulk_discount'), round((1.0 - $coeff) * 100.0, 2)) . "'>" .
163
  "<span class='old-price' style='$old_css'>$oldprice</span>" .
164
  "<span class='new-price' style='$new_css'>$discprice</span></span>";
165
  }
@@ -171,28 +170,28 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
171
  * @param $values
172
  * @return string
173
  */
174
- public function filter_subtotal_price($price, $values) {
175
  global $woocommerce;
176
- if (!$values || !$values['data']) {
177
  return $price;
178
  }
179
- if ((get_option('woocommerce_t4m_show_on_subtotal', 'yes') == 'no')) {
180
  return $price;
181
  }
182
  $_product = $values['data'];
183
- if (empty($this->discount_coeffs) || !isset($this->discount_coeffs[$this->get_actual_id($_product)])
184
- || !isset($this->discount_coeffs[$this->get_actual_id($_product)]['orig_price']) || !isset($this->discount_coeffs[$this->get_actual_id($_product)]['coeff'])
185
  ) {
186
  $this->gather_discount_coeffs();
187
  }
188
- $coeff = $this->discount_coeffs[$this->get_actual_id($_product)]['coeff'];
189
- if ((get_option('woocommerce_t4m_discount_type', '') == 'flat' && $coeff == 0) || (get_option('woocommerce_t4m_discount_type', '') == '' && $coeff == 1.0)) {
190
  return $price; // no price modification
191
  }
192
- $oldprice = woocommerce_price($this->discount_coeffs[$this->get_actual_id($_product)]['orig_price']);
193
- $old_css = esc_attr(get_option('woocommerce_t4m_css_old_price', 'color: #777; text-decoration: line-through; margin-right: 4px;'));
194
- $new_css = esc_attr(get_option('woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;'));
195
- $bulk_info = sprintf(__('Incl. %s discount', 'wc_bulk_discount'), (get_option('woocommerce_t4m_discount_type', '') == 'flat' ? get_woocommerce_currency_symbol() . $coeff : (round((1 - $coeff) * 100, 2) . "%")));
196
  return "<span class='discount-info' title='$bulk_info'>" .
197
  "<span>$price</span>" .
198
  "<span class='new-price' style='$new_css'> ($bulk_info)</span></span>";
@@ -205,15 +204,15 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
205
  global $woocommerce;
206
  $cart = $woocommerce->cart;
207
  $this->discount_coeffs = array();
208
- if (sizeof($cart->cart_contents) > 0) {
209
- foreach ($cart->cart_contents as $cart_item_key => $values) {
210
  $_product = $values['data'];
211
  $quantity = 0;
212
- if (get_option('woocommerce_t4m_variations_separate', 'yes') == 'no' && $_product instanceof WC_Product_Variation && $_product->parent) {
213
  $parent = $_product->parent;
214
- foreach ($cart->cart_contents as $valuesInner) {
215
  $p = $valuesInner['data'];
216
- if ($p instanceof WC_Product_Variation && $p->parent && $p->parent->id == $parent->id) {
217
  $quantity += $valuesInner['quantity'];
218
  $this->discount_coeffs[$_product->variation_id]['quantity'] = $quantity;
219
  }
@@ -221,8 +220,8 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
221
  } else {
222
  $quantity = $values['quantity'];
223
  }
224
- $this->discount_coeffs[$this->get_actual_id($_product)]['coeff'] = $this->get_discounted_coeff($_product->id, $quantity);
225
- $this->discount_coeffs[$this->get_actual_id($_product)]['orig_price'] = $_product->get_price();
226
  }
227
  }
228
  }
@@ -234,32 +233,32 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
234
  * @param $values
235
  * @return string
236
  */
237
- public function filter_subtotal_order_price($price, $values, $order) {
238
  global $woocommerce;
239
- if (!$values || !$order) {
240
  return $price;
241
  }
242
- if ((get_option('woocommerce_t4m_show_on_order_subtotal', 'yes') == 'no')) {
243
  return $price;
244
  }
245
- $_product = get_product($values['product_id']);
246
  $actual_id = $values['product_id'];
247
- if ($_product && $_product instanceof WC_Product_Variable && $values['variation_id']) {
248
  $actual_id = $values['variation_id'];
249
  }
250
- $discount_coeffs = $this->gather_discount_coeffs_from_order($order->id);
251
- if (empty($discount_coeffs)) {
252
  return $price;
253
  }
254
  $coeff = $discount_coeffs[$actual_id]['coeff'];
255
- $discount_type = get_post_meta($order->id, '_woocommerce_t4m_discount_type', true);
256
- if (($discount_type == 'flat' && $coeff == 0) || ($discount_type == '' && $coeff == 1.0)) {
257
  return $price; // no price modification
258
  }
259
- $oldprice = woocommerce_price($discount_coeffs[$actual_id]['orig_price']);
260
- $old_css = esc_attr(get_option('woocommerce_t4m_css_old_price', 'color: #777; text-decoration: line-through; margin-right: 4px;'));
261
- $new_css = esc_attr(get_option('woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;'));
262
- $bulk_info = sprintf(__('Incl. %s discount', 'wc_bulk_discount'), ($discount_type == 'flat' ? get_woocommerce_currency_symbol() . $coeff : (round((1 - $coeff) * 100, 2) . "%")));
263
  return "<span class='discount-info' title='$bulk_info'>" .
264
  "<span>$price</span>" .
265
  "<span class='new-price' style='$new_css'> ($bulk_info)</span></span>";
@@ -271,12 +270,12 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
271
  * @param $order_id
272
  * @return array
273
  */
274
- protected function gather_discount_coeffs_from_order($order_id) {
275
- $meta = get_post_meta($order_id, '_woocommerce_t4m_discount_coeffs', true);
276
- if (!$meta) {
277
  return null;
278
  }
279
- $order_discount_coeffs = json_decode($meta, true);
280
  return $order_discount_coeffs;
281
  }
282
 
@@ -285,26 +284,26 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
285
  *
286
  * @param WC_Cart $cart
287
  */
288
- public function action_before_calculate(WC_Cart $cart) {
289
 
290
  $this->gather_discount_coeffs();
291
 
292
- if (sizeof($cart->cart_contents) > 0) {
293
- foreach ($cart->cart_contents as $cart_item_key => $values) {
294
  $_product = $values['data'];
295
- if ((get_option('woocommerce_t4m_discount_type', '') == 'flat')) {
296
- $row_base_price = max(0, $_product->get_price() - ($this->discount_coeffs[$this->get_actual_id($_product)]['coeff'] / $values['quantity']));
297
  } else {
298
- $row_base_price = $_product->get_price() * $this->discount_coeffs[$this->get_actual_id($_product)]['coeff'];
299
  }
300
 
301
- $values['data']->set_price($row_base_price);
302
  }
303
  }
304
  }
305
 
306
- protected function get_actual_id($product) {
307
- if ($product instanceof WC_Product_Variation) {
308
  return $product->variation_id;
309
  } else {
310
  return $product->id;
@@ -316,11 +315,11 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
316
  *
317
  * @param WC_Cart $cart
318
  */
319
- public function action_after_calculate(WC_Cart $cart) {
320
- if (sizeof($cart->cart_contents) > 0) {
321
- foreach ($cart->cart_contents as $cart_item_key => $values) {
322
  $_product = $values['data'];
323
- $values['data']->set_price($this->discount_coeffs[$this->get_actual_id($_product)]['orig_price']);
324
  }
325
  }
326
  }
@@ -330,7 +329,7 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
330
  */
331
  public function before_cart_table() {
332
  echo "<div class='cart-show-discounts'>";
333
- echo get_option('woocommerce_t4m_cart_info');
334
  echo "</div>";
335
  }
336
 
@@ -343,15 +342,15 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
343
  * @param WC_Cart $cart
344
  * @return string
345
  */
346
- public function filter_cart_product_subtotal($subtotal, $_product, $quantity) {
347
- if (!$_product || !$quantity) {
348
  return $subtotal;
349
  }
350
- $coeff = $this->discount_coeffs[$this->get_actual_id($_product)]['coeff'];
351
- if ((get_option('woocommerce_t4m_discount_type', '') == 'flat')) {
352
- $newsubtotal = woocommerce_price(max(0, ($_product->get_price() * $quantity) - $coeff));
353
  } else {
354
- $newsubtotal = woocommerce_price($_product->get_price() * $quantity * $coeff);
355
  }
356
  return $newsubtotal;
357
  }
@@ -361,9 +360,9 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
361
  *
362
  * @param $order_id
363
  */
364
- public function order_update_meta($order_id) {
365
- update_post_meta($order_id, "_woocommerce_t4m_discount_type", get_option('woocommerce_t4m_discount_type', ''));
366
- update_post_meta($order_id, "_woocommerce_t4m_discount_coeffs", json_encode($this->discount_coeffs));
367
  }
368
 
369
  /**
@@ -371,9 +370,9 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
371
  */
372
  public function single_product_summary() {
373
  global $thepostid, $post;
374
- if (!$thepostid) $thepostid = $post->ID;
375
  echo "<div class='productinfo-show-discounts'>";
376
- echo get_post_meta($thepostid, '_bulkdiscount_text_info', true);
377
  echo "</div>";
378
  }
379
 
@@ -381,7 +380,7 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
381
  * Add entry to Product Settings.
382
  */
383
  public function action_product_write_panel_tabs() {
384
- echo '<li class="bulkdiscount_tab bulkdiscount_options"><a href="#bulkdiscount_product_data">' . __('Bulk Discount', 'wc_bulk_discount') . '</a></li>';
385
  }
386
 
387
  /**
@@ -389,35 +388,76 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
389
  */
390
  public function action_product_write_panels() {
391
  global $thepostid, $post;
392
- if (!$thepostid) $thepostid = $post->ID;
393
  ?>
394
  <script type="text/javascript">
395
  jQuery(document).ready(function () {
 
396
  <?php
397
- for($i = 1; $i <= 5; $i++) :
398
  ?>
399
- jQuery('#bulkdiscount_product_data').find('.block<?php echo $i; ?>').hide();
400
- jQuery('#bulkdiscount_product_data').find('.options_group<?php echo max($i, 2); ?>').hide();
401
- jQuery('#bulkdiscount_product_data').find('#add_discount_line<?php echo max($i, 2); ?>').hide();
402
- jQuery("#bulkdiscount_product_data").find('#add_discount_line<?php echo $i; ?>').click(function () {
403
- jQuery('#bulkdiscount_product_data').find('.block<?php echo $i; ?>').show(400);
404
- jQuery('#bulkdiscount_product_data').find('.options_group<?php echo min($i+1, 5); ?>').show(400);
405
- jQuery('#bulkdiscount_product_data').find('#add_discount_line<?php echo min($i+1, 5); ?>').show(400);
406
- jQuery('#bulkdiscount_product_data').find('#add_discount_line<?php echo $i; ?>').hide(400);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
407
  });
408
  <?php
409
  endfor;
410
- for ($i = 1; $i <= 5; $i++) {
411
- if (get_post_meta($thepostid, "_bulkdiscount_quantity_$i", true)) {
 
412
  ?>
413
- jQuery('#bulkdiscount_product_data').find('.block<?php echo $i; ?>').show();
414
- jQuery('#bulkdiscount_product_data').find('.options_group<?php echo $i; ?>').show();
415
- jQuery("#bulkdiscount_product_data").find('#add_discount_line<?php echo $i; ?>').hide();
416
- jQuery("#bulkdiscount_product_data").find('.options_group<?php echo min($i+1,5); ?>').show();
417
- jQuery("#bulkdiscount_product_data").find('#add_discount_line<?php echo min($i+1,5); ?>').show();
 
 
418
  <?php
 
419
  }
420
  }
 
 
 
 
421
  ?>
422
  });
423
  </script>
@@ -426,25 +466,39 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
426
 
427
  <div class="options_group">
428
  <?php
429
- woocommerce_wp_textarea_input(array('id' => "_bulkdiscount_text_info", 'label' => __('Bulk discount info in product description', 'wc_bulk_discount'), 'description' => __('Optionally enter bulk discount information that will be visible on the product page.', 'wc_bulk_discount'), 'desc_tip' => 'yes', 'class' => 'fullWidth'));
430
  ?>
431
  </div>
432
 
433
  <?php
434
- for ($i = 1; $i <= 5; $i++) :
 
 
435
  ?>
436
 
437
  <div class="options_group<?php echo $i; ?>">
438
  <a id="add_discount_line<?php echo $i; ?>" class="button-secondary"
439
- href="#block<?php echo $i; ?>">Add discount line</a>
 
 
440
 
441
- <div class="block<?php echo $i; ?>">
442
  <?php
443
- woocommerce_wp_text_input(array('id' => "_bulkdiscount_quantity_$i", 'label' => __('Quantity (min.)', 'wc_bulk_discount'), 'description' => __('Enter the minimal quantity for which the discount applies.', 'wc_bulk_discount')));
444
- if (get_option('woocommerce_t4m_discount_type', '') == 'flat') {
445
- woocommerce_wp_text_input(array('id' => "_bulkdiscount_discount_flat_$i", 'label' => sprintf(__('Discount (%s)', 'wc_bulk_discount'), get_woocommerce_currency_symbol()), 'description' => sprintf(__('Enter the flat discount in %s.', 'wc_bulk_discount'), get_woocommerce_currency_symbol())));
 
 
 
 
 
 
446
  } else {
447
- woocommerce_wp_text_input(array('id' => "_bulkdiscount_discount_$i", 'label' => __('Discount (%)', 'wc_bulk_discount'), 'description' => __('Enter the discount in percents (Allowed values: 0 to 100).', 'wc_bulk_discount')));
 
 
 
 
448
  }
449
  ?>
450
  </div>
@@ -454,6 +508,11 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
454
  endfor;
455
  ?>
456
 
 
 
 
 
 
457
  <br/>
458
 
459
  </div>
@@ -465,18 +524,18 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
465
  * Enqueue frontend dependencies.
466
  */
467
  public function action_enqueue_dependencies() {
468
- wp_register_style('woocommercebulkdiscount-style', plugins_url('css/style.css', __FILE__));
469
- wp_enqueue_style('woocommercebulkdiscount-style');
470
- wp_enqueue_script('jquery');
471
  }
472
 
473
  /**
474
  * Enqueue backend dependencies.
475
  */
476
  public function action_enqueue_dependencies_admin() {
477
- wp_register_style('woocommercebulkdiscount-style-admin', plugins_url('css/admin.css', __FILE__));
478
- wp_enqueue_style('woocommercebulkdiscount-style-admin');
479
- wp_enqueue_script('jquery');
480
  }
481
 
482
  /**
@@ -484,14 +543,14 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
484
  *
485
  * @param $post_id
486
  */
487
- public function action_process_meta($post_id) {
488
- if (isset($_POST['_bulkdiscount_text_info'])) update_post_meta($post_id, '_bulkdiscount_text_info', esc_attr($_POST['_bulkdiscount_text_info']));
489
- for ($i = 1; $i <= 5; $i++) {
490
- if (isset($_POST["_bulkdiscount_quantity_$i"])) update_post_meta($post_id, "_bulkdiscount_quantity_$i", stripslashes($_POST["_bulkdiscount_quantity_$i"]));
491
- if ((get_option('woocommerce_t4m_discount_type', '') == 'flat')) {
492
- if (isset($_POST["_bulkdiscount_discount_flat_$i"])) update_post_meta($post_id, "_bulkdiscount_discount_flat_$i", stripslashes($_POST["_bulkdiscount_discount_flat_$i"]));
493
  } else {
494
- if (isset($_POST["_bulkdiscount_discount_$i"])) update_post_meta($post_id, "_bulkdiscount_discount_$i", stripslashes($_POST["_bulkdiscount_discount_$i"]));
495
  }
496
  }
497
  }
@@ -501,11 +560,11 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
501
  * @return void
502
  */
503
  public function add_tab() {
504
- foreach ($this->settings_tabs as $name => $label) {
505
  $class = 'nav-tab';
506
- if ($this->current_tab == $name)
507
  $class .= ' nav-tab-active';
508
- echo '<a href="' . admin_url('admin.php?page=woocommerce&tab=' . $name) . '" class="' . $class . '">' . $label . '</a>';
509
  }
510
  }
511
 
@@ -517,13 +576,13 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
517
  global $woocommerce_settings;
518
 
519
  // Determine the current tab in effect.
520
- $current_tab = $this->get_tab_in_view(current_filter(), 'woocommerce_settings_tabs_');
521
 
522
  // Hook onto this from another function to keep things clean.
523
- do_action('woocommerce_bulk_discount_settings');
524
 
525
  // Display settings for this tab (make sure to add the settings to the tab).
526
- woocommerce_admin_fields($woocommerce_settings[$current_tab]);
527
  }
528
 
529
  /**
@@ -535,15 +594,15 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
535
  // Make sure our settings fields are recognised.
536
  $this->add_settings_fields();
537
 
538
- $current_tab = $this->get_tab_in_view(current_filter(), 'woocommerce_update_options_');
539
- woocommerce_update_options($woocommerce_settings[$current_tab]);
540
  }
541
 
542
  /**
543
  * Get the tab current in view/processing.
544
  */
545
- public function get_tab_in_view($current_filter, $filter_base) {
546
- return str_replace($filter_base, '', $current_filter);
547
  }
548
 
549
 
@@ -556,8 +615,8 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
556
  // Load the prepared form fields.
557
  $this->init_form_fields();
558
 
559
- if (is_array($this->fields))
560
- foreach ($this->fields as $k => $v)
561
  $woocommerce_settings[$k] = $v;
562
  }
563
 
@@ -568,54 +627,54 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
568
  global $woocommerce;
569
 
570
  // Define settings
571
- $this->fields['bulk_discount'] = apply_filters('woocommerce_bulk_discount_settings_fields', array(
572
 
573
- array('name' => __('Bulk Discount', 'wc_bulk_discount'), 'type' => 'title', 'desc' => __('The following options are specific to product bulk discount.', 'wc_bulk_discount') . '<br /><br/><strong><i>' . __('After changing the settings, it is recommended to clear all sessions in WooCommerce &gt; System Status &gt; Tools.', 'wc_bulk_discount') . '</i></strong>', 'id' => 't4m_bulk_discounts_options'),
574
 
575
  array(
576
- 'name' => __('Bulk Discount enabled', 'wc_bulk_discount'),
577
  'id' => 'woocommerce_t4m_enable_bulk_discounts',
578
- 'desc' => __('', 'wc_bulk_discount'),
579
  'std' => 'yes',
580
  'type' => 'checkbox',
581
  'default' => 'yes'
582
  ),
583
 
584
  array(
585
- 'title' => __('Discount Type', 'wc_bulk_discount'),
586
  'id' => 'woocommerce_t4m_discount_type',
587
- 'desc' => sprintf(__('Select the type of discount. Percentage Discount deducts amount of %% from price while Flat Discount deducts fixed amount in %s', 'wc_bulk_discount'), get_woocommerce_currency_symbol()),
588
  'desc_tip' => true,
589
  'std' => 'yes',
590
  'type' => 'select',
591
  'css' => 'min-width:200px;',
592
  'class' => 'chosen_select',
593
  'options' => array(
594
- '' => __('Percentage Discount', 'wc_bulk_discount'),
595
- 'flat' => __('Flat Discount', 'wc_bulk_discount')
596
  )
597
  ),
598
 
599
  array(
600
- 'name' => __('Treat product variations separately', 'wc_bulk_discount'),
601
  'id' => 'woocommerce_t4m_variations_separate',
602
- 'desc' => __('You need to have this option unchecked to handle discounts on product variations as a whole.', 'wc_bulk_discount'),
603
  'std' => 'yes',
604
  'type' => 'checkbox',
605
  'default' => 'yes'
606
  ),
607
 
608
  array(
609
- 'name' => __('Show discount information next to cart item price', 'wc_bulk_discount'),
610
  'id' => 'woocommerce_t4m_show_on_item',
611
- 'desc' => __('Applies only to percentage discount.', 'wc_bulk_discount'),
612
  'std' => 'yes',
613
  'type' => 'checkbox',
614
  'default' => 'yes'
615
  ),
616
 
617
  array(
618
- 'name' => __('Show discount information next to item subtotal price', 'wc_bulk_discount'),
619
  'id' => 'woocommerce_t4m_show_on_subtotal',
620
  'std' => 'yes',
621
  'type' => 'checkbox',
@@ -623,23 +682,23 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
623
  ),
624
 
625
  array(
626
- 'name' => __('Show discount information next to item subtotal price in order history', 'wc_bulk_discount'),
627
  'id' => 'woocommerce_t4m_show_on_order_subtotal',
628
- 'desc' => __('Includes showing discount in order e-mails and invoices.', 'wc_bulk_discount'),
629
  'std' => 'yes',
630
  'type' => 'checkbox',
631
  'default' => 'yes'
632
  ),
633
 
634
  array(
635
- 'name' => __('Optionally enter information about discounts visible on cart page.', 'wc_bulk_discount'),
636
  'id' => 'woocommerce_t4m_cart_info',
637
  'type' => 'textarea',
638
  'css' => 'width:100%; height: 75px;'
639
  ),
640
 
641
  array(
642
- 'name' => __('Optionally change the CSS for old price on cart before discounting.', 'wc_bulk_discount'),
643
  'id' => 'woocommerce_t4m_css_old_price',
644
  'type' => 'textarea',
645
  'css' => 'width:100%;',
@@ -647,7 +706,7 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
647
  ),
648
 
649
  array(
650
- 'name' => __('Optionally change the CSS for new price on cart after discounting.', 'wc_bulk_discount'),
651
  'id' => 'woocommerce_t4m_css_new_price',
652
  'type' => 'textarea',
653
  'css' => 'width:100%;',
@@ -664,7 +723,7 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
664
 
665
  array('type' => 'sectionend', 'id' => 'woocommerce_t4m_begging_text'),
666
 
667
- )); // End settings
668
 
669
  $js = "
670
  jQuery('#woocommerce_t4m_enable_bulk_discounts').change(function(){
@@ -686,10 +745,10 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
686
 
687
  ";
688
 
689
- if (class_exists('WC_Inline_Javascript_Helper')) {
690
- $woocommerce->get_helper('inline-javascript')->add_inline_js($js);
691
  } else {
692
- $woocommerce->add_inline_js($js);
693
  }
694
 
695
  }
4
  Plugin URI: http://www.tools4me.net/wordpress/woocommerce-bulk-discount-plugin
5
  Description: Apply fine-grained bulk discounts to items in the shopping cart.
6
  Author: Rene Puchinger
7
+ Version: 2.0.11
8
  Author URI: http://www.renepuchinger.com
9
  License: GPL3
10
 
 
11
  Copyright (C) 2013 Rene Puchinger
12
 
13
  This program is free software: you can redistribute it and/or modify
25
 
26
  */
27
 
28
+ if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
29
 
30
+ if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) return; // Check if WooCommerce is active
31
 
32
+ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
33
 
34
  class Woo_Bulk_Discount_Plugin_t4m {
35
 
37
 
38
  public function __construct() {
39
 
40
+ load_plugin_textdomain( 'wc_bulk_discount', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
41
 
42
+ $this->current_tab = ( isset( $_GET['tab'] ) ) ? $_GET['tab'] : 'general';
43
 
44
  $this->settings_tabs = array(
45
+ 'bulk_discount' => __( 'Bulk Discount', 'wc_bulk_discount' )
46
  );
47
 
48
+ add_action( 'admin_enqueue_scripts', array($this, 'action_enqueue_dependencies_admin') );
49
+ add_action( 'wp_head', array($this, 'action_enqueue_dependencies') );
50
 
51
+ add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array($this, 'action_links') );
52
 
53
+ add_action( 'woocommerce_settings_tabs', array($this, 'add_tab'), 10 );
54
 
55
  // Run these actions when generating the settings tabs.
56
+ foreach ( $this->settings_tabs as $name => $label ) {
57
+ add_action( 'woocommerce_settings_tabs_' . $name, array($this, 'settings_tab_action'), 10 );
58
+ add_action( 'woocommerce_update_options_' . $name, array($this, 'save_settings'), 10 );
59
  }
60
 
61
  // Add the settings fields to each tab.
62
+ add_action( 'woocommerce_bulk_discount_settings', array($this, 'add_settings_fields'), 10 );
63
+
64
+ if ( get_option( 'woocommerce_t4m_enable_bulk_discounts', 'yes' ) == 'yes' ) {
65
+ add_action( 'woocommerce_before_calculate_totals', array($this, 'action_before_calculate'), 10, 1 );
66
+ add_action( 'woocommerce_calculate_totals', array($this, 'action_after_calculate'), 10, 1 );
67
+ add_action( 'woocommerce_before_cart_table', array($this, 'before_cart_table') );
68
+ add_action( 'woocommerce_single_product_summary', array($this, 'single_product_summary'), 45 );
69
+ if ( class_exists( 'WC_Inline_Javascript_Helper' ) ) { // is WooCommerce 2.1 installed?
70
+ add_filter( 'woocommerce_cart_item_price', array($this, 'filter_item_price'), 10, 2 );
71
  } else {
72
+ add_filter( 'woocommerce_cart_item_price_html', array($this, 'filter_item_price'), 10, 2 );
73
  }
74
+ add_filter( 'woocommerce_cart_item_subtotal', array($this, 'filter_subtotal_price'), 10, 2 );
75
+ add_filter( 'woocommerce_checkout_item_subtotal', array($this, 'filter_subtotal_price'), 10, 2 );
76
+ add_filter( 'woocommerce_order_formatted_line_subtotal', array($this, 'filter_subtotal_order_price'), 10, 3 );
77
+ add_filter( 'woocommerce_product_write_panel_tabs', array($this, 'action_product_write_panel_tabs') );
78
+ add_filter( 'woocommerce_product_write_panels', array($this, 'action_product_write_panels') );
79
+ add_action( 'woocommerce_process_product_meta', array($this, 'action_process_meta') );
80
+ add_filter( 'woocommerce_cart_product_subtotal', array($this, 'filter_cart_product_subtotal'), 10, 3 );
81
+ add_action( 'woocommerce_checkout_update_order_meta', array($this, 'order_update_meta') );
82
  }
83
 
84
  }
89
  * @param $links
90
  * @return array
91
  */
92
+ public function action_links( $links ) {
93
 
94
  $plugin_links = array(
95
+ '<a href="' . admin_url( 'admin.php?page=woocommerce&tab=bulk_discount' ) . '">' . __( 'Settings', 'woocommerce' ) . '</a>',
96
  );
97
 
98
+ return array_merge( $plugin_links, $links );
99
  }
100
 
101
  /**
106
  * @param $order
107
  * @return float
108
  */
109
+ protected function get_discounted_coeff( $product_id, $quantity ) {
110
  $q = array(0.0);
111
  $d = array(0.0);
112
  /* Find the appropriate discount coefficient by looping through up to the five discount settings */
113
+ for ( $i = 1; $i <= 5; $i++ ) {
114
+ array_push( $q, get_post_meta( $product_id, "_bulkdiscount_quantity_$i", true ) );
115
+ if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) {
116
+ array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_flat_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_flat_$i", true ) : 0.0 );
117
  } else {
118
+ array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_$i", true ) : 0.0 );
119
  }
120
+ if ( $quantity >= $q[$i] && $q[$i] > $q[0] ) {
121
  $q[0] = $q[$i];
122
  $d[0] = $d[$i];
123
  }
124
  }
125
  // for percentage discount convert the resulting discount from % to the multiplying coefficient
126
+ return ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ? max( 0, $d[0] ) : min( 1.0, max( 0, ( 100.0 - round( $d[0], 2 ) ) / 100.0 ) );
127
  }
128
 
129
  /**
133
  * @param $values
134
  * @return string
135
  */
136
+ public function filter_item_price( $price, $values ) {
137
  global $woocommerce;
138
+ if ( !$values || @!$values['data'] ) {
139
  return $price;
140
  }
141
+ if ( ( get_option( 'woocommerce_t4m_show_on_item', 'yes' ) == 'no' ) ) {
142
  return $price;
143
  }
144
+ if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ) {
145
  return $price; // for flat discount this filter has no meaning
146
  }
147
  $_product = $values['data'];
148
+ if ( empty( $this->discount_coeffs ) || !isset( $this->discount_coeffs[$this->get_actual_id( $_product )] )
149
+ || !isset( $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] ) || !isset( $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'] )
150
  ) {
151
  $this->gather_discount_coeffs();
152
  }
153
+ $coeff = $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'];
154
+ if ( $coeff == 1.0 ) {
155
  return $price; // no price modification
156
  }
157
+ $discprice = woocommerce_price( $_product->get_price() * $coeff );
158
+ $oldprice = woocommerce_price( $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] );
159
+ $old_css = esc_attr( get_option( 'woocommerce_t4m_css_old_price', 'color: #777; text-decoration: line-through; margin-right: 4px;' ) );
160
+ $new_css = esc_attr( get_option( 'woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;' ) );
161
+ return "<span class='discount-info' title='" . sprintf( __( '%s%% bulk discount applied!', 'wc_bulk_discount' ), round( ( 1.0 - $coeff ) * 100.0, 2 ) ) . "'>" .
162
  "<span class='old-price' style='$old_css'>$oldprice</span>" .
163
  "<span class='new-price' style='$new_css'>$discprice</span></span>";
164
  }
170
  * @param $values
171
  * @return string
172
  */
173
+ public function filter_subtotal_price( $price, $values ) {
174
  global $woocommerce;
175
+ if ( !$values || !$values['data'] ) {
176
  return $price;
177
  }
178
+ if ( ( get_option( 'woocommerce_t4m_show_on_subtotal', 'yes' ) == 'no' ) ) {
179
  return $price;
180
  }
181
  $_product = $values['data'];
182
+ if ( empty( $this->discount_coeffs ) || !isset( $this->discount_coeffs[$this->get_actual_id( $_product )] )
183
+ || !isset( $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] ) || !isset( $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'] )
184
  ) {
185
  $this->gather_discount_coeffs();
186
  }
187
+ $coeff = $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'];
188
+ if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' && $coeff == 0 ) || ( get_option( 'woocommerce_t4m_discount_type', '' ) == '' && $coeff == 1.0 ) ) {
189
  return $price; // no price modification
190
  }
191
+ $oldprice = woocommerce_price( $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] );
192
+ $old_css = esc_attr( get_option( 'woocommerce_t4m_css_old_price', 'color: #777; text-decoration: line-through; margin-right: 4px;' ) );
193
+ $new_css = esc_attr( get_option( 'woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;' ) );
194
+ $bulk_info = sprintf( __( 'Incl. %s discount', 'wc_bulk_discount' ), ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ? get_woocommerce_currency_symbol() . $coeff : ( round( ( 1 - $coeff ) * 100, 2 ) . "%" ) ) );
195
  return "<span class='discount-info' title='$bulk_info'>" .
196
  "<span>$price</span>" .
197
  "<span class='new-price' style='$new_css'> ($bulk_info)</span></span>";
204
  global $woocommerce;
205
  $cart = $woocommerce->cart;
206
  $this->discount_coeffs = array();
207
+ if ( sizeof( $cart->cart_contents ) > 0 ) {
208
+ foreach ( $cart->cart_contents as $cart_item_key => $values ) {
209
  $_product = $values['data'];
210
  $quantity = 0;
211
+ if ( get_option( 'woocommerce_t4m_variations_separate', 'yes' ) == 'no' && $_product instanceof WC_Product_Variation && $_product->parent ) {
212
  $parent = $_product->parent;
213
+ foreach ( $cart->cart_contents as $valuesInner ) {
214
  $p = $valuesInner['data'];
215
+ if ( $p instanceof WC_Product_Variation && $p->parent && $p->parent->id == $parent->id ) {
216
  $quantity += $valuesInner['quantity'];
217
  $this->discount_coeffs[$_product->variation_id]['quantity'] = $quantity;
218
  }
220
  } else {
221
  $quantity = $values['quantity'];
222
  }
223
+ $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'] = $this->get_discounted_coeff( $_product->id, $quantity );
224
+ $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] = $_product->get_price();
225
  }
226
  }
227
  }
233
  * @param $values
234
  * @return string
235
  */
236
+ public function filter_subtotal_order_price( $price, $values, $order ) {
237
  global $woocommerce;
238
+ if ( !$values || !$order ) {
239
  return $price;
240
  }
241
+ if ( ( get_option( 'woocommerce_t4m_show_on_order_subtotal', 'yes' ) == 'no' ) ) {
242
  return $price;
243
  }
244
+ $_product = get_product( $values['product_id'] );
245
  $actual_id = $values['product_id'];
246
+ if ( $_product && $_product instanceof WC_Product_Variable && $values['variation_id'] ) {
247
  $actual_id = $values['variation_id'];
248
  }
249
+ $discount_coeffs = $this->gather_discount_coeffs_from_order( $order->id );
250
+ if ( empty( $discount_coeffs ) ) {
251
  return $price;
252
  }
253
  $coeff = $discount_coeffs[$actual_id]['coeff'];
254
+ $discount_type = get_post_meta( $order->id, '_woocommerce_t4m_discount_type', true );
255
+ if ( ( $discount_type == 'flat' && $coeff == 0 ) || ( $discount_type == '' && $coeff == 1.0 ) ) {
256
  return $price; // no price modification
257
  }
258
+ $oldprice = woocommerce_price( $discount_coeffs[$actual_id]['orig_price'] );
259
+ $old_css = esc_attr( get_option( 'woocommerce_t4m_css_old_price', 'color: #777; text-decoration: line-through; margin-right: 4px;' ) );
260
+ $new_css = esc_attr( get_option( 'woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;' ) );
261
+ $bulk_info = sprintf( __( 'Incl. %s discount', 'wc_bulk_discount' ), ( $discount_type == 'flat' ? get_woocommerce_currency_symbol() . $coeff : ( round( ( 1 - $coeff ) * 100, 2 ) . "%" ) ) );
262
  return "<span class='discount-info' title='$bulk_info'>" .
263
  "<span>$price</span>" .
264
  "<span class='new-price' style='$new_css'> ($bulk_info)</span></span>";
270
  * @param $order_id
271
  * @return array
272
  */
273
+ protected function gather_discount_coeffs_from_order( $order_id ) {
274
+ $meta = get_post_meta( $order_id, '_woocommerce_t4m_discount_coeffs', true );
275
+ if ( !$meta ) {
276
  return null;
277
  }
278
+ $order_discount_coeffs = json_decode( $meta, true );
279
  return $order_discount_coeffs;
280
  }
281
 
284
  *
285
  * @param WC_Cart $cart
286
  */
287
+ public function action_before_calculate( WC_Cart $cart ) {
288
 
289
  $this->gather_discount_coeffs();
290
 
291
+ if ( sizeof( $cart->cart_contents ) > 0 ) {
292
+ foreach ( $cart->cart_contents as $cart_item_key => $values ) {
293
  $_product = $values['data'];
294
+ if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ) {
295
+ $row_base_price = max( 0, $_product->get_price() - ( $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'] / $values['quantity'] ) );
296
  } else {
297
+ $row_base_price = $_product->get_price() * $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'];
298
  }
299
 
300
+ $values['data']->set_price( $row_base_price );
301
  }
302
  }
303
  }
304
 
305
+ protected function get_actual_id( $product ) {
306
+ if ( $product instanceof WC_Product_Variation ) {
307
  return $product->variation_id;
308
  } else {
309
  return $product->id;
315
  *
316
  * @param WC_Cart $cart
317
  */
318
+ public function action_after_calculate( WC_Cart $cart ) {
319
+ if ( sizeof( $cart->cart_contents ) > 0 ) {
320
+ foreach ( $cart->cart_contents as $cart_item_key => $values ) {
321
  $_product = $values['data'];
322
+ $values['data']->set_price( $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] );
323
  }
324
  }
325
  }
329
  */
330
  public function before_cart_table() {
331
  echo "<div class='cart-show-discounts'>";
332
+ echo get_option( 'woocommerce_t4m_cart_info' );
333
  echo "</div>";
334
  }
335
 
342
  * @param WC_Cart $cart
343
  * @return string
344
  */
345
+ public function filter_cart_product_subtotal( $subtotal, $_product, $quantity ) {
346
+ if ( !$_product || !$quantity ) {
347
  return $subtotal;
348
  }
349
+ $coeff = $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'];
350
+ if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ) {
351
+ $newsubtotal = woocommerce_price( max( 0, ( $_product->get_price() * $quantity ) - $coeff ) );
352
  } else {
353
+ $newsubtotal = woocommerce_price( $_product->get_price() * $quantity * $coeff );
354
  }
355
  return $newsubtotal;
356
  }
360
  *
361
  * @param $order_id
362
  */
363
+ public function order_update_meta( $order_id ) {
364
+ update_post_meta( $order_id, "_woocommerce_t4m_discount_type", get_option( 'woocommerce_t4m_discount_type', '' ) );
365
+ update_post_meta( $order_id, "_woocommerce_t4m_discount_coeffs", json_encode( $this->discount_coeffs ) );
366
  }
367
 
368
  /**
370
  */
371
  public function single_product_summary() {
372
  global $thepostid, $post;
373
+ if ( !$thepostid ) $thepostid = $post->ID;
374
  echo "<div class='productinfo-show-discounts'>";
375
+ echo get_post_meta( $thepostid, '_bulkdiscount_text_info', true );
376
  echo "</div>";
377
  }
378
 
380
  * Add entry to Product Settings.
381
  */
382
  public function action_product_write_panel_tabs() {
383
+ echo '<li class="bulkdiscount_tab bulkdiscount_options"><a href="#bulkdiscount_product_data">' . __( 'Bulk Discount', 'wc_bulk_discount' ) . '</a></li>';
384
  }
385
 
386
  /**
388
  */
389
  public function action_product_write_panels() {
390
  global $thepostid, $post;
391
+ if ( !$thepostid ) $thepostid = $post->ID;
392
  ?>
393
  <script type="text/javascript">
394
  jQuery(document).ready(function () {
395
+ var e = jQuery('#bulkdiscount_product_data');
396
  <?php
397
+ for($i = 1; $i <= 6; $i++) :
398
  ?>
399
+ e.find('.block<?php echo $i; ?>').hide();
400
+ e.find('.options_group<?php echo max($i, 2); ?>').hide();
401
+ e.find('#add_discount_line<?php echo max($i, 2); ?>').hide();
402
+ e.find('#add_discount_line<?php echo $i; ?>').click(function () {
403
+ if (<?php echo $i; ?> == 1 || ( e.find('#_bulkdiscount_quantity_<?php echo max($i-1, 1); ?>').val() != '' &&
404
+ <?php if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) : ?>
405
+ e.find('#_bulkdiscount_discount_flat_<?php echo max($i-1, 1); ?>').val() != ''
406
+ <?php else: ?>
407
+ e.find('#_bulkdiscount_discount_<?php echo max($i-1, 1); ?>').val() != ''
408
+ <?php endif; ?>
409
+ ) )
410
+ {
411
+ e.find('.block<?php echo $i; ?>').show(400);
412
+ e.find('.options_group<?php echo min($i+1, 6); ?>').show(400);
413
+ e.find('#add_discount_line<?php echo min($i+1, 5); ?>').show(400);
414
+ e.find('#add_discount_line<?php echo $i; ?>').hide(400);
415
+ e.find('#delete_discount_line<?php echo min($i+1, 6); ?>').show(400);
416
+ e.find('#delete_discount_line<?php echo $i; ?>').hide(400);
417
+ }
418
+ else
419
+ {
420
+ alert('<?php _e( 'Please fill in the current line before adding new line.', 'wc_bulk_discount' ); ?>');
421
+ }
422
+ });
423
+ e.find('#delete_discount_line<?php echo max($i, 1); ?>').hide();
424
+ e.find('#delete_discount_line<?php echo $i; ?>').click(function () {
425
+ e.find('.block<?php echo max($i-1, 1); ?>').hide(400);
426
+ e.find('.options_group<?php echo min($i, 6); ?>').hide(400);
427
+ e.find('#add_discount_line<?php echo min($i, 5); ?>').hide(400);
428
+ e.find('#add_discount_line<?php echo max($i-1, 1); ?>').show(400);
429
+ e.find('#delete_discount_line<?php echo min($i, 6); ?>').hide(400);
430
+ e.find('#delete_discount_line<?php echo max($i-1, 2); ?>').show(400);
431
+ e.find('#_bulkdiscount_quantity_<?php echo max($i-1, 1); ?>').val('');
432
+ <?php
433
+ if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) :
434
+ ?>
435
+ e.find('#_bulkdiscount_discount_flat_<?php echo max($i-1, 1); ?>').val('');
436
+ <?php else: ?>
437
+ e.find('#_bulkdiscount_discount_<?php echo max($i-1, 1); ?>').val('');
438
+ <?php endif; ?>
439
  });
440
  <?php
441
  endfor;
442
+ for ($i = 1, $j = 2; $i <= 5; $i++, $j++) {
443
+ $cnt = 1;
444
+ if (get_post_meta($thepostid, "_bulkdiscount_quantity_$i", true) || get_post_meta($thepostid, "_bulkdiscount_quantity_$j", true)) {
445
  ?>
446
+ e.find('.block<?php echo $i; ?>').show();
447
+ e.find('.options_group<?php echo $i; ?>').show();
448
+ e.find('#add_discount_line<?php echo $i; ?>').hide();
449
+ e.find('#delete_discount_line<?php echo $i; ?>').hide();
450
+ e.find('.options_group<?php echo min($i+1,6); ?>').show();
451
+ e.find('#add_discount_line<?php echo min($i+1,6); ?>').show();
452
+ e.find('#delete_discount_line<?php echo min($i+1,6); ?>').show();
453
  <?php
454
+ $cnt++;
455
  }
456
  }
457
+ if ($cnt >= 6) {
458
+ ?>e.find('#add_discount_line6').show();
459
+ <?php
460
+ }
461
  ?>
462
  });
463
  </script>
466
 
467
  <div class="options_group">
468
  <?php
469
+ woocommerce_wp_textarea_input( array('id' => "_bulkdiscount_text_info", 'label' => __( 'Bulk discount info in product description', 'wc_bulk_discount' ), 'description' => __( 'Optionally enter bulk discount information that will be visible on the product page.', 'wc_bulk_discount' ), 'desc_tip' => 'yes', 'class' => 'fullWidth') );
470
  ?>
471
  </div>
472
 
473
  <?php
474
+ for ( $i = 1;
475
+ $i <= 5;
476
+ $i++ ) :
477
  ?>
478
 
479
  <div class="options_group<?php echo $i; ?>">
480
  <a id="add_discount_line<?php echo $i; ?>" class="button-secondary"
481
+ href="#block<?php echo $i; ?>"><?php _e( 'Add discount line', 'wc_bulk_discount' ); ?></a>
482
+ <a id="delete_discount_line<?php echo $i; ?>" class="button-secondary"
483
+ href="#block<?php echo $i; ?>"><?php _e( 'Remove last discount line', 'wc_bulk_discount' ); ?></a>
484
 
485
+ <div class="block<?php echo $i; ?> <?php echo ($i % 2 == 0) ? 'even' : 'odd' ?>">
486
  <?php
487
+ woocommerce_wp_text_input( array('id' => "_bulkdiscount_quantity_$i", 'label' => __( 'Quantity (min.)', 'wc_bulk_discount' ), 'type' => 'number', 'description' => __( 'Enter the minimal quantity for which the discount applies.', 'wc_bulk_discount' ), 'custom_attributes' => array(
488
+ 'step' => '1',
489
+ 'min' => '1'
490
+ )) );
491
+ if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) {
492
+ woocommerce_wp_text_input( array('id' => "_bulkdiscount_discount_flat_$i", 'type' => 'number', 'label' => sprintf( __( 'Discount (%s)', 'wc_bulk_discount' ), get_woocommerce_currency_symbol() ), 'description' => sprintf( __( 'Enter the flat discount in %s.', 'wc_bulk_discount' ), get_woocommerce_currency_symbol() ), 'custom_attributes' => array(
493
+ 'step' => 'any',
494
+ 'min' => '0'
495
+ )) );
496
  } else {
497
+ woocommerce_wp_text_input( array('id' => "_bulkdiscount_discount_$i", 'type' => 'number', 'label' => __( 'Discount (%)', 'wc_bulk_discount' ), 'description' => __( 'Enter the discount in percents (Allowed values: 0 to 100).', 'wc_bulk_discount' ), 'custom_attributes' => array(
498
+ 'step' => 'any',
499
+ 'min' => '0',
500
+ 'max' => '100'
501
+ )) );
502
  }
503
  ?>
504
  </div>
508
  endfor;
509
  ?>
510
 
511
+ <div class="options_group6">
512
+ <a id="delete_discount_line6" class="button-secondary"
513
+ href="#block6"><?php _e( 'Remove last discount line', 'wc_bulk_discount' ); ?></a>
514
+ </div>
515
+
516
  <br/>
517
 
518
  </div>
524
  * Enqueue frontend dependencies.
525
  */
526
  public function action_enqueue_dependencies() {
527
+ wp_register_style( 'woocommercebulkdiscount-style', plugins_url( 'css/style.css', __FILE__ ) );
528
+ wp_enqueue_style( 'woocommercebulkdiscount-style' );
529
+ wp_enqueue_script( 'jquery' );
530
  }
531
 
532
  /**
533
  * Enqueue backend dependencies.
534
  */
535
  public function action_enqueue_dependencies_admin() {
536
+ wp_register_style( 'woocommercebulkdiscount-style-admin', plugins_url( 'css/admin.css', __FILE__ ) );
537
+ wp_enqueue_style( 'woocommercebulkdiscount-style-admin' );
538
+ wp_enqueue_script( 'jquery' );
539
  }
540
 
541
  /**
543
  *
544
  * @param $post_id
545
  */
546
+ public function action_process_meta( $post_id ) {
547
+ if ( isset( $_POST['_bulkdiscount_text_info'] ) ) update_post_meta( $post_id, '_bulkdiscount_text_info', esc_attr( $_POST['_bulkdiscount_text_info'] ) );
548
+ for ( $i = 1; $i <= 5; $i++ ) {
549
+ if ( isset( $_POST["_bulkdiscount_quantity_$i"] ) ) update_post_meta( $post_id, "_bulkdiscount_quantity_$i", stripslashes( $_POST["_bulkdiscount_quantity_$i"] ) );
550
+ if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ) {
551
+ if ( isset( $_POST["_bulkdiscount_discount_flat_$i"] ) ) update_post_meta( $post_id, "_bulkdiscount_discount_flat_$i", stripslashes( $_POST["_bulkdiscount_discount_flat_$i"] ) );
552
  } else {
553
+ if ( isset( $_POST["_bulkdiscount_discount_$i"] ) ) update_post_meta( $post_id, "_bulkdiscount_discount_$i", stripslashes( $_POST["_bulkdiscount_discount_$i"] ) );
554
  }
555
  }
556
  }
560
  * @return void
561
  */
562
  public function add_tab() {
563
+ foreach ( $this->settings_tabs as $name => $label ) {
564
  $class = 'nav-tab';
565
+ if ( $this->current_tab == $name )
566
  $class .= ' nav-tab-active';
567
+ echo '<a href="' . admin_url( 'admin.php?page=woocommerce&tab=' . $name ) . '" class="' . $class . '">' . $label . '</a>';
568
  }
569
  }
570
 
576
  global $woocommerce_settings;
577
 
578
  // Determine the current tab in effect.
579
+ $current_tab = $this->get_tab_in_view( current_filter(), 'woocommerce_settings_tabs_' );
580
 
581
  // Hook onto this from another function to keep things clean.
582
+ do_action( 'woocommerce_bulk_discount_settings' );
583
 
584
  // Display settings for this tab (make sure to add the settings to the tab).
585
+ woocommerce_admin_fields( $woocommerce_settings[$current_tab] );
586
  }
587
 
588
  /**
594
  // Make sure our settings fields are recognised.
595
  $this->add_settings_fields();
596
 
597
+ $current_tab = $this->get_tab_in_view( current_filter(), 'woocommerce_update_options_' );
598
+ woocommerce_update_options( $woocommerce_settings[$current_tab] );
599
  }
600
 
601
  /**
602
  * Get the tab current in view/processing.
603
  */
604
+ public function get_tab_in_view( $current_filter, $filter_base ) {
605
+ return str_replace( $filter_base, '', $current_filter );
606
  }
607
 
608
 
615
  // Load the prepared form fields.
616
  $this->init_form_fields();
617
 
618
+ if ( is_array( $this->fields ) )
619
+ foreach ( $this->fields as $k => $v )
620
  $woocommerce_settings[$k] = $v;
621
  }
622
 
627
  global $woocommerce;
628
 
629
  // Define settings
630
+ $this->fields['bulk_discount'] = apply_filters( 'woocommerce_bulk_discount_settings_fields', array(
631
 
632
+ array('name' => __( 'Bulk Discount', 'wc_bulk_discount' ), 'type' => 'title', 'desc' => __( 'The following options are specific to product bulk discount.', 'wc_bulk_discount' ) . '<br /><br/><strong><i>' . __( 'After changing the settings, it is recommended to clear all sessions in WooCommerce &gt; System Status &gt; Tools.', 'wc_bulk_discount' ) . '</i></strong>', 'id' => 't4m_bulk_discounts_options'),
633
 
634
  array(
635
+ 'name' => __( 'Bulk Discount enabled', 'wc_bulk_discount' ),
636
  'id' => 'woocommerce_t4m_enable_bulk_discounts',
637
+ 'desc' => __( '', 'wc_bulk_discount' ),
638
  'std' => 'yes',
639
  'type' => 'checkbox',
640
  'default' => 'yes'
641
  ),
642
 
643
  array(
644
+ 'title' => __( 'Discount Type', 'wc_bulk_discount' ),
645
  'id' => 'woocommerce_t4m_discount_type',
646
+ 'desc' => sprintf( __( 'Select the type of discount. Percentage Discount deducts amount of %% from price while Flat Discount deducts fixed amount in %s', 'wc_bulk_discount' ), get_woocommerce_currency_symbol() ),
647
  'desc_tip' => true,
648
  'std' => 'yes',
649
  'type' => 'select',
650
  'css' => 'min-width:200px;',
651
  'class' => 'chosen_select',
652
  'options' => array(
653
+ '' => __( 'Percentage Discount', 'wc_bulk_discount' ),
654
+ 'flat' => __( 'Flat Discount', 'wc_bulk_discount' )
655
  )
656
  ),
657
 
658
  array(
659
+ 'name' => __( 'Treat product variations separately', 'wc_bulk_discount' ),
660
  'id' => 'woocommerce_t4m_variations_separate',
661
+ 'desc' => __( 'You need to have this option unchecked to handle discounts on product variations as a whole.', 'wc_bulk_discount' ),
662
  'std' => 'yes',
663
  'type' => 'checkbox',
664
  'default' => 'yes'
665
  ),
666
 
667
  array(
668
+ 'name' => __( 'Show discount information next to cart item price', 'wc_bulk_discount' ),
669
  'id' => 'woocommerce_t4m_show_on_item',
670
+ 'desc' => __( 'Applies only to percentage discount.', 'wc_bulk_discount' ),
671
  'std' => 'yes',
672
  'type' => 'checkbox',
673
  'default' => 'yes'
674
  ),
675
 
676
  array(
677
+ 'name' => __( 'Show discount information next to item subtotal price', 'wc_bulk_discount' ),
678
  'id' => 'woocommerce_t4m_show_on_subtotal',
679
  'std' => 'yes',
680
  'type' => 'checkbox',
682
  ),
683
 
684
  array(
685
+ 'name' => __( 'Show discount information next to item subtotal price in order history', 'wc_bulk_discount' ),
686
  'id' => 'woocommerce_t4m_show_on_order_subtotal',
687
+ 'desc' => __( 'Includes showing discount in order e-mails and invoices.', 'wc_bulk_discount' ),
688
  'std' => 'yes',
689
  'type' => 'checkbox',
690
  'default' => 'yes'
691
  ),
692
 
693
  array(
694
+ 'name' => __( 'Optionally enter information about discounts visible on cart page.', 'wc_bulk_discount' ),
695
  'id' => 'woocommerce_t4m_cart_info',
696
  'type' => 'textarea',
697
  'css' => 'width:100%; height: 75px;'
698
  ),
699
 
700
  array(
701
+ 'name' => __( 'Optionally change the CSS for old price on cart before discounting.', 'wc_bulk_discount' ),
702
  'id' => 'woocommerce_t4m_css_old_price',
703
  'type' => 'textarea',
704
  'css' => 'width:100%;',
706
  ),
707
 
708
  array(
709
+ 'name' => __( 'Optionally change the CSS for new price on cart after discounting.', 'wc_bulk_discount' ),
710
  'id' => 'woocommerce_t4m_css_new_price',
711
  'type' => 'textarea',
712
  'css' => 'width:100%;',
723
 
724
  array('type' => 'sectionend', 'id' => 'woocommerce_t4m_begging_text'),
725
 
726
+ ) ); // End settings
727
 
728
  $js = "
729
  jQuery('#woocommerce_t4m_enable_bulk_discounts').change(function(){
745
 
746
  ";
747
 
748
+ if ( class_exists( 'WC_Inline_Javascript_Helper' ) ) {
749
+ $woocommerce->get_helper( 'inline-javascript' )->add_inline_js( $js );
750
  } else {
751
+ $woocommerce->add_inline_js( $js );
752
  }
753
 
754
  }