forked from mikejolley/woocommerce-product-gift-wrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-product-gift-wrap.php
312 lines (259 loc) · 9.17 KB
/
woocommerce-product-gift-wrap.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
<?php
/*
Plugin Name: WooCommerce Product Gift Wrap
Plugin URI: https://github.com/mikejolley/woocommerce-product-gift-wrap
Description: Add an option to your products to enable gift wrapping. Optionally charge a fee.
Version: 1.1.0
Author: Mike Jolley
Author URI: http://mikejolley.com
Requires at least: 3.5
Tested up to: 4.0
Text Domain: woocommerce-product-gift-wrap
Domain Path: /languages/
Copyright: © 2014 Mike Jolley.
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* Localisation
*/
load_plugin_textdomain( 'woocommerce-product-gift-wrap', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
/**
* WC_Product_Gift_wrap class.
*/
class WC_Product_Gift_Wrap {
/**
* Hook us in :)
*
* @access public
* @return void
*/
public function __construct() {
$default_message = '{checkbox} '. sprintf( __( 'Gift wrap this item for %s?', 'woocommerce-product-gift-wrap' ), '{price}' );
$this->gift_wrap_enabled = get_option( 'product_gift_wrap_enabled' ) == 'yes' ? true : false;
$this->gift_wrap_cost = get_option( 'product_gift_wrap_cost', 0 );
$this->product_gift_wrap_message = get_option( 'product_gift_wrap_message' );
if ( ! $this->product_gift_wrap_message ) {
$this->product_gift_wrap_message = $default_message;
}
add_option( 'product_gift_wrap_enabled', 'no' );
add_option( 'product_gift_wrap_cost', '0' );
add_option( 'product_gift_wrap_message', $default_message );
// Init settings
$this->settings = array(
array(
'name' => __( 'Gift Wrapping Enabled by Default?', 'woocommerce-product-gift-wrap' ),
'desc' => __( 'Enable this to allow gift wrapping for products by default.', 'woocommerce-product-gift-wrap' ),
'id' => 'product_gift_wrap_enabled',
'type' => 'checkbox',
),
array(
'name' => __( 'Default Gift Wrap Cost', 'woocommerce-product-gift-wrap' ),
'desc' => __( 'The cost of gift wrap unless overridden per-product.', 'woocommerce-product-gift-wrap' ),
'id' => 'product_gift_wrap_cost',
'type' => 'text',
'desc_tip' => true
),
array(
'name' => __( 'Gift Wrap Message', 'woocommerce-product-gift-wrap' ),
'id' => 'product_gift_wrap_message',
'desc' => __( 'Note: <code>{checkbox}</code> will be replaced with a checkbox and <code>{price}</code> will be replaced with the gift wrap cost.', 'woocommerce-product-gift-wrap' ),
'type' => 'text',
'desc_tip' => __( 'The checkbox and label shown to the user on the frontend.', 'woocommerce-product-gift-wrap' )
),
);
// Display on the front end
add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'gift_option_html' ), 10 );
// Filters for cart actions
add_filter( 'woocommerce_add_cart_item_data', array( $this, 'add_cart_item_data' ), 10, 2 );
add_filter( 'woocommerce_get_cart_item_from_session', array( $this, 'get_cart_item_from_session' ), 10, 2 );
add_filter( 'woocommerce_get_item_data', array( $this, 'get_item_data' ), 10, 2 );
add_filter( 'woocommerce_add_cart_item', array( $this, 'add_cart_item' ), 10, 1 );
add_action( 'woocommerce_add_order_item_meta', array( $this, 'add_order_item_meta' ), 10, 2 );
// Write Panels
add_action( 'woocommerce_product_options_pricing', array( $this, 'write_panel' ) );
add_action( 'woocommerce_process_product_meta', array( $this, 'write_panel_save' ) );
// Admin
add_action( 'woocommerce_settings_general_options_end', array( $this, 'admin_settings' ) );
add_action( 'woocommerce_update_options_general', array( $this, 'save_admin_settings' ) );
}
/**
* Show the Gift Checkbox on the frontend
*
* @access public
* @return void
*/
public function gift_option_html() {
global $post;
$is_wrappable = get_post_meta( $post->ID, '_is_gift_wrappable', true );
if ( $is_wrappable == '' && $this->gift_wrap_enabled ) {
$is_wrappable = 'yes';
}
if ( $is_wrappable == 'yes' ) {
$current_value = ! empty( $_REQUEST['gift_wrap'] ) ? 1 : 0;
$cost = get_post_meta( $post->ID, '_gift_wrap_cost', true );
if ( $cost == '' ) {
$cost = $this->gift_wrap_cost;
}
$price_text = $cost > 0 ? woocommerce_price( $cost ) : __( 'free', 'woocommerce-product-gift-wrap' );
$checkbox = '<input type="checkbox" name="gift_wrap" value="yes" ' . checked( $current_value, 1, false ) . ' />';
woocommerce_get_template( 'gift-wrap.php', array(
'product_gift_wrap_message' => $this->product_gift_wrap_message,
'checkbox' => $checkbox,
'price_text' => $price_text
), 'woocommerce-product-gift-wrap', untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/' );
}
}
/**
* When added to cart, save any gift data
*
* @access public
* @param mixed $cart_item_meta
* @param mixed $product_id
* @return void
*/
public function add_cart_item_data( $cart_item_meta, $product_id ) {
$is_wrappable = get_post_meta( $product_id, '_is_gift_wrappable', true );
if ( $is_wrappable == '' && $this->gift_wrap_enabled ) {
$is_wrappable = 'yes';
}
if ( ! empty( $_POST['gift_wrap'] ) && $is_wrappable == 'yes' ) {
$cart_item_meta['gift_wrap'] = true;
}
return $cart_item_meta;
}
/**
* Get the gift data from the session on page load
*
* @access public
* @param mixed $cart_item
* @param mixed $values
* @return void
*/
public function get_cart_item_from_session( $cart_item, $values ) {
if ( ! empty( $values['gift_wrap'] ) ) {
$cart_item['gift_wrap'] = true;
$cost = get_post_meta( $cart_item['data']->id, '_gift_wrap_cost', true );
if ( $cost == '' ) {
$cost = $this->gift_wrap_cost;
}
$cart_item['data']->adjust_price( $cost );
}
return $cart_item;
}
/**
* Display gift data if present in the cart
*
* @access public
* @param mixed $other_data
* @param mixed $cart_item
* @return void
*/
public function get_item_data( $item_data, $cart_item ) {
if ( ! empty( $cart_item['gift_wrap'] ) )
$item_data[] = array(
'name' => __( 'Gift Wrapped', 'woocommerce-product-gift-wrap' ),
'value' => __( 'Yes', 'woocommerce-product-gift-wrap' ),
'display' => __( 'Yes', 'woocommerce-product-gift-wrap' )
);
return $item_data;
}
/**
* Adjust price after adding to cart
*
* @access public
* @param mixed $cart_item
* @return void
*/
public function add_cart_item( $cart_item ) {
if ( ! empty( $cart_item['gift_wrap'] ) ) {
$cost = get_post_meta( $cart_item['data']->id, '_gift_wrap_cost', true );
if ( $cost == '' ) {
$cost = $this->gift_wrap_cost;
}
$cart_item['data']->adjust_price( $cost );
}
return $cart_item;
}
/**
* After ordering, add the data to the order line items.
*
* @access public
* @param mixed $item_id
* @param mixed $values
* @return void
*/
public function add_order_item_meta( $item_id, $cart_item ) {
if ( ! empty( $cart_item['gift_wrap'] ) ) {
woocommerce_add_order_item_meta( $item_id, __( 'Gift Wrapped', 'woocommerce-product-gift-wrap' ), __( 'Yes', 'woocommerce-product-gift-wrap' ) );
}
}
/**
* write_panel function.
*
* @access public
* @return void
*/
public function write_panel() {
global $post;
echo '</div><div class="options_group show_if_simple show_if_variable">';
$is_wrappable = get_post_meta( $post->ID, '_is_gift_wrappable', true );
if ( $is_wrappable == '' && $this->gift_wrap_enabled ) {
$is_wrappable = 'yes';
}
woocommerce_wp_checkbox( array(
'id' => '_is_gift_wrappable',
'wrapper_class' => '',
'value' => $is_wrappable,
'label' => __( 'Gift Wrappable', 'woocommerce-product-gift-wrap' ),
'description' => __( 'Enable this option if the customer can choose gift wrapping.', 'woocommerce-product-gift-wrap' ),
) );
woocommerce_wp_text_input( array(
'id' => '_gift_wrap_cost',
'label' => __( 'Gift Wrap Cost', 'woocommerce-product-gift-wrap' ),
'placeholder' => $this->gift_wrap_cost,
'desc_tip' => true,
'description' => __( 'Override the default cost by inputting a cost here.', 'woocommerce-product-gift-wrap' ),
) );
wc_enqueue_js( "
jQuery('input#_is_gift_wrappable').change(function(){
jQuery('._gift_wrap_cost_field').hide();
if ( jQuery('#_is_gift_wrappable').is(':checked') ) {
jQuery('._gift_wrap_cost_field').show();
}
}).change();
" );
}
/**
* write_panel_save function.
*
* @access public
* @param mixed $post_id
* @return void
*/
public function write_panel_save( $post_id ) {
$_is_gift_wrappable = ! empty( $_POST['_is_gift_wrappable'] ) ? 'yes' : 'no';
$_gift_wrap_cost = ! empty( $_POST['_gift_wrap_cost'] ) ? woocommerce_clean( $_POST['_gift_wrap_cost'] ) : '';
update_post_meta( $post_id, '_is_gift_wrappable', $_is_gift_wrappable );
update_post_meta( $post_id, '_gift_wrap_cost', $_gift_wrap_cost );
}
/**
* admin_settings function.
*
* @access public
* @return void
*/
public function admin_settings() {
woocommerce_admin_fields( $this->settings );
}
/**
* save_admin_settings function.
*
* @access public
* @return void
*/
public function save_admin_settings() {
woocommerce_update_options( $this->settings );
}
}
new WC_Product_Gift_Wrap();