| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Global_Checkout; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
use Elementor\Plugin as Elementor; |
| 8 |
use Sellkit\Elementor\Modules\Checkout\Classes\Helper; |
| 9 |
use Sellkit\Funnel\Steps\Checkout as CheckoutStep; |
| 10 |
|
| 11 |
/** |
| 12 |
* Checkout. |
| 13 |
* |
| 14 |
* @SuppressWarnings(PHPMD.NPathComplexity) |
| 15 |
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 16 |
* @since 1.7.4 |
| 17 |
*/ |
| 18 |
class Checkout { |
| 19 |
const SELLKIT_GLOBAL_CHECKOUT_OPTION = 'sellkit_global_checkout_id'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Construct. |
| 23 |
* |
| 24 |
* @since 1.7.4 |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
add_action( 'wp', [ $this, 'init_sellkit_global_checkout' ] ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Init sellkit global checkout. |
| 32 |
* |
| 33 |
* @since 1.7.4 |
| 34 |
*/ |
| 35 |
public function init_sellkit_global_checkout() { |
| 36 |
$global_checkout_id = get_option( self::SELLKIT_GLOBAL_CHECKOUT_OPTION, 0 ); |
| 37 |
|
| 38 |
if ( |
| 39 |
0 === $global_checkout_id || |
| 40 |
'publish' !== get_post_status( (int) $global_checkout_id ) || |
| 41 |
! is_checkout() || |
| 42 |
! defined( 'ELEMENTOR_VERSION' ) |
| 43 |
) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
$steps = get_post_meta( $global_checkout_id, 'nodes', true ); |
| 48 |
$checkout_id = 0; |
| 49 |
$bump_data = []; |
| 50 |
$optimization_data = ''; |
| 51 |
|
| 52 |
foreach ( $steps as $step ) { |
| 53 |
$step['type'] = (array) $step['type']; |
| 54 |
|
| 55 |
if ( 'checkout' === $step['type']['key'] ) { |
| 56 |
$checkout_id = $step['page_id']; |
| 57 |
$bump_data = ! empty( $step['bump'] ) ? $step['bump'] : []; |
| 58 |
$optimization_data = ! empty( $step['data']['optimization'] ) ? $step['data']['optimization'] : ''; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
if ( 0 === $checkout_id ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
// Remove previous content. |
| 67 |
remove_all_filters( 'the_content' ); |
| 68 |
|
| 69 |
// Set the page content. |
| 70 |
add_filter( 'the_content', function() use ( $checkout_id ) { |
| 71 |
ob_Start(); |
| 72 |
echo Elementor::instance()->frontend->get_builder_content_for_display( (int) $checkout_id, true ); |
| 73 |
return ob_get_clean(); |
| 74 |
}, 5 ); |
| 75 |
|
| 76 |
// Set sellkit canvas templates as the page template. |
| 77 |
add_action( 'template_redirect', function() { |
| 78 |
sellkit()->load_files( [ |
| 79 |
'templates/canvas' |
| 80 |
] ); |
| 81 |
|
| 82 |
exit; |
| 83 |
} ); |
| 84 |
|
| 85 |
add_filter( 'sellkit_global_checkout_activated', function() { |
| 86 |
return true; |
| 87 |
} ); |
| 88 |
|
| 89 |
add_action( 'sellkit_checkout_required_hidden_fields', function() use ( $checkout_id ) { |
| 90 |
?> |
| 91 |
<input type="hidden" name="sellkit_current_page_id" value="<?php echo esc_attr( $checkout_id ); ?>" > |
| 92 |
<input type="hidden" name="sellkit_global_checkout_id" value="<?php echo esc_attr( $checkout_id ); ?>" > |
| 93 |
<?php |
| 94 |
} ); |
| 95 |
|
| 96 |
if ( ! empty( $bump_data ) ) { |
| 97 |
set_query_var( 'bump_data', $bump_data ); |
| 98 |
} |
| 99 |
|
| 100 |
if ( ! empty( $optimization_data ) && CheckoutStep::apply_coupon_validation( $optimization_data ) ) { |
| 101 |
foreach ( $optimization_data['auto_apply_coupons'] as $auto_apply_coupon ) { |
| 102 |
wc()->cart->add_discount( get_the_title( $auto_apply_coupon['value'] ) ); |
| 103 |
} |
| 104 |
|
| 105 |
wc_clear_notices(); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
new Checkout(); |
| 111 |
|