| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Funnel\Steps; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || die(); |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Sellkit_Thankyou. |
| 9 |
* |
| 10 |
* @since 1.1.0 |
| 11 |
*/ |
| 12 |
class Thankyou extends Base_Step { |
| 13 |
|
| 14 |
/** |
| 15 |
* Thankyou constructor. |
| 16 |
* |
| 17 |
* @since 1.1.0 |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
parent::__construct(); |
| 21 |
|
| 22 |
add_action( 'template_redirect', [ $this, 'redirect_after_purchase' ], 10 ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Redirects after purchasing. |
| 27 |
* |
| 28 |
* @since 1.1.0 |
| 29 |
*/ |
| 30 |
public function redirect_after_purchase() { |
| 31 |
if ( ! class_exists( 'woocommerce' ) ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
global $wp; |
| 36 |
|
| 37 |
$funnel = sellkit_funnel(); |
| 38 |
|
| 39 |
if ( ! empty( $funnel->funnel_id ) && 'thankyou' === $funnel->current_step_data['type']['key'] ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
if ( ! function_exists( 'is_checkout' ) ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
$order_key = filter_input( INPUT_GET, 'key', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 48 |
|
| 49 |
if ( ! empty( $order_key ) ) { |
| 50 |
$order_id = wc_get_order_id_by_order_key( $order_key ); |
| 51 |
$order = wc_get_order( $order_id ); |
| 52 |
$next_step = ! empty( $order ) ? $order->get_meta( 'sellkit_funnel_next_step_data' ) : ''; |
| 53 |
$funnel_id = get_post_meta( $order_id, 'sellkit_funnel_id', true ); |
| 54 |
|
| 55 |
if ( empty( $next_step ) ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if ( empty( $funnel_id ) ) { |
| 60 |
$step_id = intval( $next_step['page_id'] ); |
| 61 |
$step_data = get_post_meta( $step_id, 'step_data', true ); |
| 62 |
$funnel_id = (int) $step_data['funnel_id']; |
| 63 |
} |
| 64 |
|
| 65 |
// For the new method( upsell popup ) we show thakyou page right away after checkout step. |
| 66 |
$thankyou_id = $this->find_funnel_thankyou_page( $funnel_id ); |
| 67 |
$next_step_link = add_query_arg( [ 'order-key' => $order_key ], get_permalink( $thankyou_id ) ); |
| 68 |
$last_price = $order->get_total() - $order->get_total_discount() - $order->get_total_tax(); |
| 69 |
|
| 70 |
$this->contacts->add_total_spent( $last_price, $funnel_id ); |
| 71 |
|
| 72 |
wp_safe_redirect( $next_step_link ); |
| 73 |
exit(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Find funnel thankyou page using one of steps data. |
| 79 |
* |
| 80 |
* @param array $funnel_id funnel id. |
| 81 |
* @since 1.6.2 |
| 82 |
*/ |
| 83 |
private function find_funnel_thankyou_page( $funnel_id ) { |
| 84 |
$funnel_data = get_post_meta( $funnel_id, 'nodes', true ); |
| 85 |
$id = 0; |
| 86 |
|
| 87 |
foreach ( $funnel_data as $step ) { |
| 88 |
$step['type'] = (array) $step['type']; |
| 89 |
|
| 90 |
if ( 'thankyou' === $step['type']['key'] ) { |
| 91 |
$id = $step['page_id']; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return $id; |
| 96 |
} |
| 97 |
} |
| 98 |
|