-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-checkout-plugin.php
More file actions
429 lines (402 loc) · 18 KB
/
custom-checkout-plugin.php
File metadata and controls
429 lines (402 loc) · 18 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
/**
* Plugin Name: Checkoutify Lite - Easy Checkout for WooCommerce
* Plugin URI: https://checkoutify.net/
* Description: Transform your WooCommerce checkout with modern, conversion-optimized templates. Features include customizable checkout designs, field manager, Skip cart option. Boost your sales with professional checkout experiences that convert visitors into customers.
* Version: 1.1
* Author: dominiio
* Text Domain: checkoutify-lite
* Domain Path: /languages
* Requires at least: 6.0
* Tested up to: 6.9
* Requires PHP: 7.4
* WC requires at least: 7.0
* WC tested up to: 10.3.6
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Define plugin constants
define('CKFYLT25_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('CKFYLT25_PLUGIN_URL', plugin_dir_url(__FILE__));
define('CKFYLT25_PLUGIN_BASENAME', plugin_basename(__FILE__));
// Include necessary files
require_once CKFYLT25_PLUGIN_DIR . 'includes/admin-settings.php';
require_once CKFYLT25_PLUGIN_DIR . 'includes/checkout-override.php';
require_once CKFYLT25_PLUGIN_DIR . 'includes/class-ckfylt25-testimonial-display.php';
require_once CKFYLT25_PLUGIN_DIR . 'includes/class-ckfylt25-order-bump-display.php';
register_activation_hook(__FILE__, 'checkoutify_lite_on_activation');
function checkoutify_lite_on_activation() {
if (!get_option('checkoutify_lite_activation_time')) {
update_option('checkoutify_lite_activation_time', time());
}
}
/**
* Main plugin class
* Handles plugin initialization, text domain loading, and core functionality
*/
class CKFYLT25_Checkoutify_Plugin {
/**
* Constructor - Initialize plugin hooks and actions
*/
public function __construct() {
add_action('init', array($this, 'ckfylt25_init_plugin'));
add_action('plugins_loaded', array($this, 'ckfylt25_load_textdomain'));
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);
}
});
add_filter('plugin_action_links_' . CKFYLT25_PLUGIN_BASENAME, array($this, 'ckfylt25_add_plugin_action_links'));
add_action('wp_enqueue_scripts', array($this, 'ckfylt25_enqueue_skip_cart_scripts'));
add_filter('woocommerce_add_to_cart_redirect', array($this, 'ckfylt25_skip_cart_redirect'));
}
/**
* Load plugin text domain for internationalization
* Note: load_plugin_textdomain() is not needed for WordPress.org hosted plugins
*/
public function ckfylt25_load_textdomain() {
// Load plugin text domain for local/development environments and non-dotorg installs
load_plugin_textdomain(
'checkoutify-lite',
false,
dirname(plugin_basename(__FILE__)) . '/languages'
);
}
/**
* Initialize plugin components
*/
public function ckfylt25_init_plugin() {
if (!class_exists('WooCommerce')) {
add_action('admin_notices', array($this, 'ckfylt25_woocommerce_missing_notice'));
return;
}
new CKFYLT25_Admin_Settings();
global $ckfylt25_checkout_override;
$ckfylt25_checkout_override = new CKFYLT25_Checkout_Override();
new CKFYLT25_Testimonial_Display();
new CKFYLT25_Order_Bump_Display();
}
/**
* Add action links to plugin list page
*
* @param array $links Existing plugin action links
* @return array Modified plugin action links
*/
public function ckfylt25_add_plugin_action_links($links) {
$settings_link = '<a href="' . esc_url(admin_url('admin.php?page=checkoutify-settings')) . '">' . esc_html__('Settings', 'checkoutify-lite') . '</a>';
$upgrade_link = '<a href="https://checkoutify.net" target="_blank" style="color: #10b981; font-weight: 600;">' . esc_html__('Upgrade to Pro', 'checkoutify-lite') . '</a>';
array_unshift($links, $settings_link);
$links[] = $upgrade_link;
return $links;
}
/**
* Enqueue Skip Cart scripts and styles
*/
public function ckfylt25_enqueue_skip_cart_scripts() {
if (get_option('ckfylt25_skip_cart_enabled', false)) {
wp_enqueue_script(
'ckfylt25-skip-cart',
CKFYLT25_PLUGIN_URL . 'assets/js/skip-cart.js',
array('jquery'),
'1.0.0',
true
);
wp_localize_script('ckfylt25-skip-cart', 'ckfylt25SkipCartData', array(
'enabled' => get_option('ckfylt25_skip_cart_enabled', false),
'checkoutUrl' => wc_get_checkout_url(),
'ajaxUrl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('ckfylt25_skip_cart_nonce')
));
wp_add_inline_script('ckfylt25-skip-cart', '
window.ckfylt25SkipCartEnabled = ' . (get_option('ckfylt25_skip_cart_enabled', false) ? 'true' : 'false') . ';
window.ckfylt25CheckoutUrl = "' . esc_js(wc_get_checkout_url()) . '";
');
}
}
/**
* Server-side redirect to checkout when Skip Cart is enabled
* Ensures redirect works even when AJAX events are not fired
*/
public function ckfylt25_skip_cart_redirect($url) {
if (get_option('ckfylt25_skip_cart_enabled', false)) {
return wc_get_checkout_url();
}
return $url;
}
/**
* Display admin notice when WooCommerce is not active
*/
public function ckfylt25_woocommerce_missing_notice() {
?>
<div class="notice notice-error">
<p><?php esc_html_e('Checkoutify - Easy Checkout for WooCommerce requires WooCommerce to be installed and active.', 'checkoutify-lite'); ?></p>
</div>
<?php
}
}
// Initialize the plugin
new CKFYLT25_Checkoutify_Plugin();
/**
* Add plugin meta links for WordPress.org compatibility
*
* @param array $links Existing plugin meta links
* @param string $file Plugin file path
* @return array Modified plugin meta links
*/
function ckfylt25_checkoutify_plugin_meta($links, $file) {
if (plugin_basename(__FILE__) === $file) {
$links[] = '<a href="https://checkoutify.net" target="_blank">' . esc_html__('Documentation', 'checkoutify-lite') . '</a>';
$links[] = '<a href="https://checkoutify.net" target="_blank">' . esc_html__('Changelog', 'checkoutify-lite') . '</a>';
}
return $links;
}
add_filter('plugin_row_meta', 'ckfylt25_checkoutify_plugin_meta', 10, 2);
// Add hook for testimonials
add_action('ckfylt25_after_order_summary', 'ckfylt25_display_default_testimonials');
/**
* Display default testimonials section
* This function is used as a fallback when testimonials are enabled but no custom testimonials are configured
*/
function ckfylt25_display_default_testimonials() {
$testimonials_enabled = get_option('ckfylt25_enable_testimonials', false);
if ($testimonials_enabled) {
$testimonials = get_option('ckfylt25_testimonials', array());
if (!empty($testimonials)) {
echo '<div class="ckfylt25-testimonials-section">';
echo '<h3>' . esc_html__('What Our Customers Say', 'checkoutify-lite') . '</h3>';
echo '<div class="ckfylt25-testimonials-grid">';
foreach ($testimonials as $testimonial) {
echo '<div class="ckfylt25-testimonial">';
if (!empty($testimonial['avatar'])) {
echo '<div class="ckfylt25-testimonial-avatar">';
echo '<img src="' . esc_url($testimonial['avatar']) . '" alt="' . esc_attr($testimonial['name']) . '">';
echo '</div>';
}
echo '<div class="ckfylt25-testimonial-content">';
echo '<p class="ckfylt25-testimonial-text">"' . esc_html($testimonial['text']) . '"</p>';
echo '<p class="ckfylt25-testimonial-name">- ' . esc_html($testimonial['name']) . '</p>';
if (!empty($testimonial['title'])) {
echo '<p class="ckfylt25-testimonial-title">' . esc_html($testimonial['title']) . '</p>';
}
echo '</div>';
echo '</div>';
}
echo '</div>';
echo '</div>';
}
}
}
// Hook for before testimonials
add_action('ckfylt25_before_testimonials', 'ckfylt25_before_testimonials_section');
/**
* Add content before testimonials section
* This hook allows for additional content to be displayed before testimonials
*/
function ckfylt25_before_testimonials_section() {
echo '<div class="ckfylt25-testimonials-intro" style="text-align: center;">';
echo '</div>';
}
/**
* Custom function to render shipping methods in Fluid template
* This function provides a custom shipping methods display for specific templates
*/
function ckfylt25_render_shipping_methods() {
if (!function_exists('WC')) {
return;
}
$packages = WC()->shipping()->get_packages();
if (empty($packages)) {
WC()->cart->calculate_shipping();
$packages = WC()->shipping()->get_packages();
}
$first = true;
foreach ($packages as $i => $package) {
$chosen_method = isset(WC()->session->chosen_shipping_methods[$i]) ? WC()->session->chosen_shipping_methods[$i] : '';
$product_names = array();
if (count($packages) > 1) {
foreach ($package['contents'] as $item_id => $values) {
$product_names[$item_id] = $values['data']->get_name() . ' ×' . $values['quantity'];
}
$product_names = apply_filters('woocommerce_shipping_package_details_array', $product_names, $package);
}
$available_methods = $package['rates'];
$show_package_details = count($packages) > 1;
$package_details = implode(', ', $product_names);
/* translators: %d: shipping package number */
$package_name = apply_filters('woocommerce_shipping_package_name', ((($i + 1) > 1) ? sprintf(_x('Shipping %d', 'shipping packages', 'checkoutify-lite'), ($i + 1)) : _x('Shipping', 'shipping packages', 'checkoutify-lite')), $i, $package);
$index = $i;
$formatted_destination = WC()->countries->get_formatted_address($package['destination'], ', ');
$has_calculated_shipping = WC()->customer->has_calculated_shipping();
include CKFYLT25_PLUGIN_DIR . 'templates/Fluid/shipping-methods.php';
do_action('woocommerce_review_order_after_shipping');
$first = false;
}
}
// AJAX function to get shipping methods
add_action('wp_ajax_ckfylt25_get_shipping_methods', 'ckfylt25_get_shipping_methods_ajax');
add_action('wp_ajax_nopriv_ckfylt25_get_shipping_methods', 'ckfylt25_get_shipping_methods_ajax');
/**
* AJAX handler to get shipping methods
* This function handles AJAX requests for shipping methods updates
*/
function ckfylt25_get_shipping_methods_ajax() {
if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'woocommerce-update_order_review')) {
wp_die( esc_html__( 'Invalid nonce', 'checkoutify-lite' ) );
}
if (!function_exists('WC')) {
wp_send_json_error( __( 'WooCommerce is not available', 'checkoutify-lite' ) );
}
WC()->cart->calculate_shipping();
$packages = WC()->shipping()->get_packages();
if (empty($packages)) {
wp_send_json_success(array(
'html' => '<div class="shipping-no-methods">' . esc_html__('No shipping methods available for this address.', 'checkoutify-lite') . '</div>'
));
return;
}
ob_start();
do_action('woocommerce_review_order_before_shipping');
ckfylt25_render_shipping_methods();
do_action('woocommerce_review_order_after_shipping');
$shipping_html = ob_get_clean();
if (empty(trim($shipping_html))) {
$shipping_html = '<div class="shipping-no-methods">' . esc_html__('No shipping methods available for this address.', 'checkoutify-lite') . '</div>';
}
wp_send_json_success(array(
'html' => $shipping_html
));
}
// AJAX function to get sidebar totals
add_action('wp_ajax_ckfylt25_get_sidebar_totals', 'ckfylt25_get_sidebar_totals_ajax');
add_action('wp_ajax_nopriv_ckfylt25_get_sidebar_totals', 'ckfylt25_get_sidebar_totals_ajax');
add_action('wp_ajax_ckfylt25_get_sidebar_totals_simple', 'ckfylt25_get_sidebar_totals_simple_ajax');
add_action('wp_ajax_nopriv_ckfylt25_get_sidebar_totals_simple', 'ckfylt25_get_sidebar_totals_simple_ajax');
/**
* AJAX handler to get sidebar totals
* This function handles AJAX requests for sidebar totals updates
*/
function ckfylt25_get_sidebar_totals_ajax() {
$nonce_valid = false;
if (isset($_POST['nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'woocommerce-process_checkout')) {
$nonce_valid = true;
} elseif (isset($_POST['nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'woocommerce-update_order_review')) {
$nonce_valid = true;
} elseif (isset($_POST['nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'woocommerce-process_checkout')) {
$nonce_valid = true;
}
if (!$nonce_valid) {
wp_send_json_error( __( 'Invalid nonce', 'checkoutify-lite' ) );
return;
}
if (!function_exists('WC')) {
wp_send_json_error( __( 'WooCommerce is not available', 'checkoutify-lite' ) );
}
WC()->cart->calculate_totals();
$sidebar_html = ckfylt25_generate_sidebar_totals();
wp_send_json_success(array(
'html' => $sidebar_html
));
}
/**
* Alternative AJAX function without nonce for cases where nonce is not available
* This function handles AJAX requests for sidebar totals without nonce verification
*/
function ckfylt25_get_sidebar_totals_simple_ajax() {
if (!function_exists('WC')) {
wp_send_json_error( __( 'WooCommerce is not available', 'checkoutify-lite' ) );
}
WC()->cart->calculate_totals();
$sidebar_html = ckfylt25_generate_sidebar_totals_native();
wp_send_json_success(array(
'html' => $sidebar_html
));
}
/**
* Function to generate sidebar totals with shipping included
* This function creates HTML for the sidebar totals display
*
* @return string HTML content for sidebar totals
*/
function ckfylt25_generate_sidebar_totals() {
WC()->cart->calculate_shipping();
WC()->cart->calculate_totals();
ob_start();
echo '<tr class="cart-subtotal">';
echo '<th>' . esc_html__('Subtotal', 'checkoutify-lite') . '</th>';
echo '<td>' . wp_kses_post(WC()->cart->get_cart_subtotal()) . '</td>';
echo '</tr>';
foreach (WC()->cart->get_coupons() as $code => $coupon) {
echo '<tr class="cart-discount coupon-' . esc_attr(sanitize_title($code)) . '">';
echo '<th>' . wp_kses_post(wc_cart_totals_coupon_label($coupon)) . '</th>';
echo '<td>' . wp_kses_post(wc_cart_totals_coupon_html($coupon)) . '</td>';
echo '</tr>';
}
if (WC()->cart->needs_shipping() && WC()->cart->show_shipping()) {
$packages = WC()->shipping()->get_packages();
$chosen_methods = WC()->session->get('chosen_shipping_methods');
foreach ($packages as $i => $package) {
$chosen_method = isset($chosen_methods[$i]) ? $chosen_methods[$i] : '';
if ($chosen_method && isset($package['rates'][$chosen_method])) {
$method = $package['rates'][$chosen_method];
echo '<tr class="shipping">';
echo '<th>' . esc_html__('Shipping', 'checkoutify-lite') . '</th>';
echo '<td>' . wp_kses_post(wc_cart_totals_shipping_method_label($method)) . '</td>';
echo '</tr>';
} elseif (!empty($package['rates'])) {
$first_method = reset($package['rates']);
echo '<tr class="shipping">';
echo '<th>' . esc_html__('Shipping', 'checkoutify-lite') . '</th>';
echo '<td>' . wp_kses_post(wc_cart_totals_shipping_method_label($first_method)) . '</td>';
echo '</tr>';
}
}
}
foreach (WC()->cart->get_fees() as $fee) {
echo '<tr class="fee">';
echo '<th>' . esc_html($fee->name) . '</th>';
echo '<td>' . wp_kses_post(wc_cart_totals_fee_html($fee)) . '</td>';
echo '</tr>';
}
if (wc_tax_enabled() && !WC()->cart->display_prices_including_tax()) {
if ('itemized' === get_option('woocommerce_tax_total_display')) {
foreach (WC()->cart->get_tax_totals() as $code => $tax) {
echo '<tr class="tax-rate tax-rate-' . esc_attr(sanitize_title($code)) . '">';
echo '<th>' . esc_html($tax->label) . '</th>';
echo '<td>' . wp_kses_post($tax->formatted_amount) . '</td>';
echo '</tr>';
}
} else {
echo '<tr class="tax-total">';
echo '<th>' . esc_html(WC()->countries->tax_or_vat()) . '</th>';
echo '<td>' . wp_kses_post(wc_cart_totals_taxes_total_html()) . '</td>';
echo '</tr>';
}
}
$total = WC()->cart->get_total();
$formatted_total = WC()->cart->get_cart_total();
echo '<tr class="order-total">';
echo '<th>' . esc_html__('TOTAL - Including Shipping', 'checkoutify-lite') . '</th>';
echo '<td>' . wp_kses_post($formatted_total) . '</td>';
echo '</tr>';
$html = ob_get_clean();
return $html;
}
/**
* Alternative function that uses native WooCommerce template
* This function generates sidebar totals using WooCommerce's native template
*
* @return string HTML content for sidebar totals using native template
*/
function ckfylt25_generate_sidebar_totals_native() {
WC()->cart->calculate_shipping();
WC()->cart->calculate_totals();
ob_start();
wc_get_template('checkout/review-order.php', array('checkout' => WC()->checkout()));
$order_summary_html = ob_get_clean();
$order_summary_html = preg_replace('/<tr[^>]*class="[^"]*shipping[^"]*"[^>]*>.*?<\/tr>/is', '', $order_summary_html);
return $order_summary_html;
}