I started to work on small Woocommerce project. I have 3 payment gateways into this store: Paypal, Credit Card and Direct bank Transfer.
What I would like is: If coupon code is used, I would like to disable (or remove) Paypal and Credit Card from available payment gateways, and just keep "Direct bank Transfer" as available payment gateway choice.
To show how is look current state from checkout page:
I found a similar solution, but this is for removing gateway based on product category.
add_filter( 'woocommerce_available_payment_gateways', 'unset_payment_gateways_by_category' );
function unset_payment_gateways_by_category( $available_gateways ) {
global $woocommerce;
$unset = false;
$category_ids = array( 8, 37 );
foreach ( $woocommerce->cart->cart_contents as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ( $terms as $term ) {
if ( in_array( $term->term_id, $category_ids ) ) {
$unset = true;
break;
}
}
}
if ( $unset == true )
unset( $available_gateways['cheque'] );
return $available_gateways;
}
So I think that this function can be used, but slightly modified per my problem.
Any help is appreciated.
The following code will remove all payment gateways except "Direct bank Transfer" (bacs) only if at least one coupon code has been applied by the customer:
add_filter('woocommerce_available_payment_gateways', 'applied_coupons_hide_payment_gateways', 20, 1 );
function applied_coupons_hide_payment_gateways( $available_gateways){
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
// If at least a coupon is applied
if( sizeof( WC()->cart->get_applied_coupons() ) > 0 ){
// Loop through payment gateways
foreach ( $available_gateways as $gateway_id => $gateway ) {
// Remove all payment gateways except BACS (Bank Wire)
if( $gateway_id != 'bacs' )
unset($available_gateways[$gateway_id]);
}
}
return $available_gateways;
}
Code goes in function.php file of the active child theme (or active theme). Tested and works.
here you go :
add_filter('woocommerce_available_payment_gateways', 'unset_gatway_by_applied_coupons');
function unset_gatway_by_applied_coupons($available_gateways)
{
$coupons = WC()->cart->applied_coupons;
if (!empty($coupons)) {
unset($available_gateways['bacs']);
}
return $available_gateways;
}
what we did here we checked if any coupons is applied trough WC()->cart->applied_coupons; which will return an array of coupons if coupons array not empty remove specific payment gateway
if you want to check if certain coupon is applied and remove gatway based on your condition you can use the following:
add_filter('woocommerce_available_payment_gateways', 'unset_gatway_by_applied_coupons');
function unset_gatway_by_applied_coupons($available_gateways)
{
$coupons = WC()->cart->applied_coupons;
foreach ($coupons as $coupon) {
if ($coupon == 'my_coupon') { //here you can specific your coupon name
unset($available_gateways['bacs']);
}
}
return $available_gateways;
}
of course both functions are tested, you just need to place them in your functions.php
Related
I tried this code below to hide/disable Credit/Debit card and Direct bank transfer payment method on Woo commerce(WordPress) when the checkout total == 400 but did not work. Please any idea on how to achieve this? Thank you so kindly.
function payment_gateway_disable_total_amount( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['bacs'] ) && $woocommerce->cart->total == 400 ) {
unset( $available_gateways['bacs'] );
}
if ( isset( $available_gateways['youpay'] ) && $woocommerce->cart->total == 400 ) {
unset( $available_gateways['youpay'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_total_amount' );
Why using a fixed total? There is very few chances that any customer get speifically 400 as total. It should be "up to 400" instead, so something like if( $tolal >= 400 ).
Also "Debit/Credit Cards" doesn't seem to be the right Payment method Id… See [this thread][1] to find out the right Payment method Id for "Debit/Credit Cards" payment gateway.
Try the following (assuming that "Debit/Credit Cards" payment method id is correct):
add_filter( 'woocommerce_available_payment_gateways', 'show_hide_payment_methods' );
function show_hide_payment_methods( $available_gateways ) {
if ( WC()->cart->total >= 400 ) {
if ( isset($available_gateways['bacs']) ) {
unset($available_gateways['bacs']);
}
if ( isset($available_gateways['Debit/Credit Cards']) ) {
unset($available_gateways['Debit/Credit Cards']);
}
}
return $available_gateways;
}
Code goes in functions.php file of the active child theme (or active theme). It should works.
The code I have works for hiding the BACS payment gateway for guests and customers, but I need to change it so that the BACS gateway only becomes available IF the customer/admin apply a certain coupon code called FOOD on the CART or CHECKOUT.
In other words: hide the BACS gateway until the COUPON called FOOD is applied on the CART or CHECKOUT.
Here's the code that I have:
add_filter('woocommerce_available_payment_gateways', 'show_bacs_if_coupon_is_used', 99, 1);
function show_bacs_if_coupon_is_used( $available_gateways ) {
$current_user = wp_get_current_user();
if ( isset($available_gateways['bacs']) && (current_user_can('customer'))) {
unset($available_gateways['bacs']);
} else if ( isset($available_gateways['bacs']) && !is_user_logged_in()) {
unset($available_gateways['bacs']);
}
return $available_gateways;
}
Only show BACS payment method when a specific coupon is applied to cart for logged in users only (using WC_Cart get_applied_coupons() method):
add_filter('woocommerce_available_payment_gateways', 'show_bacs_for_specific_applied_coupon', 99, 1);
function show_bacs_for_specific_applied_coupon( $available_gateways ) {
if ( is_admin() ) return $available_gateways; // Only on frontend
$coupon_code = 'FOOD'; // <== Set here the coupon code
if ( isset($available_gateways['bacs']) && ! ( is_user_logged_in() &&
in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() ) ) ) {
unset($available_gateways['bacs']);
}
return $available_gateways;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
On Woocommerce, I have enabled 2 shipping methods: Free shipping or Flat rate. I have enabled 2 payment methods: Bank transfer (bacs) and PayPal (paypal).
What I want to achieve:
If a customer selects PayPal as payment type he should be forced to select "Flat rate" as shipping method. "Free shipping" should be either hidden or greyed out or something like that.
If bank transfer is chosen then both shipping methods should be available.
Any help is appreciated.
If anyone is interested, I found a solution:
function alter_payment_gateways( $list ){
// Retrieve chosen shipping options from all possible packages
$chosen_rates = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();
if( in_array( 'free_shipping:1', $chosen_rates ) ) {
$array_diff = array('WC_Gateway_Paypal');
$list = array_diff( $list, $array_diff );
}
return $list;
}
add_action('woocommerce_payment_gateways', 'alter_payment_gateways');
This code will deactivate PayPal if a customer selects free shipping.
Update 2: The following code will disable "free_shipping" shipping method (method ID) when "paypal" is the chosen payment method:
add_filter( 'woocommerce_package_rates', 'shipping_methods_based_on_chosen_payment', 100, 2 );
function shipping_methods_based_on_chosen_payment( $rates, $package ) {
// Checking if "paypal" is the chosen payment method
if ( WC()->session->get( 'chosen_payment_method' ) === 'paypal' ) {
// Loop through shipping methods rates
foreach( $rates as $rate_key => $rate ){
if ( 'free_shipping' === $rate->method_id ) {
unset($rates[$rate_key]); // Remove 'Free shipping'shipping method
}
}
}
return $rates;
}
// Enabling, disabling and refreshing session shipping methods data
add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods', 10, 1 );
function refresh_shipping_methods( $post_data ){
$bool = true;
if ( WC()->session->get('chosen_payment_method' ) ) $bool = false;
// Mandatory to make it work with shipping methods
foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){
WC()->session->set( 'shipping_for_package_' . $package_key, $bool );
}
WC()->cart->calculate_shipping();
}
// Jquery script for checkout page
add_action('wp_footer', 'refresh_checkout_on_payment_method_change' );
function refresh_checkout_on_payment_method_change() {
// Only checkout page
if( is_checkout() && ! is_wc_endpoint_url() ):
?>
<script type="text/javascript">
jQuery(function($){
// On shipping method change
$('form.checkout').on( 'change', 'input[name^="payment_method"]', function(){
$('body').trigger('update_checkout'); // Trigger Ajax checkout refresh
});
})
</script>
<?php
endif;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
To get the related shipping methods rate IDs, something like flat_rate:12, inspect with your browser code inspector each related radio button attribute name like:
Note: Since WooCommerce new versions changes, sorry, the code doesn't work anymore.
I'm need to hide paypal when there's any backordered item on cart or hide cod if there's not any item to be backordered. My problem here is if there's a item that's backorder together with one that is not, I end up whitout a payment processor
add_filter( 'woocommerce_available_payment_gateways', 'backordered_items_hide_cod', 90, 1 );
function backordered_items_hide_cod( $available_gateways ) {
// Only on front end
if ( is_admin() )
return;
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ){
if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
// Hide payment gateway
unset($available_gateways['paypal']);
} else {
unset($available_gateways['cod']);
break; // Stop the loop
}
}
return $available_gateways;
}
The following function will hide paypal for any backordered item found or if there is no backordered items it will hide COD instead:
add_filter( 'woocommerce_available_payment_gateways', 'backordered_items_hide_cod', 90, 1 );
function backordered_items_hide_cod( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
$has_a_backorder = false;
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ){
if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
$has_a_backorder = true;
break;
}
}
if( $has_a_backorder ) {
unset($available_gateways['paypal']);
} else {
unset($available_gateways['cod']);
}
return $available_gateways;
}
Code goes in functions.php file of your active child theme (active theme). Tested and works.
I am using on WooCommerce this little peace of code from this answer to autocomplete paid processing orders:
/**
* AUTO COMPLETE PAID ORDERS IN WOOCOMMERCE
*/
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
// No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
if ( ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cod' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cheque' ) ) {
return;
}
// "completed" updated status for paid Orders with all others payment methods
else {
$order->update_status( 'completed' );
}
}
But the problem is that I use a special payment gateway by SMS which API is bridged on 'cod' payment method, and the orders stay sometimes in on-hold status on this 'woocommerce_thankyou' hook.
So I will need to scan all the time the 'processing' orders to pass them in complete status. I have tried different things and hooks, but I cant get it work as expected.
How can I do this?
Thanks
To get this working you just need a little function that will scan all orders with a "processing" status on the 'init' hook, and that will update this status to "completed".
Here is that code:
function auto_update_orders_status_from_processing_to_completed(){
// Get all current "processing" customer orders
$processing_orders = wc_get_orders( $args = array(
'numberposts' => -1,
'post_status' => 'wc-processing',
) );
if(!empty($processing_orders))
foreach($processing_orders as $order)
$order->update_status( 'completed' );
}
add_action( 'init', 'auto_update_orders_status_from_processing_to_completed' );
This code is tested and works.
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
ADVICE & UPDATE
There is a little bug around email notifications sent twice that is solved in here:
Avoid repetitive emails notification on some auto completed orders
WooCommerce virtual orders can be automatically marked as ‘completed’ after payment with a little bit of code added to a custom plugin, or your themes functions.php file. By default WooCommerce will mark virtual-downloadable orders as ‘completed’ after successful payment, which makes sense, but some store owners will want to be able to automatically mark even a virtual order as complete upon payment, for instance in the case of a site which takes donations where no further action is required. To do so, use the following code, which is based on the core virtual-downloadable completed order status:
add_filter( 'woocommerce_payment_complete_order_status', 'virtual_order_payment_complete_order_status', 10, 2 );
function virtual_order_payment_complete_order_status( $order_status, $order_id ) {
$order = new WC_Order( $order_id );
if ( 'processing' == $order_status &&
( 'on-hold' == $order->status || 'pending' == $order->status || 'failed' == $order->status ) ) {
$virtual_order = null;
if ( count( $order->get_items() ) > 0 ) {
foreach( $order->get_items() as $item ) {
if ( 'line_item' == $item['type'] ) {
$_product = $order->get_product_from_item( $item );
if ( ! $_product->is_virtual() ) {
// once we've found one non-virtual product we know we're done, break out of the loop
$virtual_order = false;
break;
} else {
$virtual_order = true;
}
}
}
}
// virtual order, mark as completed
if ( $virtual_order ) {
return 'completed';
}
}
// non-virtual order, return original status
return $order_status;
}
OR
You can also use plugin for auto complete order
Here is the plugin URL : https://wordpress.org/plugins/woocommerce-autocomplete-order/screenshots/
Please let me know which is use full to you.
Thnaks.