-
Notifications
You must be signed in to change notification settings - Fork 0
/
sales-funnel-woocommerce.php
367 lines (310 loc) · 12.7 KB
/
sales-funnel-woocommerce.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
/**
* Plugin Name: Sales Funnel WooCommerce
* Plugin URI: https://github.com/Finland93/sales-funnel-woocommerce
* Description: Add buy now button, edit add to cart text, add small order fees, and more WooCommerce customizations.
* Version: 1.0
* Author: Finland93
* Author URI: https://github.com/Finland93
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
// Exit if directly accessed
if (!defined('ABSPATH')) {
exit;
}
// Add menu page
function sales_funnel_woocommerce_menu_page() {
add_menu_page(
'Sales Funnel',
'Sales Funnel',
'manage_options',
'sales-funnel-woocommerce',
'sales_funnel_woocommerce_page_callback',
'dashicons-chart-area',
98
);
}
add_action('admin_menu', 'sales_funnel_woocommerce_menu_page');
// Menu page callback function
function sales_funnel_woocommerce_page_callback() {
// Check if the form was submitted and the settings were saved
if (isset($_GET['settings-updated']) && $_GET['settings-updated'] === 'true') {
// Display the notice
sales_funnel_woocommerce_settings_saved_notice();
}
echo '<div class="wrap">';
echo '<h1>Sales Funnel</h1>';
echo '<p>Add buy now button, edit add to cart text, add small order fees, and more WooCommerce customizations.</p>';
// Recommended plugins section
echo '<div style="display: flex;">';
echo '<div style="flex: 1;">';
// Display the options form
echo '<form method="post" action="options.php">';
settings_fields('sales_funnel_woocommerce_options');
do_settings_sections('sales-funnel-woocommerce');
submit_button();
echo '</form>';
echo '</div>';
// Recommended plugins section
echo '<div style="flex: 1; padding-left: 20px;">';
echo '<h2>Recommended Plugins</h2>';
echo '<div>';
echo '<strong>Yith - Quick View:</strong> <a href="https://wordpress.org/plugins/yith-woocommerce-quick-view/" target="_blank">Download</a>';
echo '</div>';
echo '<div>';
echo '<strong>Side Cart Woocommerce:</strong> <a href="https://wordpress.org/plugins/side-cart-woocommerce/" target="_blank">Download</a>';
echo '</div>';
echo '<div>';
echo '<strong>WooCommerce Cart Abandonment Recovery:</strong> <a href="https://wordpress.org/plugins/woo-cart-abandonment-recovery/" target="_blank">Download</a>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
// Register plugin options
function sales_funnel_woocommerce_register_options() {
add_settings_section(
'sales_funnel_woocommerce_settings_section',
'Small Order Fee Settings',
'',
'sales-funnel-woocommerce'
);
add_settings_field(
'enable_small_fee',
'Enable Small Order Fee',
'sales_funnel_woocommerce_field_callback',
'sales-funnel-woocommerce',
'sales_funnel_woocommerce_settings_section',
[
'type' => 'checkbox',
'name' => 'enable_small_fee',
]
);
add_settings_field(
'small_fee_name',
'Small Fee Name',
'sales_funnel_woocommerce_field_callback',
'sales-funnel-woocommerce',
'sales_funnel_woocommerce_settings_section',
[
'type' => 'text',
'name' => 'small_fee_name',
]
);
add_settings_field(
'small_fee_amount',
'Small Fee Amount',
'sales_funnel_woocommerce_field_callback',
'sales-funnel-woocommerce',
'sales_funnel_woocommerce_settings_section',
[
'type' => 'text',
'name' => 'small_fee_amount',
]
);
add_settings_field(
'small_fee_threshold',
'Small Fee for Sum Under',
'sales_funnel_woocommerce_field_callback',
'sales-funnel-woocommerce',
'sales_funnel_woocommerce_settings_section',
[
'type' => 'text',
'name' => 'small_fee_threshold',
]
);
add_settings_section(
'sales_funnel_woocommerce_button_text_section',
'Button Text Settings',
'',
'sales-funnel-woocommerce'
);
add_settings_field(
'add_to_cart_text',
'Add to Cart Button Text',
'sales_funnel_woocommerce_field_callback',
'sales-funnel-woocommerce',
'sales_funnel_woocommerce_button_text_section',
[
'type' => 'text',
'name' => 'add_to_cart_text',
]
);
add_settings_field(
'enable_buy_now',
'Enable Buy Now',
'sales_funnel_woocommerce_field_callback',
'sales-funnel-woocommerce',
'sales_funnel_woocommerce_button_text_section',
[
'type' => 'checkbox',
'name' => 'enable_buy_now',
]
);
add_settings_field(
'buy_now_text',
'Buy Now Button Text',
'sales_funnel_woocommerce_field_callback',
'sales-funnel-woocommerce',
'sales_funnel_woocommerce_button_text_section',
[
'type' => 'text',
'name' => 'buy_now_text',
]
);
add_settings_field(
'buy_now_margin_bottom',
'Buy Now Button Margin Bottom',
'sales_funnel_woocommerce_field_callback',
'sales-funnel-woocommerce',
'sales_funnel_woocommerce_button_text_section',
[
'type' => 'number',
'name' => 'buy_now_margin_bottom',
]
);
add_settings_section(
'sales_funnel_woocommerce_checkout_text_section',
'Edit Checkout Options',
'',
'sales-funnel-woocommerce'
);
add_settings_field(
'add_cart_to_checkout_page',
'Add Cart to Checkout Page',
'sales_funnel_woocommerce_field_callback',
'sales-funnel-woocommerce',
'sales_funnel_woocommerce_checkout_text_section',
[
'type' => 'checkbox',
'name' => 'add_cart_to_checkout_page',
]
);
add_settings_field(
'cart_redirection_to_checkout',
'Cart Redirection to Checkout',
'sales_funnel_woocommerce_field_callback',
'sales-funnel-woocommerce',
'sales_funnel_woocommerce_checkout_text_section',
[
'type' => 'checkbox',
'name' => 'cart_redirection_to_checkout',
]
);
register_setting('sales_funnel_woocommerce_options', 'sales_funnel_woocommerce_options');
}
add_action('admin_init', 'sales_funnel_woocommerce_register_options');
// Options field callback
function sales_funnel_woocommerce_field_callback($args) {
$options = get_option('sales_funnel_woocommerce_options');
$type = $args['type'];
$name = $args['name'];
switch ($type) {
case 'checkbox':
$checked = isset($options[$name]) && $options[$name] === 'on';
echo '<input type="checkbox" name="sales_funnel_woocommerce_options[' . esc_attr($name) . ']" ' . checked($checked, true, false) . ' />';
break;
case 'text':
echo '<input type="text" name="sales_funnel_woocommerce_options[' . esc_attr($name) . ']" value="' . esc_attr($options[$name] ?? '') . '" />';
break;
case 'number':
echo '<input type="number" name="sales_funnel_woocommerce_options[' . esc_attr($name) . ']" value="' . esc_attr($options[$name] ?? '') . '" />';
break;
}
}
// Apply small order fee
function sales_funnel_woocommerce_apply_small_fee() {
$options = get_option('sales_funnel_woocommerce_options');
$enable_small_fee = isset($options['enable_small_fee']) && $options['enable_small_fee'] === 'on';
$small_fee_name = $options['small_fee_name'] ?? '';
$small_fee_amount = $options['small_fee_amount'] ?? '';
$small_fee_threshold = $options['small_fee_threshold'] ?? '';
$cart_total = WC()->cart->subtotal;
if ($enable_small_fee && $cart_total < $small_fee_threshold) {
$fee_label = $small_fee_name;
$fee_total = floatval($small_fee_amount);
WC()->cart->add_fee($fee_label, $fee_total);
}
}
add_action('woocommerce_cart_calculate_fees', 'sales_funnel_woocommerce_apply_small_fee');
// Change "Add to Cart" button text
function sales_funnel_woocommerce_change_add_to_cart_text($text) {
$options = get_option('sales_funnel_woocommerce_options');
$add_to_cart_text = $options['add_to_cart_text'] ?? '';
if (!empty($add_to_cart_text)) {
$text = $add_to_cart_text;
}
return $text;
}
add_filter('woocommerce_product_single_add_to_cart_text', 'sales_funnel_woocommerce_change_add_to_cart_text', 10, 1);
add_filter('woocommerce_product_add_to_cart_text', 'sales_funnel_woocommerce_change_add_to_cart_text', 10, 1);
// Redirect "Buy Now" button to checkout
function sales_funnel_woocommerce_redirect_buy_now_to_checkout($url, $product) {
$options = get_option('sales_funnel_woocommerce_options');
$enable_buy_now = isset($options['enable_buy_now']) && $options['enable_buy_now'] === 'on';
$buy_now_text = $options['buy_now_text'] ?? '';
if ($enable_buy_now && !empty($buy_now_text) && $product && $product->is_purchasable() && $product->is_in_stock() && isset($_GET['buy_now']) && $_GET['buy_now'] == $product->get_id()) {
if (isset($options['cart_redirection_to_checkout']) && $options['cart_redirection_to_checkout'] === 'on') {
// Redirect to the checkout page
wp_redirect(wc_get_checkout_url());
exit;
}
}
return $url;
}
add_filter('woocommerce_product_add_to_cart_url', 'sales_funnel_woocommerce_redirect_buy_now_to_checkout', 10, 2);
add_filter('woocommerce_product_single_add_to_cart_url', 'sales_funnel_woocommerce_redirect_buy_now_to_checkout', 10, 2);
// Add "Buy Now" button to shop listing page
function sales_funnel_woocommerce_add_buy_now_button() {
global $product;
$options = get_option('sales_funnel_woocommerce_options');
$enable_buy_now = isset($options['enable_buy_now']) && $options['enable_buy_now'] === 'on';
$buy_now_text = $options['buy_now_text'] ?? '';
$buy_now_margin_bottom = $options['buy_now_margin_bottom'] ?? '';
if ($enable_buy_now && !empty($buy_now_text) && $product && $product->is_purchasable() && $product->is_in_stock()) {
$buy_now_url = add_query_arg('buy_now', $product->get_id(), $product->add_to_cart_url());
echo '<a href="' . esc_url($buy_now_url) . '" class="button alt" style="margin-bottom: ' . esc_attr($buy_now_margin_bottom) . 'px;">' . esc_html($buy_now_text) . '</a>';
}
}
add_action('woocommerce_after_shop_loop_item', 'sales_funnel_woocommerce_add_buy_now_button', 10);
// Add cart to checkout page
function sales_funnel_woocommerce_add_cart_to_checkout_page() {
$options = get_option('sales_funnel_woocommerce_options');
$add_cart_to_checkout_page = isset($options['add_cart_to_checkout_page']) && $options['add_cart_to_checkout_page'] === 'on';
if ($add_cart_to_checkout_page) {
echo do_shortcode('[woocommerce_cart]');
}
}
add_action('woocommerce_before_checkout_form', 'sales_funnel_woocommerce_add_cart_to_checkout_page', 5);
// Redirect cart page to checkout if cart is not empty
add_action('template_redirect', 'sales_funnel_woocommerce_redirect_cart_to_checkout');
function sales_funnel_woocommerce_redirect_cart_to_checkout() {
$options = get_option('sales_funnel_woocommerce_options');
$cart_redirection_to_checkout = isset($options['cart_redirection_to_checkout']) && $options['cart_redirection_to_checkout'] === 'on';
if ($cart_redirection_to_checkout && is_cart() && !WC()->cart->is_empty()) {
wp_redirect(wc_get_checkout_url());
exit;
}
}
// Redirect cart page to shop if cart is empty
add_action('template_redirect', 'sales_funnel_woocommerce_redirect_cart_to_shop');
function sales_funnel_woocommerce_redirect_cart_to_shop() {
$options = get_option('sales_funnel_woocommerce_options');
$cart_redirection_to_checkout = isset($options['cart_redirection_to_checkout']) && $options['cart_redirection_to_checkout'] === 'on';
if ($cart_redirection_to_checkout && is_cart() && WC()->cart->is_empty()) {
wp_redirect(get_permalink(wc_get_page_id('shop')));
exit;
}
}
function sales_funnel_woocommerce_settings_saved_notice() {
echo '<div class="notice notice-success is-dismissible">';
echo '<p>Settings saved.</p>';
echo '</div>';
}
// Plugin uninstall hook
function sales_funnel_woocommerce_uninstall() {
// Code to run on plugin uninstallation
delete_option('sales_funnel_woocommerce_options');
}
register_uninstall_hook(__FILE__, 'sales_funnel_woocommerce_uninstall');