WooCommerce Bulk Discount - Version 2.1.5

Version Description

  • (7 Dec 2013) It is now possible to disable the feature of removing bulk discounts when a coupon code is applied.
  • Minor code cleanup and optimization.
Download this release

Release Info

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

Code changes from version 2.1.4 to 2.1.5

Files changed (2) hide show
  1. readme.txt +17 -4
  2. woocommerce-bulk-discount.php +126 -52
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: Rene Puchinger
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6VEQ8XXK6B3UE
4
  Tags: woocommerce, bulk, discount
5
  Requires at least: 3.5
6
- Tested up to: 3.7.1
7
- Stable tag: 2.1.4
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
@@ -15,7 +15,7 @@ Apply fine-grained bulk discounts to items in the shopping cart with this WooCom
15
  WooCommerce Bulk Discount makes it possible to apply fine-grained bulk discounts to items in the shopping cart,
16
  depending on the ordered quantity and on the specific product.
17
 
18
- WooCommerce 2.0.x is required.
19
 
20
  Let us examine some examples of usage.
21
 
@@ -45,7 +45,7 @@ Here is the list of the main features:
45
  * Discount is visible in WooCommerce order e-mails and invoices as well.
46
  * Showing the applied discount when hovering over the item price in the cart.
47
  * Possibility of easily changing the CSS of the price before and after discount.
48
- * Bulk discount is not applied if coupon code is used. **New in 2.1**
49
  * HTML markup is allowed in information about the bulk discount offer in Product Description. **New in 2.1**
50
  * Bulk Discount can be disabled more easily in the Product Options page. **New in 2.1**
51
 
@@ -56,6 +56,12 @@ WooCommerce Bulk Discount has been localised to these languages:
56
  * Portuguese (translated by João)
57
  * Czech
58
 
 
 
 
 
 
 
59
 
60
  == Installation ==
61
 
@@ -138,6 +144,10 @@ Setting bulk discounts couldn't have been easier.
138
 
139
  == Changelog ==
140
 
 
 
 
 
141
  = 2.1.4 =
142
  * (23 Nov 2013) Corrected an issue of forcing Bulk Discount disabled on products after updating from previous versions of Bulk Discount.
143
 
@@ -235,6 +245,9 @@ Setting bulk discounts couldn't have been easier.
235
 
236
  == Upgrade Notice ==
237
 
 
 
 
238
  = 2.1.4 =
239
  Bugfix release. Please update immediately.
240
 
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6VEQ8XXK6B3UE
4
  Tags: woocommerce, bulk, discount
5
  Requires at least: 3.5
6
+ Tested up to: 3.8
7
+ Stable tag: 2.1.5
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
15
  WooCommerce Bulk Discount makes it possible to apply fine-grained bulk discounts to items in the shopping cart,
16
  depending on the ordered quantity and on the specific product.
17
 
18
+ **WooCommerce 2.0.x is required. The plugin is not compatible with WooCommerce 2.1+**
19
 
20
  Let us examine some examples of usage.
21
 
45
  * Discount is visible in WooCommerce order e-mails and invoices as well.
46
  * Showing the applied discount when hovering over the item price in the cart.
47
  * Possibility of easily changing the CSS of the price before and after discount.
48
+ * Bulk discount can or cannot be applied if a coupon code is used, depending on configuration. **Updated in 2.1.5**
49
  * HTML markup is allowed in information about the bulk discount offer in Product Description. **New in 2.1**
50
  * Bulk Discount can be disabled more easily in the Product Options page. **New in 2.1**
51
 
56
  * Portuguese (translated by João)
57
  * Czech
58
 
59
+ **Check out these premium WooCommerce extensions:**
60
+
61
+ * <a href="http://codecanyon.net/item/woocommerce-free-gift/6144902?ref=renp">WooCommerce Free Gift</a>
62
+ * <a href="http://codecanyon.net/item/woocommerce-minute-discounts-on-crosssells/6337331?ref=renp">WooCommerce Minute Discounts on Cross-sells</a>
63
+ * <a href="http://codecanyon.net/item/woocommerce-gift-coupons/6207561?ref=renp">WooCommerce Gift Coupons</a>
64
+ * <a href="http://codecanyon.net/item/woocommerce-add-custom-quantity-to-cart/6360685?ref=renp">WooCommerce Add Custom Quantity to Cart</a>
65
 
66
  == Installation ==
67
 
144
 
145
  == Changelog ==
146
 
147
+ = 2.1.5 =
148
+ * (7 Dec 2013) It is now possible to disable the feature of removing bulk discounts when a coupon code is applied.
149
+ * Minor code cleanup and optimization.
150
+
151
  = 2.1.4 =
152
  * (23 Nov 2013) Corrected an issue of forcing Bulk Discount disabled on products after updating from previous versions of Bulk Discount.
153
 
245
 
246
  == Upgrade Notice ==
247
 
248
+ = 2.1.5 =
249
+ Maintenance release.
250
+
251
  = 2.1.4 =
252
  Bugfix release. Please update immediately.
253
 
woocommerce-bulk-discount.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: WooCommerce Bulk Discount
4
  Plugin URI: http://www.renepuchinger.com
5
  Description: Apply fine-grained bulk discounts to items in the shopping cart.
6
  Author: Rene Puchinger
7
- Version: 2.1.4
8
  Author URI: http://www.renepuchinger.com
9
  License: GPL3
10
 
@@ -61,16 +61,21 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
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 );
@@ -107,8 +112,10 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
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 ) );
@@ -122,8 +129,10 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
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
  /**
@@ -134,10 +143,11 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
134
  * @return string
135
  */
136
  public function filter_item_price( $price, $values ) {
 
137
  if ( !$values || @!$values['data'] ) {
138
  return $price;
139
  }
140
- if ( $this->is_coupon_applied() ) {
141
  return $price;
142
  }
143
  $_product = $values['data'];
@@ -166,6 +176,7 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
166
  return "<span class='discount-info' title='" . sprintf( __( '%s%% bulk discount applied!', 'wc_bulk_discount' ), round( ( 1.0 - $coeff ) * 100.0, 2 ) ) . "'>" .
167
  "<span class='old-price' style='$old_css'>$oldprice</span>" .
168
  "<span class='new-price' style='$new_css'>$discprice</span></span>";
 
169
  }
170
 
171
  /**
@@ -176,10 +187,11 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
176
  * @return string
177
  */
178
  public function filter_subtotal_price( $price, $values ) {
 
179
  if ( !$values || !$values['data'] ) {
180
  return $price;
181
  }
182
- if ( $this->is_coupon_applied() ) {
183
  return $price;
184
  }
185
  $_product = $values['data'];
@@ -200,18 +212,23 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
200
  }
201
  $new_css = esc_attr( get_option( 'woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;' ) );
202
  $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 ) . "%" ) ) );
 
203
  return "<span class='discount-info' title='$bulk_info'>" .
204
  "<span>$price</span>" .
205
  "<span class='new-price' style='$new_css'> ($bulk_info)</span></span>";
 
206
  }
207
 
208
  /**
209
  * Gather discount information to the array $this->discount_coefs
210
  */
211
  protected function gather_discount_coeffs() {
 
212
  global $woocommerce;
 
213
  $cart = $woocommerce->cart;
214
  $this->discount_coeffs = array();
 
215
  if ( sizeof( $cart->cart_contents ) > 0 ) {
216
  foreach ( $cart->cart_contents as $cart_item_key => $values ) {
217
  $_product = $values['data'];
@@ -232,6 +249,7 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
232
  $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] = $_product->get_price();
233
  }
234
  }
 
235
  }
236
 
237
  /**
@@ -242,12 +260,14 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
242
  * @return string
243
  */
244
  public function filter_subtotal_order_price( $price, $values, $order ) {
 
245
  if ( !$values || !$order ) {
246
  return $price;
247
  }
248
- if ( $this->is_coupon_applied() ) {
249
  return $price;
250
  }
 
251
  $_product = get_product( $values['product_id'] );
252
  if ( get_post_meta( $values['product_id'], "_bulkdiscount_enabled", true ) != '' && get_post_meta( $values['product_id'], "_bulkdiscount_enabled", true ) !== 'yes' ) {
253
  return $price;
@@ -273,9 +293,11 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
273
  }
274
  $new_css = esc_attr( get_option( 'woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;' ) );
275
  $bulk_info = sprintf( __( 'Incl. %s discount', 'wc_bulk_discount' ), ( $discount_type == 'flat' ? get_woocommerce_currency_symbol() . $coeff : ( round( ( 1 - $coeff ) * 100, 2 ) . "%" ) ) );
 
276
  return "<span class='discount-info' title='$bulk_info'>" .
277
  "<span>$price</span>" .
278
  "<span class='new-price' style='$new_css'> ($bulk_info)</span></span>";
 
279
  }
280
 
281
  /**
@@ -285,12 +307,16 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
285
  * @return array
286
  */
287
  protected function gather_discount_coeffs_from_order( $order_id ) {
 
288
  $meta = get_post_meta( $order_id, '_woocommerce_t4m_discount_coeffs', true );
 
289
  if ( !$meta ) {
290
  return null;
291
  }
 
292
  $order_discount_coeffs = json_decode( $meta, true );
293
  return $order_discount_coeffs;
 
294
  }
295
 
296
  /**
@@ -300,7 +326,7 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
300
  */
301
  public function action_before_calculate( WC_Cart $cart ) {
302
 
303
- if ( $this->is_coupon_applied() ) {
304
  return;
305
  }
306
 
@@ -321,6 +347,7 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
321
  $values['data']->set_price( $row_base_price );
322
  }
323
  }
 
324
  }
325
 
326
  /**
@@ -328,11 +355,13 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
328
  * @return int
329
  */
330
  protected function get_actual_id( $product ) {
 
331
  if ( $product instanceof WC_Product_Variation ) {
332
  return $product->variation_id;
333
  } else {
334
  return $product->id;
335
  }
 
336
  }
337
 
338
  /**
@@ -342,7 +371,7 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
342
  */
343
  public function action_after_calculate( WC_Cart $cart ) {
344
 
345
- if ( $this->is_coupon_applied() ) {
346
  return;
347
  }
348
 
@@ -355,17 +384,20 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
355
  $values['data']->set_price( $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] );
356
  }
357
  }
 
358
  }
359
 
360
  /**
361
  * Show discount info in cart.
362
  */
363
  public function before_cart_table() {
 
364
  if ( get_option( 'woocommerce_t4m_cart_info' ) != '' ) {
365
  echo "<div class='cart-show-discounts'>";
366
  echo get_option( 'woocommerce_t4m_cart_info' );
367
  echo "</div>";
368
  }
 
369
  }
370
 
371
  /**
@@ -378,22 +410,26 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
378
  * @return string
379
  */
380
  public function filter_cart_product_subtotal( $subtotal, $_product, $quantity ) {
 
381
  if ( !$_product || !$quantity ) {
382
  return $subtotal;
383
  }
384
- if ( $this->is_coupon_applied() ) {
385
  return $subtotal;
386
  }
387
  if ( get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) != '' && get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) !== 'yes' ) {
388
  return $subtotal;
389
  }
 
390
  $coeff = $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'];
391
  if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ) {
392
  $newsubtotal = woocommerce_price( max( 0, ( $_product->get_price() * $quantity ) - $coeff ) );
393
  } else {
394
  $newsubtotal = woocommerce_price( $_product->get_price() * $quantity * $coeff );
395
  }
 
396
  return $newsubtotal;
 
397
  }
398
 
399
  /**
@@ -402,19 +438,24 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
402
  * @param $order_id
403
  */
404
  public function order_update_meta( $order_id ) {
 
405
  update_post_meta( $order_id, "_woocommerce_t4m_discount_type", get_option( 'woocommerce_t4m_discount_type', '' ) );
406
  update_post_meta( $order_id, "_woocommerce_t4m_discount_coeffs", json_encode( $this->discount_coeffs ) );
 
407
  }
408
 
409
  /**
410
  * Display discount information in Product Detail.
411
  */
412
  public function single_product_summary() {
 
413
  global $thepostid, $post;
414
  if ( !$thepostid ) $thepostid = $post->ID;
 
415
  echo "<div class='productinfo-show-discounts'>";
416
  echo get_post_meta( $thepostid, '_bulkdiscount_text_info', true );
417
  echo "</div>";
 
418
  }
419
 
420
  /**
@@ -432,75 +473,75 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
432
  if ( !$thepostid ) $thepostid = $post->ID;
433
  ?>
434
  <script type="text/javascript">
435
- jQuery(document).ready(function () {
436
- var e = jQuery('#bulkdiscount_product_data');
437
  <?php
438
  for($i = 1; $i <= 6; $i++) :
439
  ?>
440
- e.find('.block<?php echo $i; ?>').hide();
441
- e.find('.options_group<?php echo max($i, 2); ?>').hide();
442
- e.find('#add_discount_line<?php echo max($i, 2); ?>').hide();
443
- e.find('#add_discount_line<?php echo $i; ?>').click(function () {
444
- if (<?php echo $i; ?> == 1 || ( e.find('#_bulkdiscount_quantity_<?php echo max($i-1, 1); ?>').val() != '' &&
445
  <?php if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) : ?>
446
- e.find('#_bulkdiscount_discount_flat_<?php echo max($i-1, 1); ?>').val() != ''
447
  <?php else: ?>
448
- e.find('#_bulkdiscount_discount_<?php echo max($i-1, 1); ?>').val() != ''
449
  <?php endif; ?>
450
  ) )
451
  {
452
- e.find('.block<?php echo $i; ?>').show(400);
453
- e.find('.options_group<?php echo min($i+1, 6); ?>').show(400);
454
- e.find('#add_discount_line<?php echo min($i+1, 5); ?>').show(400);
455
- e.find('#add_discount_line<?php echo $i; ?>').hide(400);
456
- e.find('#delete_discount_line<?php echo min($i+1, 6); ?>').show(400);
457
- e.find('#delete_discount_line<?php echo $i; ?>').hide(400);
458
  }
459
  else
460
  {
461
- alert('<?php _e( 'Please fill in the current line before adding new line.', 'wc_bulk_discount' ); ?>');
462
  }
463
- });
464
- e.find('#delete_discount_line<?php echo max($i, 1); ?>').hide();
465
- e.find('#delete_discount_line<?php echo $i; ?>').click(function () {
466
- e.find('.block<?php echo max($i-1, 1); ?>').hide(400);
467
- e.find('.options_group<?php echo min($i, 6); ?>').hide(400);
468
- e.find('#add_discount_line<?php echo min($i, 5); ?>').hide(400);
469
- e.find('#add_discount_line<?php echo max($i-1, 1); ?>').show(400);
470
- e.find('#delete_discount_line<?php echo min($i, 6); ?>').hide(400);
471
- e.find('#delete_discount_line<?php echo max($i-1, 2); ?>').show(400);
472
- e.find('#_bulkdiscount_quantity_<?php echo max($i-1, 1); ?>').val('');
473
  <?php
474
  if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) :
475
  ?>
476
- e.find('#_bulkdiscount_discount_flat_<?php echo max($i-1, 1); ?>').val('');
477
  <?php else: ?>
478
- e.find('#_bulkdiscount_discount_<?php echo max($i-1, 1); ?>').val('');
479
  <?php endif; ?>
480
- });
481
  <?php
482
  endfor;
483
  for ($i = 1, $j = 2; $i <= 5; $i++, $j++) {
484
  $cnt = 1;
485
  if (get_post_meta($thepostid, "_bulkdiscount_quantity_$i", true) || get_post_meta($thepostid, "_bulkdiscount_quantity_$j", true)) {
486
  ?>
487
- e.find('.block<?php echo $i; ?>').show();
488
- e.find('.options_group<?php echo $i; ?>').show();
489
- e.find('#add_discount_line<?php echo $i; ?>').hide();
490
- e.find('#delete_discount_line<?php echo $i; ?>').hide();
491
- e.find('.options_group<?php echo min($i+1,6); ?>').show();
492
- e.find('#add_discount_line<?php echo min($i+1,6); ?>').show();
493
- e.find('#delete_discount_line<?php echo min($i+1,6); ?>').show();
494
  <?php
495
  $cnt++;
496
  }
497
  }
498
  if ($cnt >= 6) {
499
- ?>e.find('#add_discount_line6').show();
500
  <?php
501
  }
502
  ?>
503
- });
504
  </script>
505
 
506
  <div id="bulkdiscount_product_data" class="panel woocommerce_options_panel">
@@ -566,18 +607,22 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
566
  * Enqueue frontend dependencies.
567
  */
568
  public function action_enqueue_dependencies() {
 
569
  wp_register_style( 'woocommercebulkdiscount-style', plugins_url( 'css/style.css', __FILE__ ) );
570
  wp_enqueue_style( 'woocommercebulkdiscount-style' );
571
  wp_enqueue_script( 'jquery' );
 
572
  }
573
 
574
  /**
575
  * Enqueue backend dependencies.
576
  */
577
  public function action_enqueue_dependencies_admin() {
 
578
  wp_register_style( 'woocommercebulkdiscount-style-admin', plugins_url( 'css/admin.css', __FILE__ ) );
579
  wp_enqueue_style( 'woocommercebulkdiscount-style-admin' );
580
  wp_enqueue_script( 'jquery' );
 
581
  }
582
 
583
  /**
@@ -586,12 +631,15 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
586
  * @param $post_id
587
  */
588
  public function action_process_meta( $post_id ) {
 
589
  if ( isset( $_POST['_bulkdiscount_text_info'] ) ) update_post_meta( $post_id, '_bulkdiscount_text_info', stripslashes( $_POST['_bulkdiscount_text_info'] ) );
 
590
  if ( isset( $_POST['_bulkdiscount_enabled'] ) && $_POST['_bulkdiscount_enabled'] == 'yes' ) {
591
  update_post_meta( $post_id, '_bulkdiscount_enabled', stripslashes( $_POST['_bulkdiscount_enabled'] ) );
592
  } else {
593
  update_post_meta( $post_id, '_bulkdiscount_enabled', stripslashes( 'no' ) );
594
  }
 
595
  for ( $i = 1; $i <= 5; $i++ ) {
596
  if ( isset( $_POST["_bulkdiscount_quantity_$i"] ) ) update_post_meta( $post_id, "_bulkdiscount_quantity_$i", stripslashes( $_POST["_bulkdiscount_quantity_$i"] ) );
597
  if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ) {
@@ -600,6 +648,7 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
600
  if ( isset( $_POST["_bulkdiscount_discount_$i"] ) ) update_post_meta( $post_id, "_bulkdiscount_discount_$i", stripslashes( $_POST["_bulkdiscount_discount_$i"] ) );
601
  }
602
  }
 
603
  }
604
 
605
  /**
@@ -607,12 +656,14 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
607
  * @return void
608
  */
609
  public function add_tab() {
 
610
  foreach ( $this->settings_tabs as $name => $label ) {
611
  $class = 'nav-tab';
612
  if ( $this->current_tab == $name )
613
  $class .= ' nav-tab-active';
614
  echo '<a href="' . admin_url( 'admin.php?page=woocommerce&tab=' . $name ) . '" class="' . $class . '">' . $label . '</a>';
615
  }
 
616
  }
617
 
618
  /**
@@ -620,6 +671,7 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
620
  * @return void
621
  */
622
  public function settings_tab_action() {
 
623
  global $woocommerce_settings;
624
 
625
  // Determine the current tab in effect.
@@ -629,12 +681,14 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
629
 
630
  // Display settings for this tab (make sure to add the settings to the tab).
631
  woocommerce_admin_fields( $woocommerce_settings[$current_tab] );
 
632
  }
633
 
634
  /**
635
  * Save settings in a single field in the database for each tab's fields (one field per tab).
636
  */
637
  public function save_settings() {
 
638
  global $woocommerce_settings;
639
 
640
  // Make sure our settings fields are recognised.
@@ -642,13 +696,16 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
642
 
643
  $current_tab = $this->get_tab_in_view( current_filter(), 'woocommerce_update_options_' );
644
  woocommerce_update_options( $woocommerce_settings[$current_tab] );
 
645
  }
646
 
647
  /**
648
  * Get the tab current in view/processing.
649
  */
650
  public function get_tab_in_view( $current_filter, $filter_base ) {
 
651
  return str_replace( $filter_base, '', $current_filter );
 
652
  }
653
 
654
 
@@ -710,6 +767,14 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
710
  'default' => 'yes'
711
  ),
712
 
 
 
 
 
 
 
 
 
713
  array(
714
  'name' => __( 'Show discount information next to cart item price', 'wc_bulk_discount' ),
715
  'id' => 'woocommerce_t4m_show_on_item',
@@ -762,7 +827,7 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
762
  array( 'type' => 'sectionend', 'id' => 't4m_bulk_discounts_options' ),
763
 
764
  array(
765
- 'desc' => 'If you find the WooCommerce Bulk Discount extension useful, please rate it <a target="_blank" href="http://wordpress.org/support/view/plugin-reviews/woocommerce-bulk-discount#postform">&#9733;&#9733;&#9733;&#9733;&#9733;</a>. You can also contact us if you want a custom tailored WooCommerce extension.',
766
  'id' => 'woocommerce_t4m_bulk_discount_notice_text',
767
  'type' => 'title'
768
  ),
@@ -801,16 +866,25 @@ if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
801
  * @param $js
802
  */
803
  protected function run_js( $js ) {
 
804
  global $woocommerce;
 
805
  if ( function_exists( 'wc_enqueue_js' ) ) {
806
  wc_enqueue_js( $js );
807
  } else {
808
  $woocommerce->add_inline_js( $js );
809
  }
 
810
  }
811
 
812
- protected function is_coupon_applied() {
 
 
 
 
813
  global $woocommerce;
 
 
814
  return !( empty( $woocommerce->cart->applied_coupons ) );
815
  }
816
 
4
  Plugin URI: http://www.renepuchinger.com
5
  Description: Apply fine-grained bulk discounts to items in the shopping cart.
6
  Author: Rene Puchinger
7
+ Version: 2.1.5
8
  Author URI: http://www.renepuchinger.com
9
  License: GPL3
10
 
61
  // Add the settings fields to each tab.
62
  add_action( 'woocommerce_bulk_discount_settings', array( $this, 'add_settings_fields' ), 10 );
63
 
64
+ add_action( 'woocommerce_loaded', array( $this, 'woocommerce_loaded' ) );
65
+
66
+ }
67
+
68
+ /**
69
+ * Main processing hooks
70
+ */
71
+ public function woocommerce_loaded() {
72
+
73
  if ( get_option( 'woocommerce_t4m_enable_bulk_discounts', 'yes' ) == 'yes' ) {
74
  add_action( 'woocommerce_before_calculate_totals', array( $this, 'action_before_calculate' ), 10, 1 );
75
  add_action( 'woocommerce_calculate_totals', array( $this, 'action_after_calculate' ), 10, 1 );
76
  add_action( 'woocommerce_before_cart_table', array( $this, 'before_cart_table' ) );
77
  add_action( 'woocommerce_single_product_summary', array( $this, 'single_product_summary' ), 45 );
78
+ add_filter( 'woocommerce_cart_item_price_html', array( $this, 'filter_item_price' ), 10, 2 );
 
 
 
 
79
  add_filter( 'woocommerce_cart_item_subtotal', array( $this, 'filter_subtotal_price' ), 10, 2 );
80
  add_filter( 'woocommerce_checkout_item_subtotal', array( $this, 'filter_subtotal_price' ), 10, 2 );
81
  add_filter( 'woocommerce_order_formatted_line_subtotal', array( $this, 'filter_subtotal_order_price' ), 10, 3 );
112
  * @return float
113
  */
114
  protected function get_discounted_coeff( $product_id, $quantity ) {
115
+
116
  $q = array( 0.0 );
117
  $d = array( 0.0 );
118
+
119
  /* Find the appropriate discount coefficient by looping through up to the five discount settings */
120
  for ( $i = 1; $i <= 5; $i++ ) {
121
  array_push( $q, get_post_meta( $product_id, "_bulkdiscount_quantity_$i", true ) );
129
  $d[0] = $d[$i];
130
  }
131
  }
132
+
133
  // for percentage discount convert the resulting discount from % to the multiplying coefficient
134
  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 ) );
135
+
136
  }
137
 
138
  /**
143
  * @return string
144
  */
145
  public function filter_item_price( $price, $values ) {
146
+
147
  if ( !$values || @!$values['data'] ) {
148
  return $price;
149
  }
150
+ if ( $this->coupon_check() ) {
151
  return $price;
152
  }
153
  $_product = $values['data'];
176
  return "<span class='discount-info' title='" . sprintf( __( '%s%% bulk discount applied!', 'wc_bulk_discount' ), round( ( 1.0 - $coeff ) * 100.0, 2 ) ) . "'>" .
177
  "<span class='old-price' style='$old_css'>$oldprice</span>" .
178
  "<span class='new-price' style='$new_css'>$discprice</span></span>";
179
+
180
  }
181
 
182
  /**
187
  * @return string
188
  */
189
  public function filter_subtotal_price( $price, $values ) {
190
+
191
  if ( !$values || !$values['data'] ) {
192
  return $price;
193
  }
194
+ if ( $this->coupon_check() ) {
195
  return $price;
196
  }
197
  $_product = $values['data'];
212
  }
213
  $new_css = esc_attr( get_option( 'woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;' ) );
214
  $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 ) . "%" ) ) );
215
+
216
  return "<span class='discount-info' title='$bulk_info'>" .
217
  "<span>$price</span>" .
218
  "<span class='new-price' style='$new_css'> ($bulk_info)</span></span>";
219
+
220
  }
221
 
222
  /**
223
  * Gather discount information to the array $this->discount_coefs
224
  */
225
  protected function gather_discount_coeffs() {
226
+
227
  global $woocommerce;
228
+
229
  $cart = $woocommerce->cart;
230
  $this->discount_coeffs = array();
231
+
232
  if ( sizeof( $cart->cart_contents ) > 0 ) {
233
  foreach ( $cart->cart_contents as $cart_item_key => $values ) {
234
  $_product = $values['data'];
249
  $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] = $_product->get_price();
250
  }
251
  }
252
+
253
  }
254
 
255
  /**
260
  * @return string
261
  */
262
  public function filter_subtotal_order_price( $price, $values, $order ) {
263
+
264
  if ( !$values || !$order ) {
265
  return $price;
266
  }
267
+ if ( $this->coupon_check() ) {
268
  return $price;
269
  }
270
+
271
  $_product = get_product( $values['product_id'] );
272
  if ( get_post_meta( $values['product_id'], "_bulkdiscount_enabled", true ) != '' && get_post_meta( $values['product_id'], "_bulkdiscount_enabled", true ) !== 'yes' ) {
273
  return $price;
293
  }
294
  $new_css = esc_attr( get_option( 'woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;' ) );
295
  $bulk_info = sprintf( __( 'Incl. %s discount', 'wc_bulk_discount' ), ( $discount_type == 'flat' ? get_woocommerce_currency_symbol() . $coeff : ( round( ( 1 - $coeff ) * 100, 2 ) . "%" ) ) );
296
+
297
  return "<span class='discount-info' title='$bulk_info'>" .
298
  "<span>$price</span>" .
299
  "<span class='new-price' style='$new_css'> ($bulk_info)</span></span>";
300
+
301
  }
302
 
303
  /**
307
  * @return array
308
  */
309
  protected function gather_discount_coeffs_from_order( $order_id ) {
310
+
311
  $meta = get_post_meta( $order_id, '_woocommerce_t4m_discount_coeffs', true );
312
+
313
  if ( !$meta ) {
314
  return null;
315
  }
316
+
317
  $order_discount_coeffs = json_decode( $meta, true );
318
  return $order_discount_coeffs;
319
+
320
  }
321
 
322
  /**
326
  */
327
  public function action_before_calculate( WC_Cart $cart ) {
328
 
329
+ if ( $this->coupon_check() ) {
330
  return;
331
  }
332
 
347
  $values['data']->set_price( $row_base_price );
348
  }
349
  }
350
+
351
  }
352
 
353
  /**
355
  * @return int
356
  */
357
  protected function get_actual_id( $product ) {
358
+
359
  if ( $product instanceof WC_Product_Variation ) {
360
  return $product->variation_id;
361
  } else {
362
  return $product->id;
363
  }
364
+
365
  }
366
 
367
  /**
371
  */
372
  public function action_after_calculate( WC_Cart $cart ) {
373
 
374
+ if ( $this->coupon_check() ) {
375
  return;
376
  }
377
 
384
  $values['data']->set_price( $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] );
385
  }
386
  }
387
+
388
  }
389
 
390
  /**
391
  * Show discount info in cart.
392
  */
393
  public function before_cart_table() {
394
+
395
  if ( get_option( 'woocommerce_t4m_cart_info' ) != '' ) {
396
  echo "<div class='cart-show-discounts'>";
397
  echo get_option( 'woocommerce_t4m_cart_info' );
398
  echo "</div>";
399
  }
400
+
401
  }
402
 
403
  /**
410
  * @return string
411
  */
412
  public function filter_cart_product_subtotal( $subtotal, $_product, $quantity ) {
413
+
414
  if ( !$_product || !$quantity ) {
415
  return $subtotal;
416
  }
417
+ if ( $this->coupon_check() ) {
418
  return $subtotal;
419
  }
420
  if ( get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) != '' && get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) !== 'yes' ) {
421
  return $subtotal;
422
  }
423
+
424
  $coeff = $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'];
425
  if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ) {
426
  $newsubtotal = woocommerce_price( max( 0, ( $_product->get_price() * $quantity ) - $coeff ) );
427
  } else {
428
  $newsubtotal = woocommerce_price( $_product->get_price() * $quantity * $coeff );
429
  }
430
+
431
  return $newsubtotal;
432
+
433
  }
434
 
435
  /**
438
  * @param $order_id
439
  */
440
  public function order_update_meta( $order_id ) {
441
+
442
  update_post_meta( $order_id, "_woocommerce_t4m_discount_type", get_option( 'woocommerce_t4m_discount_type', '' ) );
443
  update_post_meta( $order_id, "_woocommerce_t4m_discount_coeffs", json_encode( $this->discount_coeffs ) );
444
+
445
  }
446
 
447
  /**
448
  * Display discount information in Product Detail.
449
  */
450
  public function single_product_summary() {
451
+
452
  global $thepostid, $post;
453
  if ( !$thepostid ) $thepostid = $post->ID;
454
+
455
  echo "<div class='productinfo-show-discounts'>";
456
  echo get_post_meta( $thepostid, '_bulkdiscount_text_info', true );
457
  echo "</div>";
458
+
459
  }
460
 
461
  /**
473
  if ( !$thepostid ) $thepostid = $post->ID;
474
  ?>
475
  <script type="text/javascript">
476
+ jQuery( document ).ready( function () {
477
+ var e = jQuery( '#bulkdiscount_product_data' );
478
  <?php
479
  for($i = 1; $i <= 6; $i++) :
480
  ?>
481
+ e.find( '.block<?php echo $i; ?>' ).hide();
482
+ e.find( '.options_group<?php echo max($i, 2); ?>' ).hide();
483
+ e.find( '#add_discount_line<?php echo max($i, 2); ?>' ).hide();
484
+ e.find( '#add_discount_line<?php echo $i; ?>' ).click( function () {
485
+ if ( <?php echo $i; ?> == 1 || ( e.find( '#_bulkdiscount_quantity_<?php echo max($i-1, 1); ?>' ).val() != '' &&
486
  <?php if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) : ?>
487
+ e.find( '#_bulkdiscount_discount_flat_<?php echo max($i-1, 1); ?>' ).val() != ''
488
  <?php else: ?>
489
+ e.find( '#_bulkdiscount_discount_<?php echo max($i-1, 1); ?>' ).val() != ''
490
  <?php endif; ?>
491
  ) )
492
  {
493
+ e.find( '.block<?php echo $i; ?>' ).show( 400 );
494
+ e.find( '.options_group<?php echo min($i+1, 6); ?>' ).show( 400 );
495
+ e.find( '#add_discount_line<?php echo min($i+1, 5); ?>' ).show( 400 );
496
+ e.find( '#add_discount_line<?php echo $i; ?>' ).hide( 400 );
497
+ e.find( '#delete_discount_line<?php echo min($i+1, 6); ?>' ).show( 400 );
498
+ e.find( '#delete_discount_line<?php echo $i; ?>' ).hide( 400 );
499
  }
500
  else
501
  {
502
+ alert( '<?php _e( 'Please fill in the current line before adding new line.', 'wc_bulk_discount' ); ?>' );
503
  }
504
+ } );
505
+ e.find( '#delete_discount_line<?php echo max($i, 1); ?>' ).hide();
506
+ e.find( '#delete_discount_line<?php echo $i; ?>' ).click( function () {
507
+ e.find( '.block<?php echo max($i-1, 1); ?>' ).hide( 400 );
508
+ e.find( '.options_group<?php echo min($i, 6); ?>' ).hide( 400 );
509
+ e.find( '#add_discount_line<?php echo min($i, 5); ?>' ).hide( 400 );
510
+ e.find( '#add_discount_line<?php echo max($i-1, 1); ?>' ).show( 400 );
511
+ e.find( '#delete_discount_line<?php echo min($i, 6); ?>' ).hide( 400 );
512
+ e.find( '#delete_discount_line<?php echo max($i-1, 2); ?>' ).show( 400 );
513
+ e.find( '#_bulkdiscount_quantity_<?php echo max($i-1, 1); ?>' ).val( '' );
514
  <?php
515
  if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) :
516
  ?>
517
+ e.find( '#_bulkdiscount_discount_flat_<?php echo max($i-1, 1); ?>' ).val( '' );
518
  <?php else: ?>
519
+ e.find( '#_bulkdiscount_discount_<?php echo max($i-1, 1); ?>' ).val( '' );
520
  <?php endif; ?>
521
+ } );
522
  <?php
523
  endfor;
524
  for ($i = 1, $j = 2; $i <= 5; $i++, $j++) {
525
  $cnt = 1;
526
  if (get_post_meta($thepostid, "_bulkdiscount_quantity_$i", true) || get_post_meta($thepostid, "_bulkdiscount_quantity_$j", true)) {
527
  ?>
528
+ e.find( '.block<?php echo $i; ?>' ).show();
529
+ e.find( '.options_group<?php echo $i; ?>' ).show();
530
+ e.find( '#add_discount_line<?php echo $i; ?>' ).hide();
531
+ e.find( '#delete_discount_line<?php echo $i; ?>' ).hide();
532
+ e.find( '.options_group<?php echo min($i+1,6); ?>' ).show();
533
+ e.find( '#add_discount_line<?php echo min($i+1,6); ?>' ).show();
534
+ e.find( '#delete_discount_line<?php echo min($i+1,6); ?>' ).show();
535
  <?php
536
  $cnt++;
537
  }
538
  }
539
  if ($cnt >= 6) {
540
+ ?>e.find( '#add_discount_line6' ).show();
541
  <?php
542
  }
543
  ?>
544
+ } );
545
  </script>
546
 
547
  <div id="bulkdiscount_product_data" class="panel woocommerce_options_panel">
607
  * Enqueue frontend dependencies.
608
  */
609
  public function action_enqueue_dependencies() {
610
+
611
  wp_register_style( 'woocommercebulkdiscount-style', plugins_url( 'css/style.css', __FILE__ ) );
612
  wp_enqueue_style( 'woocommercebulkdiscount-style' );
613
  wp_enqueue_script( 'jquery' );
614
+
615
  }
616
 
617
  /**
618
  * Enqueue backend dependencies.
619
  */
620
  public function action_enqueue_dependencies_admin() {
621
+
622
  wp_register_style( 'woocommercebulkdiscount-style-admin', plugins_url( 'css/admin.css', __FILE__ ) );
623
  wp_enqueue_style( 'woocommercebulkdiscount-style-admin' );
624
  wp_enqueue_script( 'jquery' );
625
+
626
  }
627
 
628
  /**
631
  * @param $post_id
632
  */
633
  public function action_process_meta( $post_id ) {
634
+
635
  if ( isset( $_POST['_bulkdiscount_text_info'] ) ) update_post_meta( $post_id, '_bulkdiscount_text_info', stripslashes( $_POST['_bulkdiscount_text_info'] ) );
636
+
637
  if ( isset( $_POST['_bulkdiscount_enabled'] ) && $_POST['_bulkdiscount_enabled'] == 'yes' ) {
638
  update_post_meta( $post_id, '_bulkdiscount_enabled', stripslashes( $_POST['_bulkdiscount_enabled'] ) );
639
  } else {
640
  update_post_meta( $post_id, '_bulkdiscount_enabled', stripslashes( 'no' ) );
641
  }
642
+
643
  for ( $i = 1; $i <= 5; $i++ ) {
644
  if ( isset( $_POST["_bulkdiscount_quantity_$i"] ) ) update_post_meta( $post_id, "_bulkdiscount_quantity_$i", stripslashes( $_POST["_bulkdiscount_quantity_$i"] ) );
645
  if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ) {
648
  if ( isset( $_POST["_bulkdiscount_discount_$i"] ) ) update_post_meta( $post_id, "_bulkdiscount_discount_$i", stripslashes( $_POST["_bulkdiscount_discount_$i"] ) );
649
  }
650
  }
651
+
652
  }
653
 
654
  /**
656
  * @return void
657
  */
658
  public function add_tab() {
659
+
660
  foreach ( $this->settings_tabs as $name => $label ) {
661
  $class = 'nav-tab';
662
  if ( $this->current_tab == $name )
663
  $class .= ' nav-tab-active';
664
  echo '<a href="' . admin_url( 'admin.php?page=woocommerce&tab=' . $name ) . '" class="' . $class . '">' . $label . '</a>';
665
  }
666
+
667
  }
668
 
669
  /**
671
  * @return void
672
  */
673
  public function settings_tab_action() {
674
+
675
  global $woocommerce_settings;
676
 
677
  // Determine the current tab in effect.
681
 
682
  // Display settings for this tab (make sure to add the settings to the tab).
683
  woocommerce_admin_fields( $woocommerce_settings[$current_tab] );
684
+
685
  }
686
 
687
  /**
688
  * Save settings in a single field in the database for each tab's fields (one field per tab).
689
  */
690
  public function save_settings() {
691
+
692
  global $woocommerce_settings;
693
 
694
  // Make sure our settings fields are recognised.
696
 
697
  $current_tab = $this->get_tab_in_view( current_filter(), 'woocommerce_update_options_' );
698
  woocommerce_update_options( $woocommerce_settings[$current_tab] );
699
+
700
  }
701
 
702
  /**
703
  * Get the tab current in view/processing.
704
  */
705
  public function get_tab_in_view( $current_filter, $filter_base ) {
706
+
707
  return str_replace( $filter_base, '', $current_filter );
708
+
709
  }
710
 
711
 
767
  'default' => 'yes'
768
  ),
769
 
770
+ array(
771
+ 'name' => __( 'Remove any bulk discounts if a coupon code is applied', 'wc_bulk_discount' ),
772
+ 'id' => 'woocommerce_t4m_remove_discount_on_coupon',
773
+ 'std' => 'yes',
774
+ 'type' => 'checkbox',
775
+ 'default' => 'yes'
776
+ ),
777
+
778
  array(
779
  'name' => __( 'Show discount information next to cart item price', 'wc_bulk_discount' ),
780
  'id' => 'woocommerce_t4m_show_on_item',
827
  array( 'type' => 'sectionend', 'id' => 't4m_bulk_discounts_options' ),
828
 
829
  array(
830
+ 'desc' => 'If you find the WooCommerce Bulk Discount extension useful, please rate it <a target="_blank" href="http://wordpress.org/support/view/plugin-reviews/woocommerce-bulk-discount#postform">&#9733;&#9733;&#9733;&#9733;&#9733;</a>.',
831
  'id' => 'woocommerce_t4m_bulk_discount_notice_text',
832
  'type' => 'title'
833
  ),
866
  * @param $js
867
  */
868
  protected function run_js( $js ) {
869
+
870
  global $woocommerce;
871
+
872
  if ( function_exists( 'wc_enqueue_js' ) ) {
873
  wc_enqueue_js( $js );
874
  } else {
875
  $woocommerce->add_inline_js( $js );
876
  }
877
+
878
  }
879
 
880
+ /**
881
+ * @return bool
882
+ */
883
+ protected function coupon_check() {
884
+
885
  global $woocommerce;
886
+
887
+ if ( get_option( 'woocommerce_t4m_remove_discount_on_coupon', 'yes' ) == 'no' ) return false;
888
  return !( empty( $woocommerce->cart->applied_coupons ) );
889
  }
890