Last active
April 25, 2020 11:38
-
-
Save seb86/99e9df6f80be9c6f3da9a4b94a0d2755 to your computer and use it in GitHub Desktop.
Disables certain action buttons for weekly subscriptions and subscriptions with a free trial. Weekly subscriptions can not be cancelled for at least a year.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: WooCommerce Subscriptions - Contract Subscriptions | |
* Description: Disables certain action buttons for weekly subscriptions and subscriptions with a free trial. Weekly subscriptions can not be cancelled for at least a year. | |
* Author: Sébastien Dumont | |
* Author URI: https://sebastiendumont.com | |
* Version: 0.0.2 | |
* | |
* License: GNU General Public License v3.0 | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
*/ | |
add_filter( 'wcs_view_subscription_actions', 'wcs_contract_remove_my_subscriptions_button', 100, 2 ); | |
function wcs_contract_remove_my_subscriptions_button( $actions, $subscription ) { | |
$subtime = $subscription->get_billing_interval() . "-" . $subscription->get_billing_period(); | |
// If subscription is billed per week then remove actions. | |
if ( in_array( $subtime, array( '1-week' ), true ) ) { | |
foreach ( $actions as $action_key => $action ) { | |
switch ( $action_key ) { | |
case 'switch': // Hide "Switch Subscription" button. | |
case 'resubscribe': // Hide "Resubscribe" button from an expired or cancelled subscription. | |
case 'suspend': // Hide "Suspend" button on subscriptions that are "active". | |
unset( $actions[ $action_key ] ); | |
break; | |
} | |
} | |
// Only remove cancel action if the subscription has not yet paid a year's worth. | |
if ( $subscription->get_payment_count() < 52 ) { | |
unset( $actions[ 'cancel' ] ); | |
} | |
} | |
// If subscription has a free trial then remove cancel action. | |
if ( $subscription->get_time( 'trial_end' ) > 0 && $subscription->get_payment_count() < 2 ) { | |
unset( $actions[ 'cancel' ] ); | |
} | |
return $actions; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment