settings = get_option( 'woocommerce_payplug_settings', [] ); if ( empty( $this->settings ) || ! isset( $this->settings['enabled'] ) || 'yes' !== $this->settings['enabled'] ) { return; } // Don't load for change payment method page. if ( isset( $_GET['change_payment_method'] ) ) { return; } add_action( 'template_redirect', [ $this, 'set_session' ] ); add_action( 'wc_ajax_payplug_create_order', [ $this, 'ajax_create_order' ] ); } /** * Sets the WC customer session if one is not set. * This is needed so nonces can be verified by AJAX Request. */ public function set_session() { if ( ! is_product() || ( isset( WC()->session ) && WC()->session->has_session() ) ) { return; } $session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' ); $wc_session = new $session_class(); if ( version_compare( WC_VERSION, '3.3', '>=' ) ) { $wc_session->init(); } $wc_session->set_customer_session_cookie( true ); } public function ajax_create_order() { if ( WC()->cart->is_empty() ) { wp_send_json_error( __( 'Empty cart', 'payplug' ) ); } if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { define( 'WOOCOMMERCE_CHECKOUT', true ); } WC()->checkout()->process_checkout(); die( 0 ); } }