Skip to content

Instantly share code, notes, and snippets.

@swoboda
Created July 18, 2020 17:48
Show Gist options
  • Save swoboda/985fc8a37ddef9daf7c24838d9140f72 to your computer and use it in GitHub Desktop.
Save swoboda/985fc8a37ddef9daf7c24838d9140f72 to your computer and use it in GitHub Desktop.
WIP: AutomateWoo Trigger for Auto Renew Toggle
<?php
/**
* This is an example trigger that is triggered via a WordPress action and includes a user data item.
* Trigger with: do_action('my_custom_action', $user_id );
*/
class WPDesk_AutomateWoo_Subscription_Auto_Renew_Trigger extends AutomateWoo\Trigger {
/** @var array - Define which data items are set by this trigger, this determines which rules and actions will be available */
public $supplied_data_items = array( 'customer', 'subscription' );
/**
* Set up the trigger
*/
public function init() {
$this->title = __( 'Subscription Auto Renew Disabled', 'wpdesk' );
$this->group = __( 'Subscriptions', 'wpdesk' );
}
/**
* Add any fields to the trigger (optional)
*/
public function load_fields() {
$auto_renew = ( new AutomateWoo\Fields\Select() )
->set_title( __( 'Auto renew changes to', 'wpdesk' ) )
->set_name( 'auto_renew' )
->set_options( array( 'enable' => __( 'Enabled', 'wpdesk' ), 'disable' => __( 'Disabled', 'wpdesk' ) ) )
->set_placeholder( __( 'Select toggle', 'wpdesk' ) )
->set_description( __( 'Select toggle', 'wpdesk' ) )
->set_required();
//$this->add_field( $auto_renew );
}
/**
* Defines when the trigger is run
*/
public function register_hooks() {
add_action( 'wp_ajax_wcs_disable_auto_renew', array( $this, 'catch_hooks' ), 9 );
//add_action( 'wp_ajax_wcs_enable_auto_renew', array( $this, 'catch_hooks' ), 9 );
}
/**
* Catches the action and calls the maybe_run() method.
*
* @param $user_id
*/
public function catch_hooks( $subscription ) {
$subscription_id = absint( $_POST['subscription_id'] );
$subscription = wcs_get_subscription( $subscription_id );
$this->maybe_run(array(
'subscription' => $subscription,
'customer' => AutomateWoo\Customer_Factory::get_by_user_id( $subscription->get_user_id() )
));
}
/**
* This method is called just before a queued workflow runs
*
* @param $workflow AutomateWoo\Workflow
* @return bool
*/
public function validate_before_queued_event( $workflow ) {
$subscription = $workflow->data_layer()->get_subscription();
if ( ! $subscription ) {
return false;
}
if ( false === $subscription->get_requires_manual_renewal() ) {
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment