-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwcs-cancel-subscription-confirmation.php
executable file
·91 lines (76 loc) · 3.91 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
<?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_confirmation_required = apply_filters( 'wcs_cancel_confirmation_promt_enabled', ( 'yes' == get_option( "wcs-cancel-confirmation", 'no' ) ) ? true : false );
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 );
$script_atts = array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'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" ) ) ,
'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" ) )
);
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 for the cancellation reason', 'wcs-cancel-confirmation' ),
'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.' ),
),
) );
return $settings;
}
add_filter( 'woocommerce_subscription_settings', 'add_cancelation_settings' );