WooCommerce Bulk Discount - Version 1.1

Version Description

  • (7 Jul 2013) resolved major issue of incorrect discount application in some cases.
  • code optimization.
  • cleaned up some code.
Download this release

Release Info

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

Code changes from version 1.0.1 to 1.1

Files changed (2) hide show
  1. readme.txt +16 -5
  2. woocommerce-bulk-discount.php +45 -17
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_butt
4
  Tags: woocommerce, discount
5
  Requires at least: 3.5.0
6
  Tested up to: 3.5.2
7
- Stable tag: 1.0.1
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
@@ -14,7 +14,9 @@ Apply fine-grained discounts to items in the shopping cart, dependently on order
14
 
15
  WooCommerce Bulk Discount Plugin makes possible to apply fine-grained discounts
16
  to items in the shopping cart, dependently on ordered quantity and on concrete
17
- product. The plugin was tested with WooCommerce versions 1.6.4 to 2.0.12.
 
 
18
 
19
  Let us examine some examples of usage.
20
 
@@ -52,9 +54,10 @@ add up to five discount lines to fine-tune the discount setting.
52
 
53
  == Frequently Asked Questions ==
54
 
55
- = How many levels of discounting may be applied? =
56
 
57
- Currently 5 discount lines are supported. That should be enough for reasonable fine-tuning of the discount.
 
58
 
59
  == Screenshots ==
60
 
@@ -63,6 +66,11 @@ Currently 5 discount lines are supported. That should be enough for reasonable f
63
 
64
  == Changelog ==
65
 
 
 
 
 
 
66
  = 1.0.1 =
67
  * (5 Jul 2013) cleaned up some code.
68
 
@@ -71,6 +79,9 @@ Currently 5 discount lines are supported. That should be enough for reasonable f
71
 
72
  == Upgrade Notice ==
73
 
 
 
 
74
  = 1.0.1 =
75
  This version has minor change in code without any noticeable impact.
76
 
@@ -79,7 +90,7 @@ N/A.
79
 
80
  == Example of setting a discount ==
81
 
82
- Below is an example with three discount lines.
83
 
84
  Example:
85
 
4
  Tags: woocommerce, discount
5
  Requires at least: 3.5.0
6
  Tested up to: 3.5.2
7
+ Stable tag: 1.1
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
14
 
15
  WooCommerce Bulk Discount Plugin makes possible to apply fine-grained discounts
16
  to items in the shopping cart, dependently on ordered quantity and on concrete
17
+ product.
18
+
19
+ The plugin works with WooCommerce versions 2.0.x.
20
 
21
  Let us examine some examples of usage.
22
 
54
 
55
  == Frequently Asked Questions ==
56
 
57
+ = Are multiple discounts supported? How many levels of discounting may be applied? =
58
 
59
+ Yes, multiple discounts (related to single product) are supported. Currently one may set up
60
+ to 5 discount lines. That should be enough for reasonable fine-tuning of the discount.
61
 
62
  == Screenshots ==
63
 
66
 
67
  == Changelog ==
68
 
69
+ = 1.1 =
70
+ * (7 Jul 2013) resolved major issue of incorrect discount application in some cases.
71
+ * code optimization.
72
+ * cleaned up some code.
73
+
74
  = 1.0.1 =
75
  * (5 Jul 2013) cleaned up some code.
76
 
79
 
80
  == Upgrade Notice ==
81
 
82
+ = 1.1 =
83
+ Important bugfix release. Upgrading recommended as soon as possible.
84
+
85
  = 1.0.1 =
86
  This version has minor change in code without any noticeable impact.
87
 
90
 
91
  == Example of setting a discount ==
92
 
93
+ Below is an example of setting a multiple discount for some product with three discount lines.
94
 
95
  Example:
96
 
woocommerce-bulk-discount.php CHANGED
@@ -4,22 +4,45 @@ Plugin Name: WooCommerce Bulk Discount
4
  Plugin URI: http://www.tools4me.net/wordpress/woocommerce-bulk-discount-plugin
5
  Description: Apply fine-grained discounts to items in the shopping cart, dependently on ordered quantity and on concrete product.
6
  Author: Rene Puchinger
7
- Version: 1.0.1
8
  Author URI: http://www.renepuchinger.com
 
9
  */
10
 
11
- class Woo_Bulk_Discount_Plugin {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  public function __construct() {
14
  if (get_option('woocommerce_t4m_enable_bulk_discounts') == 'yes') {
15
- add_action('woocommerce_before_calculate_totals', array($this, 'action_before_calculate'));
16
- add_action('woocommerce_calculate_total', array($this, 'action_after_calculate'));
17
  add_action('woocommerce_before_cart_table', array($this, 'before_cart_table'));
18
  add_action('woocommerce_single_product_summary', array($this, 'single_product_summary'), 45);
19
  add_filter('woocommerce_cart_item_price_html', array($this, 'filter_item_price'), 10, 2);
20
  add_filter('woocommerce_product_write_panel_tabs', array($this, 'action_product_write_panel_tabs'));
21
  add_filter('woocommerce_product_write_panels', array($this, 'action_product_write_panels'));
22
  add_action('woocommerce_process_product_meta', array($this, 'action_process_meta'));
 
23
  }
24
 
25
  add_filter('woocommerce_general_settings', array($this, 'action_general_settings'));
@@ -27,7 +50,7 @@ class Woo_Bulk_Discount_Plugin {
27
  add_action('wp_head', array($this, 'action_enqueue_dependencies'));
28
  }
29
 
30
- private function get_discounted_coeff($prodId, $price, $quantity) {
31
  $q = array(0.0);
32
  $d = array(0.0);
33
  for ($i = 1; $i <= 5; $i++) {
@@ -44,13 +67,12 @@ class Woo_Bulk_Discount_Plugin {
44
  public function filter_item_price($price, $values) {
45
  $_product = $values['data'];
46
  $coeff = $this->get_discounted_coeff($_product->id, $_product->get_price(), $values['quantity']);
47
- $orig_price = $_product->get_price() / $coeff;
48
- $discount_info = ($coeff < 1.0) ? woocommerce_price($orig_price) : "";
49
- return "<span class='discount_price_info'><span>$discount_info</span></span>$price";
50
- }
51
 
52
  public function action_before_calculate(WC_Cart $cart) {
53
- $_SESSION['woocommerce_discount_cart'] = $cart;
54
  if ( sizeof( $cart->cart_contents ) > 0) {
55
  foreach ( $cart->cart_contents as $cart_item_key => $values ) {
56
  $_product = $values['data'];
@@ -60,12 +82,12 @@ class Woo_Bulk_Discount_Plugin {
60
  }
61
  }
62
 
63
- public function action_after_calculate() {
64
- $cart = $_SESSION['woocommerce_discount_cart'];
65
  if ( sizeof( $cart->cart_contents ) > 0) {
66
  foreach ( $cart->cart_contents as $cart_item_key => $values ) {
67
  $_product = $values['data'];
68
- $row_base_price = $_product->get_price() / $this->get_discounted_coeff($_product->id, $_product->get_price(), $values['quantity']);
 
69
  $values['data']->set_price($row_base_price);
70
  }
71
  }
@@ -77,6 +99,12 @@ class Woo_Bulk_Discount_Plugin {
77
  echo "</div>";
78
  }
79
 
 
 
 
 
 
 
80
  public function single_product_summary() {
81
  global $thepostid, $post;
82
  if (!$thepostid) $thepostid = $post->ID;
@@ -178,9 +206,9 @@ class Woo_Bulk_Discount_Plugin {
178
  }
179
 
180
  public function action_general_settings($settings) {
181
- array_push ($settings, array( 'name' => __( 'Bulk Discounts', 'woocommerce' ), 'type' => 'title','desc' => __('The following options are specific to product bulk discounts.', 'woocommerce'), 'id' => 't4m_bulk_discounts_options' ));
182
  array_push ($settings, array(
183
- 'name' => __('Bulk discounts globally enabled', 'woocommerce'),
184
  'id' => 'woocommerce_t4m_enable_bulk_discounts',
185
  'desc' => 'Enable bulk discounts',
186
  'std' => 'yes',
@@ -188,7 +216,7 @@ class Woo_Bulk_Discount_Plugin {
188
  )
189
  );
190
  array_push ($settings, array(
191
- 'name' => __('Optionally enter information about discounts visible on cart page', 'woocommerce'),
192
  'id' => 'woocommerce_t4m_cart_info',
193
  'type' => 'textarea',
194
  'css' => 'width:100%; height: 75px;'
@@ -200,4 +228,4 @@ class Woo_Bulk_Discount_Plugin {
200
 
201
  }
202
 
203
- $woo_bulk_discount_plugin = new Woo_Bulk_Discount_Plugin();
4
  Plugin URI: http://www.tools4me.net/wordpress/woocommerce-bulk-discount-plugin
5
  Description: Apply fine-grained discounts to items in the shopping cart, dependently on ordered quantity and on concrete product.
6
  Author: Rene Puchinger
7
+ Version: 1.1
8
  Author URI: http://www.renepuchinger.com
9
+ License: GPL3
10
  */
11
 
12
+ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
13
+
14
+ if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) return; // Check if WooCommerce is active
15
+
16
+ class Woo_Bulk_Discount_Plugin_t4m {
17
+
18
+ /*
19
+ Copyright (C) 2013 Rene Puchinger
20
+
21
+ This program is free software: you can redistribute it and/or modify
22
+ it under the terms of the GNU General Public License as published by
23
+ the Free Software Foundation, either version 3 of the License, or
24
+ (at your option) any later version.
25
+
26
+ This program is distributed in the hope that it will be useful,
27
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
28
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29
+ GNU General Public License for more details.
30
+
31
+ You should have received a copy of the GNU General Public License
32
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
33
+ */
34
 
35
  public function __construct() {
36
  if (get_option('woocommerce_t4m_enable_bulk_discounts') == 'yes') {
37
+ add_action('woocommerce_before_calculate_totals', array($this, 'action_before_calculate'), 10, 1);
38
+ add_action('woocommerce_calculate_totals', array($this, 'action_after_calculate'), 10, 1);
39
  add_action('woocommerce_before_cart_table', array($this, 'before_cart_table'));
40
  add_action('woocommerce_single_product_summary', array($this, 'single_product_summary'), 45);
41
  add_filter('woocommerce_cart_item_price_html', array($this, 'filter_item_price'), 10, 2);
42
  add_filter('woocommerce_product_write_panel_tabs', array($this, 'action_product_write_panel_tabs'));
43
  add_filter('woocommerce_product_write_panels', array($this, 'action_product_write_panels'));
44
  add_action('woocommerce_process_product_meta', array($this, 'action_process_meta'));
45
+ add_filter('woocommerce_cart_product_subtotal', array($this, 'filter_cart_product_subtotal'), 10, 3);
46
  }
47
 
48
  add_filter('woocommerce_general_settings', array($this, 'action_general_settings'));
50
  add_action('wp_head', array($this, 'action_enqueue_dependencies'));
51
  }
52
 
53
+ protected function get_discounted_coeff($prodId, $price, $quantity) {
54
  $q = array(0.0);
55
  $d = array(0.0);
56
  for ($i = 1; $i <= 5; $i++) {
67
  public function filter_item_price($price, $values) {
68
  $_product = $values['data'];
69
  $coeff = $this->get_discounted_coeff($_product->id, $_product->get_price(), $values['quantity']);
70
+ $oldprice = ($coeff < 1.0) ? woocommerce_price($_product->get_price()) : "";
71
+ $discprice = woocommerce_price($_product->get_price() * $coeff);
72
+ return "<span class='discount_price_info'><span>$oldprice</span></span>$discprice";
73
+ }
74
 
75
  public function action_before_calculate(WC_Cart $cart) {
 
76
  if ( sizeof( $cart->cart_contents ) > 0) {
77
  foreach ( $cart->cart_contents as $cart_item_key => $values ) {
78
  $_product = $values['data'];
82
  }
83
  }
84
 
85
+ public function action_after_calculate(WC_Cart $cart) {
 
86
  if ( sizeof( $cart->cart_contents ) > 0) {
87
  foreach ( $cart->cart_contents as $cart_item_key => $values ) {
88
  $_product = $values['data'];
89
+ $coeff = $this->get_discounted_coeff($_product->id, $_product->get_price(), $values['quantity']);
90
+ $row_base_price = $_product->get_price() / $coeff;
91
  $values['data']->set_price($row_base_price);
92
  }
93
  }
99
  echo "</div>";
100
  }
101
 
102
+ public function filter_cart_product_subtotal($subtotal, $_product, $quantity) {
103
+ $coeff = $this->get_discounted_coeff($_product->id, $_product->get_price(), $quantity);
104
+ $newsubtotal = woocommerce_price($_product->get_price() * $quantity * $coeff);
105
+ return $newsubtotal;
106
+ }
107
+
108
  public function single_product_summary() {
109
  global $thepostid, $post;
110
  if (!$thepostid) $thepostid = $post->ID;
206
  }
207
 
208
  public function action_general_settings($settings) {
209
+ array_push ($settings, array( 'name' => 'Bulk Discounts', 'type' => 'title','desc' => 'The following options are specific to product bulk discounts.', 'id' => 't4m_bulk_discounts_options' ));
210
  array_push ($settings, array(
211
+ 'name' => 'Bulk discounts globally enabled',
212
  'id' => 'woocommerce_t4m_enable_bulk_discounts',
213
  'desc' => 'Enable bulk discounts',
214
  'std' => 'yes',
216
  )
217
  );
218
  array_push ($settings, array(
219
+ 'name' => 'Optionally enter information about discounts visible on cart page',
220
  'id' => 'woocommerce_t4m_cart_info',
221
  'type' => 'textarea',
222
  'css' => 'width:100%; height: 75px;'
228
 
229
  }
230
 
231
+ $woo_bulk_discount_plugin = new Woo_Bulk_Discount_Plugin_t4m();