WooCommerce payment complete hook

后端 未结 3 1007
忘了有多久
忘了有多久 2020-11-29 03:56

After a long search, I found this post:

WooCommerce hook for "after payment complete" actions

which talks about creating web hooks in WooCommerce t

3条回答
  •  执念已碎
    2020-11-29 04:40

    with the help from @helgatheviking and @Scriptonomy I settled on this code, with NO webhook enabled in woocommerce->settings->api->webhooks:

    add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
    function so_payment_complete( $order_id ){  
      $order = wc_get_order( $order_id );
      $billingEmail = $order->billing_email;
      $products = $order->get_items();
    
    foreach($products as $prod){
      $items[$prod['product_id']] = $prod['name'];
    }
    
    $url = 'http://requestb.in/15gbo981';
    // post to the request somehow
    wp_remote_post( $url, array(
     'method' => 'POST',
     'timeout' => 45,
     'redirection' => 5,
     'httpversion' => '1.0',
     'blocking' => true,
     'headers' => array(),
     'body' => array( 'billingemail' => $billingEmail, 'items' => $items ),
     'cookies' => array()
     )
    );
    

    Now I just have to write the listener :) This is the body of the request that gets sent (which I can see at requestb.in):

    billingemail=%22aaron-buyer%40thirdoptionmusic.com%22&items%5B78%5D=Cult+Of+Nice&items%5B126%5D=Still&items%5B125%5D=The+Monkey+Set
    

提交回复
热议问题