-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwcs-cancel-subscription-confirmation.php
executable file
·132 lines (108 loc) · 5.59 KB
/
wcs-cancel-subscription-confirmation.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
<?php
/**
* Plugin Name: WooCommerce Subscriptions - Delete Cancel Subscription Confirmation
* Plugin URI: https://github.com/Prospress/woocommerce-subscriptions-cancel-subscription-confirmation
* Description: Adds a confirmation dialog when deleting a payment method.
* Author: Prospress Inc.
* Author URI: http://prospress.com/
* Version: 1.0
* License: GPLv3
* Tested up to: 5.0.0
* WC tested up to: 3.5.1
*
* GitHub Plugin URI: Prospress/woocommerce-subscriptions-cancel-subscription-confirmation
* GitHub Branch: master
*
* Copyright 2017 Prospress, Inc. (email : [email protected])
*
* 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
* @author Prospress Inc.
* @since 1.0
*/
function wcs_cancel_subscription_confirmation()
{
if (! function_exists('is_account_page')) {
return;
}
$cancel_confirm_setting = false;
if (('yes' == get_option("wcs-ask-confirmation", 'no')) || ('yes' == get_option('wcs-cancel-confirmation', 'no'))) {
$cancel_confirm_setting = true;
}
$cancel_confirmation_required = apply_filters('wcs_cancel_confirmation_promt_enabled', $cancel_confirm_setting);
if (is_account_page() && 'yes' == $cancel_confirmation_required) {
wp_register_script('wcs-cancel-subscription-confirmation-script', plugin_dir_url(__FILE__) . 'wcs-cancel-subscription-confirmation.js', array( 'jquery' ), '1.0.0', true);
if ('yes' == get_option('wcs-cancel-confirmation', 'no')) {
$promt_msg = apply_filters("wcs_cancel_confirmation_promt_msg", __("Are you sure you want to cancel your subscription?\nIf so, please type the reason why you want to cancel it here:", "wcs-cancel-confirmation"));
$reason_required = true;
} else {
$promt_msg = apply_filters("wcs_cancel_confirmation_promt_msg", __("Are you sure you want to cancel your subscription?", "wcs-cancel-confirmation"));
$reason_required = false;
}
$script_atts = array(
'ajax_url' => admin_url('admin-ajax.php'),
'promt_msg' => $promt_msg ,
'error_msg' => apply_filters("wcs_cancel_confirmation_error_msg", __("There has been an error when saving the cancellation reason. Please try again.", "wcs-cancel-confirmation")),
'reason_required' => $reason_required,
);
wp_localize_script('wcs-cancel-subscription-confirmation-script', 'ajax_object', $script_atts);
wp_enqueue_script('wcs-cancel-subscription-confirmation-script');
}
}
add_action('wp_enqueue_scripts', 'wcs_cancel_subscription_confirmation');
function wcs_cancel_confirmation()
{
$subscription_id = intval($_POST['subscription_id']);
$reason_to_cancel = sanitize_text_field($_POST['reason_to_cancel']);
$subscription = wc_get_order($subscription_id);
$note_id = $subscription->add_order_note(apply_filters("wcs_cancel_confirmation_note_header", __("Cancellation Reason:", "wcs-cancel-confirmation"))."<br /><b><i>".$reason_to_cancel."</i></b>");
$subscription->save();
echo $note_id;
wp_die();
}
add_action('wp_ajax_wcs_cancel_confirmation', 'wcs_cancel_confirmation');
function add_cancelation_settings($settings)
{
$misc_section_end = wp_list_filter($settings, array( 'id' => 'woocommerce_subscriptions_miscellaneous', 'type' => 'sectionend' ));
$spliced_array = array_splice($settings, key($misc_section_end), 0, array(
array(
'name' => __('Ask to confirm on cancellation', 'wcs-cancel-confirmation'),
'desc' => __('Ask for Cancel confirmation', 'wcs-cancel-confirmation'),
'id' => 'wcs-ask-confirmation',
'default' => 'no',
'type' => 'checkbox',
'desc_tip' => __('Ask for the confirmation when the customer cancels a subscription from the My Account page.'),
'checkboxgroup' => 'start',
'show_if_checked' => 'option',
),
array(
'desc' => __('Prompt the customer for a cancellation reason', 'wcs-cancel-confirmation'),
'id' => 'wcs-cancel-confirmation',
'default' => 'no',
'type' => 'checkbox',
'desc_tip' => __('Ask for the cancellation reason when the customer cancels a subscription from the My Account page. The provided reason will be added as a subscription note in the backend.'),
'checkboxgroup' => 'end',
'show_if_checked' => 'yes',
),
));
return $settings;
}
add_filter('woocommerce_subscription_settings', 'add_cancelation_settings');
// Declare Woocommerce HPOS compatibility 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 );
}
} );