WooCommerce Bulk Discount - Version 1.1.1

Version Description

  • (21 Aug 2013) Plugin settings moved to separate tab under WooCommerce > Settings.
  • CSS refined.
  • code cleanup.
  • more code comments added.
Download this release

Release Info

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

Code changes from version 1.1 to 1.1.1

Files changed (4) hide show
  1. admin.css +2 -7
  2. readme.txt +12 -3
  3. screenshot-1.png +0 -0
  4. woocommerce-bulk-discount.php +364 -175
admin.css CHANGED
@@ -1,10 +1,5 @@
1
  #woocommerce-product-data ul.product_data_tabs li.bulkdiscount_options a {
2
- line-height: 16px;
3
- border-bottom: 1px solid #d5d5d5;
4
- text-shadow: 0 1px 1px #fff;
5
- color: #555;
6
- background: #f1f1f1 no-repeat 9px 9px;
7
- width: auto !important;
8
  }
9
 
10
  #bulkdiscount_product_data a.button-secondary {
@@ -13,4 +8,4 @@
13
 
14
  #bulkdiscount_product_data textarea {
15
  width: 60% !important;
16
- }
1
  #woocommerce-product-data ul.product_data_tabs li.bulkdiscount_options a {
2
+ padding: 5px 5px 5px 28px;
 
 
 
 
 
3
  }
4
 
5
  #bulkdiscount_product_data a.button-secondary {
8
 
9
  #bulkdiscount_product_data textarea {
10
  width: 60% !important;
11
+ }
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, 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
 
@@ -61,11 +61,17 @@ to 5 discount lines. That should be enough for reasonable fine-tuning of the dis
61
 
62
  == Screenshots ==
63
 
64
- 1. Enabling the plugin and setting information about discounts policy
65
  2. Setting the discount lines. Often only one line is sufficient.
66
 
67
  == Changelog ==
68
 
 
 
 
 
 
 
69
  = 1.1 =
70
  * (7 Jul 2013) resolved major issue of incorrect discount application in some cases.
71
  * code optimization.
@@ -79,6 +85,9 @@ to 5 discount lines. That should be enough for reasonable fine-tuning of the dis
79
 
80
  == Upgrade Notice ==
81
 
 
 
 
82
  = 1.1 =
83
  Important bugfix release. Upgrading recommended as soon as possible.
84
 
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6VEQ8XXK6B3UE
4
  Tags: woocommerce, discount
5
  Requires at least: 3.5.0
6
+ Tested up to: 3.6
7
+ Stable tag: 1.1.1
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
61
 
62
  == Screenshots ==
63
 
64
+ 1. Enabling the plugin and setting information about discounts policy.
65
  2. Setting the discount lines. Often only one line is sufficient.
66
 
67
  == Changelog ==
68
 
69
+ = 1.1.1 =
70
+ * (21 Aug 2013) Plugin settings moved to separate tab under WooCommerce > Settings.
71
+ * CSS refined.
72
+ * code cleanup.
73
+ * more code comments added.
74
+
75
  = 1.1 =
76
  * (7 Jul 2013) resolved major issue of incorrect discount application in some cases.
77
  * code optimization.
85
 
86
  == Upgrade Notice ==
87
 
88
+ = 1.1.1 =
89
+ Maintenance release.
90
+
91
  = 1.1 =
92
  Important bugfix release. Upgrading recommended as soon as possible.
93
 
screenshot-1.png CHANGED
Binary file
woocommerce-bulk-discount.php CHANGED
@@ -4,18 +4,11 @@ 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.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
@@ -30,202 +23,398 @@ class Woo_Bulk_Discount_Plugin_t4m {
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'));
49
- add_action('admin_enqueue_scripts', array($this, 'action_enqueue_dependencies_admin'));
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++) {
57
- array_push($q, get_post_meta($prodId, "_bulkdiscount_quantity_$i", true));
58
- array_push($d, get_post_meta($prodId, "_bulkdiscount_discount_$i", true));
59
- if ($quantity >= $q[$i] && $q[$i] > $q[0]) {
60
- $q[0] = $q[$i];
61
- $d[0] = $d[$i];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  }
64
- return (100.0 - $d[0]) / 100.0;
65
- }
66
 
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'];
79
- $row_base_price = $_product->get_price() * $this->get_discounted_coeff($_product->id, $_product->get_price(), $values['quantity']);
80
- $values['data']->set_price($row_base_price);
 
 
 
 
 
 
81
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  }
94
- }
95
 
96
- public function before_cart_table() {
97
- echo "<div class='cart-show-discounts'>";
98
- echo get_option('woocommerce_t4m_cart_info');
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;
111
- echo "<div class='productinfo-show-discounts'>";
112
- echo get_post_meta($thepostid, '_bulkdiscount_text_info', true);
113
- echo "</div>";
114
- }
 
 
 
 
 
 
 
115
 
116
- public function action_product_write_panel_tabs() {
117
- echo '<li class="bulkdiscount_tab bulkdiscount_options"><a href="#bulkdiscount_product_data">Bulk Discounts</a></li>';
118
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
- public function action_product_write_panels() {
121
- global $thepostid, $post;
122
- if (!$thepostid) $thepostid = $post->ID;
123
- ?>
124
- <script type="text/javascript">
125
- jQuery(document).ready( function () {
126
  <?php
127
- for($i = 1; $i <= 5; $i++) :
128
- ?>
129
- jQuery('#bulkdiscount_product_data').find('.block<?php echo $i; ?>').hide();
130
- jQuery('#bulkdiscount_product_data').find('.options_group<?php echo max($i, 2); ?>').hide();
131
- jQuery('#bulkdiscount_product_data').find('#add_discount_line<?php echo max($i, 2); ?>').hide();
132
- jQuery("#bulkdiscount_product_data").find('#add_discount_line<?php echo $i; ?>').click(function() {
133
- jQuery('#bulkdiscount_product_data').find('.block<?php echo $i; ?>').show(600);
134
- jQuery('#bulkdiscount_product_data').find('.options_group<?php echo min($i+1, 5); ?>').show(600);
135
- jQuery('#bulkdiscount_product_data').find('#add_discount_line<?php echo min($i+1, 5); ?>').show(600);
136
- jQuery('#bulkdiscount_product_data').find('#add_discount_line<?php echo $i; ?>').hide(600);
137
- });
 
 
 
 
138
  <?php
139
  endfor;
140
- for ($i = 1; $i <= 5; $i++) {
141
- if (get_post_meta($thepostid, "_bulkdiscount_quantity_$i", true)) {
142
- ?>
143
- jQuery('#bulkdiscount_product_data').find('.block<?php echo $i; ?>').show();
144
- jQuery('#bulkdiscount_product_data').find('.options_group<?php echo $i; ?>').show();
145
- jQuery("#bulkdiscount_product_data").find('#add_discount_line<?php echo $i; ?>').hide();
146
- jQuery("#bulkdiscount_product_data").find('.options_group<?php echo min($i+1,5); ?>').show();
147
- jQuery("#bulkdiscount_product_data").find('#add_discount_line<?php echo min($i+1,5); ?>').show();
148
- <?php
149
- }
150
- }
151
  ?>
152
- });
153
- </script>
154
 
155
- <div id="bulkdiscount_product_data" class="panel woocommerce_options_panel">
156
 
157
- <div class="options_group">
158
- <?php
159
- woocommerce_wp_textarea_input( array( 'id' => "_bulkdiscount_text_info", 'label' => 'Bulk discount info in product description', 'description' => 'Optionally enter bulk discount information that will be visible on the product page.', 'desc_tip' => 'yes', 'class' => 'fullWidth') );
160
- ?>
161
  </div>
162
 
163
- <?php
164
- for($i = 1; $i <= 5; $i++) :
165
- ?>
166
 
167
- <div class="options_group<?php echo $i; ?>">
168
- <a id="add_discount_line<?php echo $i; ?>" class="button-secondary" href="#block<?php echo $i; ?>">Add discount line</a>
169
- <div class="block<?php echo $i; ?>">
170
- <?php
171
- woocommerce_wp_text_input( array( 'id' => "_bulkdiscount_quantity_$i", 'label' => __('Quantity (min.)', 'woobulkdiscount'), 'description' => __('Enter up minimal quantity for which the discount applies.', 'woocommerce') ) );
172
- woocommerce_wp_text_input( array( 'id' => "_bulkdiscount_discount_$i", 'label' => __('Discount (%)', 'woobulkdiscount'), 'description' => __('Enter the discount in percents (Allowed values: 0 to 100).', 'woocommerce') ) );
173
- ?>
174
- </div>
175
- </div>
176
-
177
- <?php
178
- endfor;
179
- ?>
180
 
181
- <br />
182
-
183
- </div>
184
-
185
- <?php
186
- }
 
 
 
187
 
188
- public function action_enqueue_dependencies() {
189
- wp_register_style('woocommercebulkdiscount-style', plugins_url('style.css', __FILE__));
190
- wp_enqueue_style('woocommercebulkdiscount-style');
191
- wp_enqueue_script('jquery');
192
- }
 
 
 
 
 
 
 
 
193
 
194
- public function action_enqueue_dependencies_admin() {
195
- wp_register_style('woocommercebulkdiscount-style-admin', plugins_url('admin.css', __FILE__));
196
- wp_enqueue_style('woocommercebulkdiscount-style-admin');
197
- wp_enqueue_script('jquery');
198
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
 
200
- public function action_process_meta($post_id) {
201
- if (isset($_POST['_bulkdiscount_text_info'])) update_post_meta( $post_id, '_bulkdiscount_text_info', esc_attr($_POST['_bulkdiscount_text_info']) );
202
- for ($i = 1; $i <= 5; $i++) {
203
- if (isset($_POST["_bulkdiscount_quantity_$i"])) update_post_meta( $post_id, "_bulkdiscount_quantity_$i", stripslashes($_POST["_bulkdiscount_quantity_$i"]) );
204
- if (isset($_POST["_bulkdiscount_discount_$i"])) update_post_meta( $post_id, "_bulkdiscount_discount_$i", stripslashes($_POST["_bulkdiscount_discount_$i"]) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  }
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',
215
- 'type' => 'checkbox'
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;'
223
- )
224
- );
225
- array_push ($settings, array( 'type' => 'sectionend', 'id' => 't4m_bulk_discounts_options' ));
226
- return $settings;
227
  }
228
 
229
- }
230
 
231
- $woo_bulk_discount_plugin = new Woo_Bulk_Discount_Plugin_t4m();
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.1
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
23
 
24
  You should have received a copy of the GNU General Public License
25
  along with this program. If not, see <http://www.gnu.org/licenses/>.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ {
37
+
38
+ public function __construct()
39
+ {
40
+
41
+ $this->current_tab = (isset($_GET['tab'])) ? $_GET['tab'] : 'general';
42
+
43
+ $this->settings_tabs = array(
44
+ 'bulk_discount' => __('Bulk Discount', 'wc_bulk_discount')
45
+ );
46
+
47
+ add_action('admin_enqueue_scripts', array($this, 'action_enqueue_dependencies_admin'));
48
+ add_action('wp_head', array($this, 'action_enqueue_dependencies'));
49
+
50
+ add_action('woocommerce_settings_tabs', array($this, 'add_tab'), 10);
51
+
52
+ // Run these actions when generating the settings tabs.
53
+ foreach ($this->settings_tabs as $name => $label) {
54
+ add_action('woocommerce_settings_tabs_' . $name, array($this, 'settings_tab_action'), 10);
55
+ add_action('woocommerce_update_options_' . $name, array($this, 'save_settings'), 10);
56
  }
57
+
58
+ // Add the settings fields to each tab.
59
+ add_action('woocommerce_bulk_discount_settings', array($this, 'add_settings_fields'), 10);
60
+
61
+ if (get_option('woocommerce_t4m_enable_bulk_discounts', 'yes') == 'yes') {
62
+ add_action('woocommerce_before_calculate_totals', array($this, 'action_before_calculate'), 10, 1);
63
+ add_action('woocommerce_calculate_totals', array($this, 'action_after_calculate'), 10, 1);
64
+ add_action('woocommerce_before_cart_table', array($this, 'before_cart_table'));
65
+ add_action('woocommerce_single_product_summary', array($this, 'single_product_summary'), 45);
66
+ add_filter('woocommerce_cart_item_price_html', array($this, 'filter_item_price'), 10, 2);
67
+ add_filter('woocommerce_product_write_panel_tabs', array($this, 'action_product_write_panel_tabs'));
68
+ add_filter('woocommerce_product_write_panels', array($this, 'action_product_write_panels'));
69
+ add_action('woocommerce_process_product_meta', array($this, 'action_process_meta'));
70
+ add_filter('woocommerce_cart_product_subtotal', array($this, 'filter_cart_product_subtotal'), 10, 3);
71
+ }
72
+
73
  }
 
 
74
 
75
+ /**
76
+ * For given product, price, and quantity return the price modifying factor.
77
+ *
78
+ * @param $prodId
79
+ * @param $price
80
+ * @param $quantity
81
+ * @return float
82
+ */
83
+ protected function get_discounted_coeff($prodId, $price, $quantity)
84
+ {
85
+ $q = array(0.0);
86
+ $d = array(0.0);
87
+ /* Find the appropriate discount coefficient by looping through up to the five discount settings */
88
+ for ($i = 1; $i <= 5; $i++) {
89
+ array_push($q, get_post_meta($prodId, "_bulkdiscount_quantity_$i", true));
90
+ array_push($d, get_post_meta($prodId, "_bulkdiscount_discount_$i", true));
91
+ if ($quantity >= $q[$i] && $q[$i] > $q[0]) {
92
+ $q[0] = $q[$i];
93
+ $d[0] = $d[$i];
94
+ }
95
  }
96
+ return (100.0 - $d[0]) / 100.0; // convert the resulting discount from % to the multiplying coefficient
97
+ }
98
+
99
+ /**
100
+ * Filter product price so that the discount is visible.
101
+ *
102
+ * @param $price
103
+ * @param $values
104
+ * @return string
105
+ */
106
+ public function filter_item_price($price, $values)
107
+ {
108
+ $_product = $values['data'];
109
+ $coeff = $this->get_discounted_coeff($_product->id, $_product->get_price(), $values['quantity']);
110
+ $oldprice = ($coeff < 1.0) ? woocommerce_price($_product->get_price()) : "";
111
+ $discprice = woocommerce_price($_product->get_price() * $coeff);
112
+ return "<span class='discount_price_info'><span>$oldprice</span></span>$discprice";
113
  }
 
114
 
115
+ /**
116
+ * Hook to woocommerce_before_calculate_totals action.
117
+ *
118
+ * @param WC_Cart $cart
119
+ */
120
+ public function action_before_calculate(WC_Cart $cart)
121
+ {
122
+ if (sizeof($cart->cart_contents) > 0) {
123
+ foreach ($cart->cart_contents as $cart_item_key => $values) {
124
+ $_product = $values['data'];
125
+ $row_base_price = $_product->get_price() * $this->get_discounted_coeff($_product->id, $_product->get_price(), $values['quantity']);
126
+ $values['data']->set_price($row_base_price);
127
+ }
128
  }
129
  }
 
130
 
131
+ /**
132
+ * Hook to woocommerce_calculate_totals.
133
+ *
134
+ * @param WC_Cart $cart
135
+ */
136
+ public function action_after_calculate(WC_Cart $cart)
137
+ {
138
+ if (sizeof($cart->cart_contents) > 0) {
139
+ foreach ($cart->cart_contents as $cart_item_key => $values) {
140
+ $_product = $values['data'];
141
+ $coeff = $this->get_discounted_coeff($_product->id, $_product->get_price(), $values['quantity']);
142
+ $row_base_price = $_product->get_price() / $coeff;
143
+ $values['data']->set_price($row_base_price);
144
+ }
145
+ }
146
+ }
147
 
148
+ /**
149
+ * Show discount info in cart.
150
+ */
151
+ public function before_cart_table()
152
+ {
153
+ echo "<div class='cart-show-discounts'>";
154
+ echo get_option('woocommerce_t4m_cart_info');
155
+ echo "</div>";
156
+ }
157
 
158
+ /**
159
+ * Hook to woocommerce_cart_product_subtotal filter.
160
+ *
161
+ * @param $subtotal
162
+ * @param $_product
163
+ * @param $quantity
164
+ * @return string
165
+ */
166
+ public function filter_cart_product_subtotal($subtotal, $_product, $quantity)
167
+ {
168
+ $coeff = $this->get_discounted_coeff($_product->id, $_product->get_price(), $quantity);
169
+ $newsubtotal = woocommerce_price($_product->get_price() * $quantity * $coeff);
170
+ return $newsubtotal;
171
+ }
172
 
173
+ /**
174
+ * Display discount information in Product Detail.
175
+ */
176
+ public function single_product_summary()
177
+ {
178
+ global $thepostid, $post;
179
+ if (!$thepostid) $thepostid = $post->ID;
180
+ echo "<div class='productinfo-show-discounts'>";
181
+ echo get_post_meta($thepostid, '_bulkdiscount_text_info', true);
182
+ echo "</div>";
183
+ }
184
+
185
+ /**
186
+ * Add entry to Product Settings.
187
+ */
188
+ public function action_product_write_panel_tabs()
189
+ {
190
+ echo '<li class="bulkdiscount_tab bulkdiscount_options"><a href="#bulkdiscount_product_data">Bulk Discount</a></li>';
191
+ }
192
+
193
+ /**
194
+ * Add entry content to Product Settings.
195
+ */
196
+ public function action_product_write_panels()
197
+ {
198
+ global $thepostid, $post;
199
+ if (!$thepostid) $thepostid = $post->ID;
200
+ ?>
201
+ <script type="text/javascript">
202
+ jQuery(document).ready(function () {
203
+ <?php
204
+ for($i = 1; $i <= 5; $i++) :
205
+ ?>
206
+ jQuery('#bulkdiscount_product_data').find('.block<?php echo $i; ?>').hide();
207
+ jQuery('#bulkdiscount_product_data').find('.options_group<?php echo max($i, 2); ?>').hide();
208
+ jQuery('#bulkdiscount_product_data').find('#add_discount_line<?php echo max($i, 2); ?>').hide();
209
+ jQuery("#bulkdiscount_product_data").find('#add_discount_line<?php echo $i; ?>').click(function () {
210
+ jQuery('#bulkdiscount_product_data').find('.block<?php echo $i; ?>').show(400);
211
+ jQuery('#bulkdiscount_product_data').find('.options_group<?php echo min($i+1, 5); ?>').show(400);
212
+ jQuery('#bulkdiscount_product_data').find('#add_discount_line<?php echo min($i+1, 5); ?>').show(400);
213
+ jQuery('#bulkdiscount_product_data').find('#add_discount_line<?php echo $i; ?>').hide(400);
214
+ });
215
+ <?php
216
+ endfor;
217
+ for ($i = 1; $i <= 5; $i++) {
218
+ if (get_post_meta($thepostid, "_bulkdiscount_quantity_$i", true)) {
219
+ ?>
220
+ jQuery('#bulkdiscount_product_data').find('.block<?php echo $i; ?>').show();
221
+ jQuery('#bulkdiscount_product_data').find('.options_group<?php echo $i; ?>').show();
222
+ jQuery("#bulkdiscount_product_data").find('#add_discount_line<?php echo $i; ?>').hide();
223
+ jQuery("#bulkdiscount_product_data").find('.options_group<?php echo min($i+1,5); ?>').show();
224
+ jQuery("#bulkdiscount_product_data").find('#add_discount_line<?php echo min($i+1,5); ?>').show();
225
+ <?php
226
+ }
227
+ }
228
+ ?>
229
+ });
230
+ </script>
231
+
232
+ <div id="bulkdiscount_product_data" class="panel woocommerce_options_panel">
233
+
234
+ <div class="options_group">
235
+ <?php
236
+ woocommerce_wp_textarea_input(array('id' => "_bulkdiscount_text_info", 'label' => 'Bulk discount info in product description', 'description' => 'Optionally enter bulk discount information that will be visible on the product page.', 'desc_tip' => 'yes', 'class' => 'fullWidth'));
237
+ ?>
238
+ </div>
239
 
 
 
 
 
 
 
240
  <?php
241
+ for ($i = 1; $i <= 5; $i++) :
242
+ ?>
243
+
244
+ <div class="options_group<?php echo $i; ?>">
245
+ <a id="add_discount_line<?php echo $i; ?>" class="button-secondary"
246
+ href="#block<?php echo $i; ?>">Add discount line</a>
247
+
248
+ <div class="block<?php echo $i; ?>">
249
+ <?php
250
+ woocommerce_wp_text_input(array('id' => "_bulkdiscount_quantity_$i", 'label' => __('Quantity (min.)', 'woobulkdiscount'), 'description' => __('Enter up minimal quantity for which the discount applies.', 'woocommerce')));
251
+ woocommerce_wp_text_input(array('id' => "_bulkdiscount_discount_$i", 'label' => __('Discount (%)', 'woobulkdiscount'), 'description' => __('Enter the discount in percents (Allowed values: 0 to 100).', 'woocommerce')));
252
+ ?>
253
+ </div>
254
+ </div>
255
+
256
  <?php
257
  endfor;
 
 
 
 
 
 
 
 
 
 
 
258
  ?>
 
 
259
 
260
+ <br/>
261
 
 
 
 
 
262
  </div>
263
 
264
+ <?php
265
+ }
 
266
 
267
+ /**
268
+ * Enqueue frontend dependencies.
269
+ */
270
+ public function action_enqueue_dependencies()
271
+ {
272
+ wp_register_style('woocommercebulkdiscount-style', plugins_url('style.css', __FILE__));
273
+ wp_enqueue_style('woocommercebulkdiscount-style');
274
+ wp_enqueue_script('jquery');
275
+ }
 
 
 
 
276
 
277
+ /**
278
+ * Enqueue backend dependencies.
279
+ */
280
+ public function action_enqueue_dependencies_admin()
281
+ {
282
+ wp_register_style('woocommercebulkdiscount-style-admin', plugins_url('admin.css', __FILE__));
283
+ wp_enqueue_style('woocommercebulkdiscount-style-admin');
284
+ wp_enqueue_script('jquery');
285
+ }
286
 
287
+ /**
288
+ * Updating post meta.
289
+ *
290
+ * @param $post_id
291
+ */
292
+ public function action_process_meta($post_id)
293
+ {
294
+ if (isset($_POST['_bulkdiscount_text_info'])) update_post_meta($post_id, '_bulkdiscount_text_info', esc_attr($_POST['_bulkdiscount_text_info']));
295
+ for ($i = 1; $i <= 5; $i++) {
296
+ if (isset($_POST["_bulkdiscount_quantity_$i"])) update_post_meta($post_id, "_bulkdiscount_quantity_$i", stripslashes($_POST["_bulkdiscount_quantity_$i"]));
297
+ if (isset($_POST["_bulkdiscount_discount_$i"])) update_post_meta($post_id, "_bulkdiscount_discount_$i", stripslashes($_POST["_bulkdiscount_discount_$i"]));
298
+ }
299
+ }
300
 
301
+ /**
302
+ * @access public
303
+ * @return void
304
+ */
305
+ public function add_tab()
306
+ {
307
+ foreach ($this->settings_tabs as $name => $label) {
308
+ $class = 'nav-tab';
309
+ if ($this->current_tab == $name)
310
+ $class .= ' nav-tab-active';
311
+ echo '<a href="' . admin_url('admin.php?page=woocommerce&tab=' . $name) . '" class="' . $class . '">' . $label . '</a>';
312
+ }
313
+ }
314
+
315
+ /**
316
+ * @access public
317
+ * @return void
318
+ */
319
+ public function settings_tab_action()
320
+ {
321
+ global $woocommerce_settings;
322
+
323
+ // Determine the current tab in effect.
324
+ $current_tab = $this->get_tab_in_view(current_filter(), 'woocommerce_settings_tabs_');
325
 
326
+ // Hook onto this from another function to keep things clean.
327
+ do_action('woocommerce_bulk_discount_settings');
328
+
329
+ // Display settings for this tab (make sure to add the settings to the tab).
330
+ woocommerce_admin_fields($woocommerce_settings[$current_tab]);
331
+ }
332
+
333
+ /**
334
+ * Save settings in a single field in the database for each tab's fields (one field per tab).
335
+ */
336
+ public function save_settings()
337
+ {
338
+ global $woocommerce_settings;
339
+
340
+ // Make sure our settings fields are recognised.
341
+ $this->add_settings_fields();
342
+
343
+ $current_tab = $this->get_tab_in_view(current_filter(), 'woocommerce_update_options_');
344
+ woocommerce_update_options($woocommerce_settings[$current_tab]);
345
+ }
346
+
347
+ /**
348
+ * Get the tab current in view/processing.
349
+ */
350
+ public function get_tab_in_view($current_filter, $filter_base)
351
+ {
352
+ return str_replace($filter_base, '', $current_filter);
353
+ }
354
+
355
+
356
+ /**
357
+ * Add settings fields for each tab.
358
+ */
359
+ public function add_settings_fields()
360
+ {
361
+ global $woocommerce_settings;
362
+
363
+ // Load the prepared form fields.
364
+ $this->init_form_fields();
365
+
366
+ if (is_array($this->fields))
367
+ foreach ($this->fields as $k => $v)
368
+ $woocommerce_settings[$k] = $v;
369
+ }
370
+
371
+ /**
372
+ * Prepare form fields to be used in the various tabs.
373
+ */
374
+ public function init_form_fields()
375
+ {
376
+ global $woocommerce;
377
+
378
+ // Define settings
379
+ $this->fields['bulk_discount'] = apply_filters('woocommerce_bulk_discount_settings_fields', array(
380
+
381
+ array('name' => __('Bulk Discount', 'wc_bulk_discount'), 'type' => 'title', 'desc' => 'The following options are specific to product bulk discount.', 'id' => 't4m_bulk_discounts_options'),
382
+
383
+ array(
384
+ 'name' => __('Bulk Discount enabled', 'wc_bulk_discount'),
385
+ 'id' => 'woocommerce_t4m_enable_bulk_discounts',
386
+ 'desc' => __('', 'wc_bulk_discount'),
387
+ 'std' => 'yes',
388
+ 'type' => 'checkbox',
389
+ 'default' => 'yes'
390
+ ),
391
+
392
+ array(
393
+ 'name' => __('Optionally enter information about discounts visible on cart page', 'wc_bulk_discount'),
394
+ 'id' => 'woocommerce_t4m_cart_info',
395
+ 'type' => 'textarea',
396
+ 'css' => 'width:100%; height: 75px;'
397
+ ),
398
+
399
+ array('type' => 'sectionend', 'id' => 't4m_bulk_discounts_options')
400
+
401
+ )); // End settings
402
+
403
+ $woocommerce->add_inline_js("
404
+ jQuery('#woocommerce_t4m_enable_bulk_discounts').change(function(){
405
+
406
+ jQuery('#woocommerce_t4m_cart_info').closest('tr').hide();
407
+
408
+ if ( jQuery(this).attr('checked') ) {
409
+ jQuery('#woocommerce_t4m_cart_info').closest('tr').show();
410
+ }
411
+
412
+ }).change();
413
+ ");
414
  }
 
415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
416
  }
417
 
418
+ $woo_bulk_discount_plugin = new Woo_Bulk_Discount_Plugin_t4m();
419
 
420
+ }