duandanbeng1829 2019-04-26 12:05
浏览 52
已采纳

WooCommerce Extension - 重定向到'Pay'端点时适用的挂钩

I'm integrating a WooCommerce payment gateway plugin and have a requirement to embed the gateway in an iframe. I'm redirecting with the below code to the Pay (the /order-pay route by default) endpoint where I'd like to include the iframe where the payment process will continue.

Does anyone know of an applicable hook in which I load the necessary JS scripts to initialise the iframe on the order-pay template?

function process_payment( $order_id ) {
  global $woocommerce;
  $order = new WC_Order( $order_id );
  $order->update_status( 'pending-payment', __( 'Awaiting payment', 'some-domain' ) );

  return array(
    'result'   => 'success',
    'redirect' => $order->get_checkout_payment_url( $on_checkout = true )
  );
}
  • 写回答

1条回答 默认 最新

  • duan33360 2019-04-29 04:42
    关注

    I couldn't find an appropriate WooCommerce hook for the order-pay page. What I decided to do is add an action for the wp_enqueue_scripts and only load my scripts if the applicable route is active.

    In my plugin constructor:

    function __contstruct {
      // other stuff
      add_action( 'wp_enqueue_scripts', array( $this, 'load_my_order_pay_scripts' ) );
    }
    

    The function to load my scripts:

    public function load_my_order_pay_scripts() {
      if( !is_wc_endpoint_url( 'order-pay' ) ) return;
    
      global $wp;
      $order_id = $wp->query_vars['order-pay'];
      $order = wc_get_order( $order_id );
    
      wp_enqueue_script( 'my-js-sdk', 'http://my.cdn.com/my-js-sdk.js', true );
      wp_register_script( 'my-plugin-script', plugin_dir_url( __FILE__ ) . '/js/my-plugin-script.js', true );
      wp_localize_script( 'my-plugin-script', 'params', $this->script_parameters, true ); // I need to pass my script some data
      wp_enqueue_script( 'my-plugin-script', array('my-js-sdk'), true ); // my plugin's JS is dependent on my SDK
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?