-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwoocommerce-subscriptions-custom-pricestring.php
executable file
·338 lines (292 loc) · 11.9 KB
/
woocommerce-subscriptions-custom-pricestring.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/**
* Plugin Name: WooCommerce Subscriptions - Custom Price String
* Plugin URI: https://github.com/woocommerce/woocommerce-subscriptions-custom-price-string.git
* Description: A small add-on plugin to customize the product price strings
* Author: WooCommerce
* Author URI: https://woo.com
* License: GPLv3
* Version: 1.0.6
* WC requires at least: 3.0.0
*
* GitHub Plugin URI: woocommerce/woocommerce-subscriptions-custom-price-string
* GitHub Branch: master
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package WooCommerce Subscriptions - Custom Price String
* @author WooCommerce
* @since 1.0
*/
/*
TODO:
- Should the custom price string for simple/variable products (not subscriptions) be displayed on the cart page too?
- Check the scope of the custom strings (product page, cart, checkout, emails, backend, orders, renewals, etc)
*/
// add_filter("woocommerce_subscription_price_string", "wcs_custom_price_strings_cart", 10, 2);
// Declare HPOS compat per https://github.com/woocommerce/woocommerce/wiki/High-Performance-Order-Storage-Upgrade-Recipe-Book#declaring-extension-incompatibility
add_action( 'before_woocommerce_init', function() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
} );
require_once 'includes/class-pp-dependencies.php';
if ( false === PP_Dependencies::is_woocommerce_active( '3.0' ) ) {
PP_Dependencies::enqueue_admin_notice( 'WooCommerce Subscriptions - Custom price string', 'WooCommerce', '3.0' );
return;
}
if ( false === PP_Dependencies::is_subscriptions_active( '2.1' ) ) {
PP_Dependencies::enqueue_admin_notice( 'WooCommerce Subscriptions - Custom price string', 'WooCommerce Subscriptions', '2.1' );
return;
}
// Uses 'woocommerce_variable_product_before_variations' hook if WC>3.34 to add the "custom From string field" to the Variations tab (https://github.com/woocommerce/woocommerce/pull/19557#pullrequestreview-107731914).
$variations_hook = 'woocommerce_product_options_advanced';
if ( true === PP_Dependencies::is_woocommerce_active( '3.3.6' ) ) {
$variations_hook = 'woocommerce_variable_product_before_variations';
}
if ( class_exists( 'WC_Bookings' ) ) {
// WooCommerce Bookings is enabled -> Add the custom from string field in the "Advanced" tab.
add_action( 'woocommerce_product_options_advanced', 'wcs_cps_from_field' );
}
/**
* Adds the 'Custom Price String' field to the product editor page.
*
* @access public
* @return void
*/
function wcs_cps_admin_field() {
global $post;
$product = wc_get_product( $post->ID );
$p_type = $product->get_type();
if ( 'subscription' === $p_type || 'variable-subscription' === $p_type ) {
$price = WC_Subscriptions_Product::get_price( $product );
$custom_price_string = WC_Subscriptions_Product::get_price_string( $post->ID, array( 'price' => $price ) );
$original_price_string = WC_Subscriptions_Product::get_price_string(
$post->ID,
array(
'price' => $price,
'custom' => false,
)
);
} else {
$original_price_string = $product->get_price();
$custom_price_string = get_post_meta( $post->ID, '_custom_price_string', true );
}
if ( $original_price_string === $custom_price_string ) {
$custom_price_string = '';
}
woocommerce_wp_text_input(
array(
'id' => '_custom_price_string',
'class' => 'wc_input_custom_price_string short',
'label' => __( 'Custom price string', 'woocommerce-subscriptions' ),
'placeholder' => apply_filters( 'wcs_custom_price_string_value', $original_price_string ),
'description' => __( 'Customize the default price string that is generated for this specific product. Leave it empty to use the default string.', 'woocommerce-subscriptions' ),
'desc_tip' => true,
'type' => 'text',
'value' => apply_filters( 'wcs_custom_price_string_value', $custom_price_string ),
'data_type' => 'any',
)
);
}
add_action( 'woocommerce_product_options_pricing', 'wcs_cps_admin_field' );
/**
* Adds the 'Custom Price String' field to each variation
*
* @access public
* @param mixed $loop
* @param mixed $variation_data
* @param mixed $variation
* @return void
*/
function wcs_cps_variation_admin_field( $loop, $variation_data, $variation ) {
$wc_variation = wc_get_product( $variation->ID );
$price = $wc_variation->get_price();
if ( 'variable-subscription' === $wc_variation->get_type() ) {
$custom_price_string = WC_Subscriptions_Product::get_price_string( ( $variation->ID ), array( 'price' => $price ) );
$original_price_string = WC_Subscriptions_Product::get_price_string(
( $variation->ID ),
array(
'price' => $price,
'custom' => false,
)
);
} else {
$original_price_string = $wc_variation->get_price();
$custom_price_string = get_post_meta( $variation->ID, '_custom_price_string', true );
}
if ( $original_price_string === $custom_price_string ) {
$custom_price_string = '';
}
woocommerce_wp_text_input(
array(
'id' => '_custom_price_string[' . $loop . ']',
'class' => 'wc_input_custom_price_string short',
'label' => __( 'Custom price string', 'woocommerce-subscriptions' ),
'placeholder' => apply_filters( 'wcs_custom_price_string_value', $original_price_string ),
'description' => __( 'Customize the default price string that is generated for this specific variation. Leave it empty to use the default string.', 'woocommerce-subscriptions' ),
'desc_tip' => true,
'type' => 'text',
'value' => apply_filters( 'wcs_custom_price_string_value', $custom_price_string ),
'data_type' => 'any',
)
);
}
add_action( 'woocommerce_variable_subscription_pricing', 'wcs_cps_variation_admin_field', 11, 3 );
/**
* Adds the 'Custom From String' field to a variable product
*
* @access public
* @return void
*/
function wcs_cps_from_field() {
global $post;
$product = wc_get_product( $post->ID );
$p_type = $product->get_type();
$remnoved = remove_filter( 'woocommerce_get_price_html', 'wcs_cps_from_string', 10 );
$original_from_string = $product->get_price_html();
add_filter( 'woocommerce_get_price_html', 'wcs_cps_from_string', 10, 2 );
$custom_from_string = get_post_meta( $post->ID, '_custom_from_string', true );
if ( $original_from_string === $custom_from_string ) {
$custom_from_string = '';
}
echo "<div class='toolbar toolbar-variations-from-string'>";
woocommerce_wp_text_input(
array(
'id' => '_custom_from_string',
'class' => 'wc_input_custom_from_string short',
'label' => __( 'Custom from string', 'woocommerce-subscriptions' ),
'placeholder' => apply_filters( 'wcs_custom_price_string_value', $original_from_string ),
'description' => __( 'Customize the default From string that is generated for this specific product. Leave it empty to use the default string.', 'woocommerce-subscriptions' ),
'desc_tip' => true,
'type' => 'text',
'value' => apply_filters( 'wcs_custom_price_string_value', $custom_from_string ),
'data_type' => 'any',
)
);
echo '</div>';
}
add_action( $variations_hook, 'wcs_cps_from_field' );
/**
* Strip the HTML tags of the custom price string fields (added as a filter to allow customers to remove it if needed)
*
* @access public
* @param string $price_string_value
* @return string $price_string_value
*/
function strip_tags_of_custom_price_value( $price_string_value ) {
return wp_strip_all_tags( $price_string_value );
}
add_filter( 'wcs_custom_price_string_value', 'strip_tags_of_custom_price_value', 10, 1 );
/**
* Save the custom price string in the current product's '_custom_price_string' meta
*
* @access public
* @param mixed $post_id
* @return void
*/
function save_custom_price_string( $post_id ) {
if ( isset( $_REQUEST['_custom_price_string'] ) ) {
$custom_price_string = $_REQUEST['_custom_price_string'];
// If it is coming from a variable subscription product, get only the first element (the one that belongs to the parent product) as we're already saving the variation ones in a different function.
if ( is_array( $custom_price_string ) ) {
$custom_price_string = $custom_price_string[0];
}
$custom_price_string = wp_unslash( $custom_price_string );
update_post_meta( $post_id, '_custom_price_string', $custom_price_string );
}
if ( isset( $_REQUEST['_custom_from_string'] ) ) {
$custom_from_string = wp_unslash( $_REQUEST['_custom_from_string'] );
update_post_meta( $post_id, '_custom_from_string', $custom_from_string );
}
}
add_action( 'save_post', 'save_custom_price_string', 11 );
/**
* Save the custom price string in the current variation's '_custom_price_string' meta
*
* @access public
* @param mixed $variation_id
* @param mixed $index
* @return void
*/
function save_variation_custom_price_string( $variation_id, $index ) {
if ( isset( $_POST['_custom_price_string'][ $index ] ) ) {
update_post_meta( $variation_id, '_custom_price_string', stripslashes( $_POST['_custom_price_string'][ $index ] ) );
}
}
add_action( 'woocommerce_save_product_variation', 'save_variation_custom_price_string', 20, 2 );
/**
* Hooks 'woocommerce_subscriptions_product_price_string' filter to return the custom price string if the product has the '_custom_price_string' meta
*
* @access public
* @param mixed $subscription_string
* @param mixed $product
* @param mixed $include
* @return void
*/
function wcs_custom_price_strings( $subscription_string, $product, $include ) {
$product_id = $product->get_id();
if ( ! isset( $include['custom'] ) ) {
$include['custom'] = true;
}
$custom_price_string = get_post_meta( $product_id, '_custom_price_string', true );
if ( false !== $custom_price_string && '' !== $custom_price_string && false !== $include['custom'] ) {
$subscription_string = $custom_price_string;
}
return $subscription_string;
}
add_filter( 'woocommerce_subscriptions_product_price_string', 'wcs_custom_price_strings', 10, 3 );
/**
* Hooks 'woocommerce_get_price_html' filter to return the custom price string if the product has the '_custom_price_string' meta
*
* @access public
* @param mixed $price
* @param mixed $product
* @return void
*/
function wcs_custom_price_strings_simple_prod( $price, $product ) {
$product_id = $product->get_id();
if ( ! isset( $include['custom'] ) ) {
$include['custom'] = true;
}
$custom_price_string = get_post_meta( $product_id, '_custom_price_string', true );
if ( false !== $custom_price_string && '' !== $custom_price_string && false !== $include['custom'] ) {
$price = $custom_price_string;
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'wcs_custom_price_strings_simple_prod', 10, 2 );
/**
* Hooks 'woocommerce_get_price_html' filter to return the "custom from string" when the (variable) product has the "_custom_from_string" meta
*
* @access public
* @param mixed $price
* @param mixed $product
* @return void
*/
function wcs_cps_from_string( $price, $product ) {
$target_product_types = array(
'variable',
'variable-subscription',
'booking',
);
if ( in_array( $product->get_type(), $target_product_types, true ) ) {
$custom_from_string = get_post_meta( $product->get_id(), '_custom_from_string', true );
if ( '' !== $custom_from_string ) {
$price = $custom_from_string;
}
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'wcs_cps_from_string', 10, 2 );