# sellkit/1.6.8/includes/funnel/steps/thankyou.php

SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster, version 1.6.8. 96 lines.

- Page: https://pluginprobe.com/plugins/sellkit/1.6.8/code/includes/funnel/steps/thankyou.php
- Raw: https://pluginprobe.com/plugins/sellkit/1.6.8/raw/includes/funnel/steps/thankyou.php
- Modified: 2023-06-20T08:15:52+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/sellkit/1.6.8/code/includes/funnel/steps/thankyou.php#L10-L20`.

```php
<?php

namespace Sellkit\Funnel\Steps;

defined( 'ABSPATH' ) || die();

/**
 * Class Sellkit_Thankyou.
 *
 * @since 1.1.0
 */
class Thankyou extends Base_Step {

	/**
	 * Thankyou constructor.
	 *
	 * @since 1.1.0
	 */
	public function __construct() {
		parent::__construct();

		add_action( 'template_redirect', [ $this, 'redirect_after_purchase' ], 10 );
	}

	/**
	 * Redirects after purchasing.
	 *
	 * @since 1.1.0
	 */
	public function redirect_after_purchase() {
		if ( ! class_exists( 'woocommerce' ) ) {
			return;
		}

		global $wp;

		$funnel = sellkit_funnel();

		if ( ! empty( $funnel->funnel_id ) && 'thankyou' === $funnel->current_step_data['type']['key'] ) {
			return;
		}

		if ( ! function_exists( 'is_checkout' ) ) {
			return;
		}

		$order_key = filter_input( INPUT_GET, 'key', FILTER_SANITIZE_FULL_SPECIAL_CHARS );

		if ( ! empty( $order_key ) ) {
			$order_id  = wc_get_order_id_by_order_key( $order_key );
			$order     = wc_get_order( $order_id );
			$next_step = ! empty( $order ) ? $order->get_meta( 'sellkit_funnel_next_step_data' ) : '';
			$funnel_id = get_post_meta( $order_id, 'sellkit_funnel_id', true );

			if ( empty( $next_step ) ) {
				return;
			}

			if ( empty( $funnel_id ) ) {
				$step_id   = intval( $next_step['page_id'] );
				$step_data = get_post_meta( $step_id, 'step_data', true );
				$funnel_id = (int) $step_data['funnel_id'];
			}

			// For the new method( upsell popup ) we show thakyou page right away after checkout step.
			$thankyou_id    = $this->find_funnel_thankyou_page( $funnel_id );
			$next_step_link = add_query_arg( [ 'order-key' => $order_key ], get_permalink( $thankyou_id ) );
			$last_price     = $order->get_total() - $order->get_total_discount() - $order->get_total_tax();

			$this->contacts->add_total_spent( $last_price, $funnel_id );

			wp_safe_redirect( $next_step_link );
			exit();
		}
	}

	/**
	 * Find funnel thankyou page using one of steps data.
	 *
	 * @param array $funnel_id funnel id.
	 * @since 1.6.2
	 */
	private function find_funnel_thankyou_page( $funnel_id ) {
		$funnel_data = get_post_meta( $funnel_id, 'nodes', true );
		$id          = 0;

		foreach ( $funnel_data as $step ) {
			if ( 'thankyou' === $step['type']['key'] ) {
				$id = $step['page_id'];
			}
		}

		return $id;
	}
}

```
