WooCommerce Empty cart after payment with custom gateway API

心不动则不痛 提交于 2019-12-23 00:49:47

问题


I'm working on custom API for Merchant Safe Unipay (MSU) for woocommerce and need to change quantity after successful payment.

Here is process:

  1. Customer collect articles in shopping bag
  2. When click on "Pay All" it is redirected to MSU where need to fill Credit Card info
  3. After payment, MSU return him back to website where PHP send emails and print message about payment.

All works well but can't find hook where and how to mark all products from shopping card payed and change quantity.

How can I do that?

Thanks


回答1:


Normally after payment process, the customer is redirected to "Thank you" page (or "Order received" where customer can review his payed order)… Normally the cart is emptied somewhere (I don't remember where exactly).

So if not emptied, you need to do it for example with (2 different hooks options):

add_action( 'woocommerce_checkout_order_processed', 'order_received_empty_cart_action', 10, 1 );
// or 
// add_action( 'woocommerce_thankyou', 'order_received_empty_cart_action', 10, 1 );
function order_received_empty_cart_action( $order_id ){
    WC()->cart->empty_cart();
}

The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

You will have to test that, to see if it's convenient…




回答2:


With this code the payment is skipped. (Versión WC 3.5.7).

I include the code of class-wc-checkout.php lines 983 - 989:

   do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );

   if ( WC()->cart->needs_payment() ) {
       $this->process_order_payment( $order_id, $posted_data['payment_method'] );
   } else {
       $this->process_order_without_payment( $order_id );
   }

If we clean the cart, it takes the else route:

 $this->process_order_without_payment( $order_id );


来源:https://stackoverflow.com/questions/42819368/woocommerce-empty-cart-after-payment-with-custom-gateway-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!