WooCommerce Bulk Discount - Version 2.0.2

Version Description

  • (5 Sep 2013) Important maintenance release. Now the bulk discount metadata are stored to orders as well, making it possible to correctly display discounts for past orders. One can also change the plugin settings any time.
  • Added quick link to settings on Wordpress > Plugins page.
Download this release

Release Info

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

Code changes from version 2.0.1 to 2.0.2

Files changed (2) hide show
  1. readme.txt +9 -3
  2. woocommerce-bulk-discount.php +37 -31
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.1
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
@@ -38,8 +38,7 @@ policies to be adopted in your store.
38
 
39
  Here is the list of main features:
40
 
41
- * Possibility of setting percentage bulk discount or flat (fixed) bulk discount.
42
- * Possibility of setting a flat (fixed) discount in currency units.
43
  * Bulk discount for product variations supported to treat them as a whole when discounting.
44
  * Discount is better visible and is present on more places (see below).
45
  * Discount is visible on Checkout page
@@ -130,6 +129,10 @@ Setting bulk discounts couldn't have been easier.
130
 
131
  == Changelog ==
132
 
 
 
 
 
133
  = 2.0.1 =
134
  * (3 Sep 2013) Added a warning for changing discount type on a site with existing orders (currently it is safe to change discount type only for the first time after installing the plugin or on fresh WooCommerce installation with no orders).
135
 
@@ -171,6 +174,9 @@ Setting bulk discounts couldn't have been easier.
171
 
172
  == Upgrade Notice ==
173
 
 
 
 
174
  = 2.0.1 =
175
  Maintenance release.
176
 
4
  Tags: woocommerce, bulk, discount
5
  Requires at least: 3.5
6
  Tested up to: 3.6
7
+ Stable tag: 2.0.2
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
38
 
39
  Here is the list of main features:
40
 
41
+ * Possibility of setting percentage bulk discount or flat (fixed) bulk discount in currency units.
 
42
  * Bulk discount for product variations supported to treat them as a whole when discounting.
43
  * Discount is better visible and is present on more places (see below).
44
  * Discount is visible on Checkout page
129
 
130
  == Changelog ==
131
 
132
+ = 2.0.2 =
133
+ * (5 Sep 2013) Important maintenance release. Now the bulk discount metadata are stored to orders as well, making it possible to correctly display discounts for past orders. One can also change the plugin settings any time.
134
+ * Added quick link to settings on Wordpress > Plugins page.
135
+
136
  = 2.0.1 =
137
  * (3 Sep 2013) Added a warning for changing discount type on a site with existing orders (currently it is safe to change discount type only for the first time after installing the plugin or on fresh WooCommerce installation with no orders).
138
 
174
 
175
  == Upgrade Notice ==
176
 
177
+ = 2.0.2 =
178
+ Important maintenance release. Please update immediately.
179
+
180
  = 2.0.1 =
181
  Maintenance release.
182
 
woocommerce-bulk-discount.php CHANGED
@@ -4,7 +4,7 @@ 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.1
8
  Author URI: http://www.renepuchinger.com
9
  License: GPL3
10
 
@@ -51,6 +51,8 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
51
  add_action('admin_enqueue_scripts', array($this, 'action_enqueue_dependencies_admin'));
52
  add_action('wp_head', array($this, 'action_enqueue_dependencies'));
53
 
 
 
54
  add_action('woocommerce_settings_tabs', array($this, 'add_tab'), 10);
55
 
56
  // Run these actions when generating the settings tabs.
@@ -79,15 +81,32 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
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
  }
83
 
84
  }
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  /**
87
  * For given product, and quantity return the price modifying factor (percentage discount) or value to deduct (flat discount).
88
  *
89
  * @param $product_id
90
  * @param $quantity
 
91
  * @return float
92
  */
93
  protected function get_discounted_coeff($product_id, $quantity)
@@ -213,15 +232,16 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
213
  {
214
  global $woocommerce;
215
  $_product = get_product($values['product_id']);
216
- $discount_coeffs = $this->gather_discount_coeffs_from_order($order);
217
  $coeff = $discount_coeffs[$this->get_actual_id($_product)]['coeff'];
218
- if (( get_option('woocommerce_t4m_discount_type', '') == 'flat' && $coeff == 0 ) || (get_option('woocommerce_t4m_discount_type', '') == '' && $coeff == 1.0 ) ) {
 
219
  return $price; // no price modification
220
  }
221
  $oldprice = woocommerce_price($discount_coeffs[$this->get_actual_id($_product)]['orig_price']);
222
  $old_css = esc_attr(get_option('woocommerce_t4m_css_old_price', 'color: #777; text-decoration: line-through; margin-right: 4px;'));
223
  $new_css = esc_attr(get_option('woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;'));
224
- $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) . "%")));
225
  return "<span class='discount-info' title='$bulk_info'>" .
226
  "<span>$price</span>" .
227
  "<span class='new-price' style='$new_css'> ($bulk_info)</span></span>";
@@ -230,32 +250,12 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
230
  /**
231
  * Gather discount information from order.
232
  *
233
- * @param $order
234
  * @return array
235
  */
236
- protected function gather_discount_coeffs_from_order($order)
237
  {
238
- global $woocommerce;
239
- $order_discount_coeffs = array();
240
- if (sizeof($order->get_items()) > 0) {
241
- foreach($order->get_items() as $item) {
242
- $_product = get_product($item['product_id']);
243
- $quantity = 0;
244
- if (get_option('woocommerce_t4m_variations_separate', 'yes') == 'no' && $_product instanceof WC_Product_Variable) {
245
- foreach($order->get_items() as $itemInner) {
246
- $p = get_product($itemInner['product_id']);
247
- if ($p instanceof WC_Product_Variable && $p->id == $_product->id) {
248
- $quantity += $itemInner['qty'];
249
- $order_discount_coeffs[$_product->variation_id]['quantity'] = $quantity;
250
- }
251
- }
252
- } else {
253
- $quantity = $item['qty'];
254
- }
255
- $order_discount_coeffs[$this->get_actual_id($_product)]['coeff'] = $this->get_discounted_coeff($_product->id, $quantity);
256
- $order_coeffs[$this->get_actual_id($_product)]['orig_price'] = $_product->get_price();
257
- }
258
- }
259
  return $order_discount_coeffs;
260
  }
261
 
@@ -337,6 +337,16 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
337
  return $newsubtotal;
338
  }
339
 
 
 
 
 
 
 
 
 
 
 
340
  /**
341
  * Display discount information in Product Detail.
342
  */
@@ -470,7 +480,6 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
470
  } else {
471
  if (isset($_POST["_bulkdiscount_discount_$i"])) update_post_meta($post_id, "_bulkdiscount_discount_$i", stripslashes($_POST["_bulkdiscount_discount_$i"]));
472
  }
473
-
474
  }
475
  }
476
 
@@ -639,9 +648,6 @@ if (!class_exists('Woo_Bulk_Discount_Plugin_t4m')) {
639
 
640
  }).change();
641
 
642
- jQuery('#woocommerce_t4m_discount_type').change(function(){
643
- alert('In current version of Bulk Discount you are safe to change Discount Type for the first time after plugin installation or on a new site with no orders yet. Otherwise, changing this parameter would later show invalid values in order details.');
644
- });
645
  ";
646
 
647
  if (class_exists('WC_Inline_Javascript_Helper')) {
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.2
8
  Author URI: http://www.renepuchinger.com
9
  License: GPL3
10
 
51
  add_action('admin_enqueue_scripts', array($this, 'action_enqueue_dependencies_admin'));
52
  add_action('wp_head', array($this, 'action_enqueue_dependencies'));
53
 
54
+ add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'action_links' ) );
55
+
56
  add_action('woocommerce_settings_tabs', array($this, 'add_tab'), 10);
57
 
58
  // Run these actions when generating the settings tabs.
81
  add_filter('woocommerce_product_write_panels', array($this, 'action_product_write_panels'));
82
  add_action('woocommerce_process_product_meta', array($this, 'action_process_meta'));
83
  add_filter('woocommerce_cart_product_subtotal', array($this, 'filter_cart_product_subtotal'), 10, 3);
84
+ add_action('woocommerce_checkout_update_order_meta', array($this, 'order_update_meta'));
85
  }
86
 
87
  }
88
 
89
+ /**
90
+ * Add action links under WordPress > Plugins
91
+ *
92
+ * @param $links
93
+ * @return array
94
+ */
95
+ public function action_links( $links ) {
96
+
97
+ $plugin_links = array(
98
+ '<a href="' . admin_url( 'admin.php?page=woocommerce&tab=bulk_discount' ) . '">' . __( 'Settings', 'woocommerce' ) . '</a>',
99
+ );
100
+
101
+ return array_merge( $plugin_links, $links );
102
+ }
103
+
104
  /**
105
  * For given product, and quantity return the price modifying factor (percentage discount) or value to deduct (flat discount).
106
  *
107
  * @param $product_id
108
  * @param $quantity
109
+ * @param $order
110
  * @return float
111
  */
112
  protected function get_discounted_coeff($product_id, $quantity)
232
  {
233
  global $woocommerce;
234
  $_product = get_product($values['product_id']);
235
+ $discount_coeffs = $this->gather_discount_coeffs_from_order($order->id);
236
  $coeff = $discount_coeffs[$this->get_actual_id($_product)]['coeff'];
237
+ $discount_type = get_post_meta($order->id, '_woocommerce_t4m_discount_type', true);
238
+ if (( $discount_type == 'flat' && $coeff == 0 ) || ($discount_type == '' && $coeff == 1.0 ) ) {
239
  return $price; // no price modification
240
  }
241
  $oldprice = woocommerce_price($discount_coeffs[$this->get_actual_id($_product)]['orig_price']);
242
  $old_css = esc_attr(get_option('woocommerce_t4m_css_old_price', 'color: #777; text-decoration: line-through; margin-right: 4px;'));
243
  $new_css = esc_attr(get_option('woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;'));
244
+ $bulk_info = sprintf(__('Incl. %s discount', 'wc_bulk_discount'), ($discount_type == 'flat' ? get_woocommerce_currency_symbol() . $coeff : (round((1 - $coeff) * 100) . "%")));
245
  return "<span class='discount-info' title='$bulk_info'>" .
246
  "<span>$price</span>" .
247
  "<span class='new-price' style='$new_css'> ($bulk_info)</span></span>";
250
  /**
251
  * Gather discount information from order.
252
  *
253
+ * @param $order_id
254
  * @return array
255
  */
256
+ protected function gather_discount_coeffs_from_order($order_id)
257
  {
258
+ $order_discount_coeffs = json_decode(get_post_meta($order_id, '_woocommerce_t4m_discount_coeffs', true), true);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
  return $order_discount_coeffs;
260
  }
261
 
337
  return $newsubtotal;
338
  }
339
 
340
+ /**
341
+ * Store discount info to order as well
342
+ *
343
+ * @param $order_id
344
+ */
345
+ public function order_update_meta($order_id) {
346
+ update_post_meta($order_id, "_woocommerce_t4m_discount_type", get_option('woocommerce_t4m_discount_type', ''));
347
+ update_post_meta($order_id, "_woocommerce_t4m_discount_coeffs", json_encode($this->discount_coeffs));
348
+ }
349
+
350
  /**
351
  * Display discount information in Product Detail.
352
  */
480
  } else {
481
  if (isset($_POST["_bulkdiscount_discount_$i"])) update_post_meta($post_id, "_bulkdiscount_discount_$i", stripslashes($_POST["_bulkdiscount_discount_$i"]));
482
  }
 
483
  }
484
  }
485
 
648
 
649
  }).change();
650
 
 
 
 
651
  ";
652
 
653
  if (class_exists('WC_Inline_Javascript_Helper')) {