WooCommerce Empty cart after payment with custom gateway API - php

I'm working on custom API for Merchant Safe Unipay (MSU) for woocommerce and need to change quantity after successful payment.
Here is process:
Customer collect articles in shopping bag
When click on "Pay All" it is redirected to MSU where need to fill Credit Card info
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

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…

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 );

Related

Redirect customer to recently acquired product in woocommerce

I'm selling digital goods (videos) that are unlocked after you buy them on my WooCommerce shop.
What I'm trying to do is that after successful purchase completion, the customer is redirected to the recently acquired product. The shop is already configured for redirec them logged-in and knowing that they acquired the product.
There is no cart o quantity, so there is no chance you'll add more than one product.
How can this be possible to make? I want to skip the after checkout page and redirect the customer directly to product they just bought.
Thanks!
There you go:
/***
* Redirect to custom page when reaching thank you page
*/
add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_action' );
function woocommerce_thankyou_action( int $order_id ) {
$order = wc_get_order( $order_id );
$first_order_item = $order->get_items()[0];
$first_product_id = $first_order_item->get_product_id();
if ( ! $order->has_status( 'failed' ) ) {
wp_safe_redirect( get_permalink( $first_product_id ) );
exit;
}
}
In this example, I use the first order item as reference since you're only talking about one product and not multiple products.
This hook goes inside the functions.php file of your child theme.

Move cart.php and cart-totals.php to checkout page

To reduce the number of redirects on my site, i added the cart page on the same page as the checkout. and used this code to do it:
function cart_on_checkout_page_only() {
if ( is_wc_endpoint_url( 'order-received' ) ) return;
// add woocomerce cart shortcode on checkout page
echo do_shortcode('[woocommerce_cart]');
}
// Cart and Checkout same page
add_action( 'woocommerce_before_checkout_form', 'cart_on_checkout_page_only', 5 );
The problem is in the layout specifically, on devices it is too far from the order items as the order total is at the bottom of the page near the checkout button. I got a lot of complaints because people are too lazy to scroll down to see the coupon application in the cart at the bottom of the page
so I made the following modification:
I copied the woocommerce plugin files woocommerce/template/cart/cat.php and cart-totals.php
and pasted it into my theme in themes/mytheme/template/cart/custom-cart.php
and adapted it in the code above:
function cart_on_checkout_page_only() {
if ( is_wc_endpoint_url( 'order-received' ) ) return;
// add custom cart template on checkout page
get_template_part('template/cart/custom-cart');
}
// Cart and Checkout same page
add_action( 'woocommerce_before_checkout_form', 'cart_on_checkout_page_only', 5 );
my code did the job, but I believe there should be a simpler and more elegant way to make this kind of change. it's possible to make more simpler?
by the way I'm using a child storefront theme.

How to remove woocommerce added cart items and redirect to checkout?

I have a Woocommerce form to 'Add Funds'. It has an amount input field ($20, $30 ...etc.) and a submit button that redirects to cart page with the input amount as total.
Redirect to checkout is working, but the cart items are not getting removed if a user abandons the cart and tries to order again.
I tried numerous solutions for the redirect to checkout, but only one worked.
Working solution for redirect to checkout:
WooCommerce - Skip cart page redirecting to checkout page
Solutions not working for redirect to checkout:
https://wordpress.stackexchange.com/questions/267071/redirect-to-woocommerce-checkout-after-adding-to-cart-item-already-in-cart
Woocommerce add to cart button redirect to checkout
N.B. I have added the working and not working solutions for redirect
to checkout because it may provide an insight as to why the empty cart
solutions are not working.
Incase of emptying cart before adding a new product, none of the solutions are working:
https://gist.github.com/viniciusrtf/b49403b5f87dcd7699c1
https://hungred.com/how-to/empty-woocommerce-cart-adding-item/
https://wordpress.stackexchange.com/questions/267071/redirect-to-woocommerce-checkout-after-adding-to-cart-item-already-in-cart
Using Woocommerce 3.2.6 and WordPress 4.9.2
First you will need in WooCommerce Settings > Products > Display in "Add to cart behaviour" to enabled the checkbox : Redirect to the cart page after successful addition
Then you will need the 3 following hooked function:
1) Empty cart before add-to-cart (if cart is not empty)
add_filter( 'woocommerce_add_to_cart_validation', 'one_cart_item_at_the_time', 10, 3 );
function one_cart_item_at_the_time( $passed, $product_id, $quantity ) {
if( ! WC()->cart->is_empty())
WC()->cart->empty_cart();
return $passed;
}
2) Add-to-cart redirection to checkout:
add_filter( 'woocommerce_add_to_cart_redirect', 'add_to_cart_checkout_redirection', 10, 1 );
function add_to_cart_checkout_redirection( $url ) {
return wc_get_checkout_url();
}
3) Skip cart page redirecting to checkout:
add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');
function skip_cart_page_redirection_to_checkout() {
if( is_cart() )
wp_redirect( wc_get_checkout_url() );
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works (with Woocommerce 3.2.6 and WordPress 4.9.2 on Storefront theme).

Display a custom message for cheque payment method in WooCommerce thankyou

I am trying to modify the page thanks to buy at WooCommerce, at least for the method of payment by bank check.
All I want to do is show a summary of the order and empty the cart, because currently shows me an empty page with the text that the cart has been emptied. Is there a hook that allows me to insert code on the page thanks for paying by bank check?
I found the 'woocommerce_thankyou' action but I do not know what to do with it, does anyone explain to me a little?
you can target cheque payment method using woocommerce_thankyou_cheque action hook, that will display a custom message on order received page for cheque payments:
add_action( 'woocommerce_thankyou_cheque', 'woocommerce_thankyou_cheque_payment', 10, 1 );
function woocommerce_thankyou_cheque_payment( $order_id ){
if( ! $order_id ) return;
// SET your message below
echo '<p>'.__( 'Thanks for paying by cheque message.', 'woocommerce' ).'</p>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.

WooCommerce - Skip cart page redirecting to checkout page

Our website is kohsamuitour.net. I have added custom code to skip the cart page on checkout, which works for all sales. This code:
function wc_empty_cart_redirect_url() {
return 'https://www.kohsamuitour.net/all-tours/';
}
add_filter( 'woocommerce_return_to_shop_redirect', 'wc_empty_cart_redirect_url' );
Now that does the job, but we also have a possibility to check booking availability. That can be found on the pages of private charters, i.e. this one: https://www.kohsamuitour.net/tours/kia-ora-catamaran/ .
Here the customer is being redirected to the cart, where I don't want that to happen as this is not a sale.
How can I make sure the 'Check booking availability' is also redirected to the checkout straight away?
You can skip cart definitively, redirecting customers to checkout page when cart url is called.
To achieve this use this code snippet, that should do the trick:
// Function that skip cart redirecting to checkout
function skip_cart_page_redirection_to_checkout() {
// If is cart page, redirect checkout.
if( is_cart() )
wp_redirect( WC()->cart->get_checkout_url() );
}
add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and fully functional.
Edit: Since WooCommerce 3 replace wp_redirect( WC()->cart->get_checkout_url() ); by:
wp_redirect( wc_get_checkout_url() );

Categories