event_merge_tag_groups = array( 'bwf_contact', 'wc_subscription' ); $this->event_name = esc_html__( 'Subscriptions Renewal Payment Failed', 'wp-marketing-automations-pro' ); $this->event_desc = esc_html__( 'This event runs after a subscription renewal payment is failed.', 'wp-marketing-automations-pro' ); $this->event_rule_groups = array( 'wc_subscription', 'wc_customer', 'bwf_contact_segments', 'bwf_contact', 'bwf_contact_fields', 'bwf_contact_user', 'bwf_contact_wc', 'bwf_contact_geo', 'bwf_engagement', 'bwf_broadcast' ); $this->optgroup_label = esc_html__( 'Subscription', 'wp-marketing-automations-pro' ); $this->support_lang = true; $this->priority = 25.6; $this->v2 = true; } public static function get_instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } public function load_hooks() { add_action( 'woocommerce_subscription_renewal_payment_failed', [ $this, 'subscription_renewal_payment_failed' ], 11, 2 ); add_action( 'woocommerce_order_status_failed', [ $this, 'early_subscription_renewal_payment_failed' ], 11, 1 ); } public function subscription_renewal_payment_failed( $subscription, $order ) { $subscription_id = $subscription->get_id(); $order_id = $order->get_id(); $this->email = $subscription->get_billing_email(); $this->process( $subscription_id, $order_id ); } public function early_subscription_renewal_payment_failed( $order_id ) { $order = wc_get_order( $order_id ); $subscription_id = $order->get_meta( '_subscription_renewal_early' ); if ( empty( $subscription_id ) || 'failed' !== $order->get_status() || ! empty( $order->get_meta( 'bwfan_renewal_payment_failed' ) ) ) { return; } $this->email = $order->get_billing_email(); $this->process( $subscription_id, $order_id ); } /** * Make the required data for the current event and send it asynchronously. * * @param $subscription_id * @param $order_id */ public function process( $subscription_id, $order_id ) { $data = $this->get_default_data(); $data['wc_subscription_id'] = $subscription_id; $data['order_id'] = $order_id; $data['email'] = $this->email; $this->send_async_call( $data ); } /** * Set up rules data * * @param $value */ public function pre_executable_actions( $value ) { BWFAN_Core()->rules->setRulesData( $this->order, 'wc_order' ); BWFAN_Core()->rules->setRulesData( $this->subscription, 'wc_subscription' ); BWFAN_Core()->rules->setRulesData( $this->event_automation_id, 'automation_id' ); BWFAN_Core()->rules->setRulesData( BWFAN_Common::get_bwf_customer( $this->order->get_billing_email(), $this->order->get_user_id() ), 'bwf_customer' ); } /** * Registers the tasks for current event. * * @param $automation_id * @param $integration_data * @param $event_data */ public function register_tasks( $automation_id, $integration_data, $event_data ) { if ( ! is_array( $integration_data ) ) { return; } $data_to_send = $this->get_event_data(); $this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send ); } public function get_event_data() { $this->subscription = wcs_get_subscription( $this->subscription_id ); $user_id = $this->get_user_id_event(); $data_to_send = [ 'global' => [] ]; $data_to_send['global']['wc_subscription_id'] = $this->subscription_id; $data_to_send['global']['wc_subscription'] = is_object( $this->subscription ) ? $this->subscription : ''; $data_to_send['global']['email'] = $this->email; $data_to_send['global']['order_id'] = is_object( $this->order ) ? BWFAN_Woocommerce_Compatibility::get_order_id( $this->order ) : ''; $data_to_send['global']['wc_order'] = is_object( $this->order ) ? $this->order : ''; if ( intval( $user_id ) > 0 ) { $data_to_send['global']['user_id'] = $user_id; $email = BWFAN_PRO_Common::get_contact_email( $user_id ); if ( ! empty( $email ) ) { $data_to_send['global']['email'] = $email; } } return $data_to_send; } /** * Make the view data for the current event which will be shown in task listing screen. * * @param $global_data * * @return false|string */ public function get_task_view( $global_data ) { ob_start(); ?>