Authorize.net AIM Can't find the payment gateway woocommerce - woocommerce

Just like it says I want to shut off the payment gateway for Authorize.net AIM if no shipping is found. My shipping is flat-rate:25. I was trying something like this...
function my_custom_available_payment_gateways( $gateways ) {
$chosen_shipping_rates = WC()->session->get( 'chosen_shipping_methods' );
// When 'local delivery' has been chosen as shipping rate
if ( in_array( 'flat-rate:25', $chosen_shipping_rates ) ) :
// Remove bank transfer payment gateway
unset( $gateways['authorize'] );
endif;
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'my_custom_available_payment_gateways' );
* Enable payment gateways for shipping methods
* Filter payment gatways
*/
function my_custom_available_payment_gateways( $gateways ) {
$chosen_shipping_rates = WC()->session->get( 'chosen_shipping_methods' );
// When 'local delivery' has been chosen as shipping rate
if ( ! in_array( 'local_delivery', $chosen_shipping_rates ) ) :
// Remove bank transfer payment gateway
unset( $gateways['authorize'] );
endif;
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'my_custom_available_payment_gateways' );
But it doesn't work.

Related

hide payment gateway on specific shipping method

I have a shipping method which is called " SMSA EXPRESS - CASH ON DELIVERY " .. so when customer chooses this shipping method he should see the cash on delivery option for the payment and not the other options, which are the payments gateways ..
I tried to use this code but it didn't work
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_gateway_disable_shipping_326' );
function bbloomer_gateway_disable_shipping_326( $available_gateways ) {
if ( ! is_admin() ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'SMSA_EXPRESS_-_CASH_ON_DELIVERY' ) ) {
unset( $available_gateways['cod'] );
}
}
return $available_gateways;
}

Add Extra charges in shipping woocommerce

using XPS for Shipping rates. How can I add handling charges in shipping.
like if the shipping charges rates $39.52, it should add 3$ in shipping rates ( like 42.52 ) even we can code in function.php.
How do I accomplish this?
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_handling' );
function woocommerce_custom_handling() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$shipping_method = WC()->session->get( 'chosen_shipping_methods' );
if($shipping_method[0]){
$handling = 3.00;
$woocommerce->cart->add_fee('Handling', $handling, true, 'standard');
}
}
To add directly to shipping costs: Since I don't know the shipping key for XPS this is my best guess to add it to all shipping methods, including XPS. See if this works
add_filter( 'woocommerce_package_rates','woocommerce_custom_handling' );
function woocommerce_custom_handling($rates, $package) {
$handling = 3.00;
foreach( $rates as $rate_key => $rate ){
$rates[$rate_key]->cost = ($handling + $rates[$rate_key]->cost);
}
return $rates;
}

Hide shipping and payment methods in WooCommerce

I'm building a WooCommerce e-shop and I need to tweak my checkout page by doing the following:
Hide a certain shipping method (only one) if the order total > 100€.
Hide the cash on delivery payment method if local pickup is selected.
Does anyone know how to do that? I have the Code Snippets plugin so I can easily add any custom code.
To hide specific shipping method based on the cart total, you can use below code snippet. You need to update your shipping method name in the code.
Disable shipping method as per cart total
Add this snippet in your theme's functions.php file or custom plugin file.
add_filter( 'woocommerce_package_rates', 'shipping_based_on_price', 10, 2 );
function shipping_based_on_price( $rates, $package ) {
$total = WC()->cart->cart_contents_total;
//echo $total;
if ( $total > 100 ) {
unset( $rates['local_delivery'] ); // Unset your shipping method
}
return $rates;
}
Disable Payment Gateway For Specific Shipping Method
Use below code snippet. Update code as per your payment method & shipping method.
add_filter( 'woocommerce_available_payment_gateways', 'x34fg_gateway_disable_shipping' );
function x34fg_gateway_disable_shipping( $available_gateways ) {
global $woocommerce;
if ( !is_admin() ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
unset( $available_gateways['cod'] );
}
}
return $available_gateways;
}
There are a number of plugins that will do this for you, take a look at this one WooCommerce Conditional Shipping and Payments
You'll want to tie in to the "woocommerce_payment_gateways" action
Something along these lines:
function alter_payment_gateways( $gateways ){
$chosen_rates = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();
if( in_array( 'local-pickup:6', $chosen_rates ) ) {
$array_diff = array('cod');
$list = array_diff( $list, $array_diff );
}
return $list;
}
add_action('woocommerce_payment_gateways', 'alter_payment_gateways', 50, 1);
The number on the end of 'local-pickup' on line 4 will depend on your woocommerce setup. You can find the string you need to put in here by adding something to a basket, going to the checkout, right clicking on the "Local Pickup" option in the delivery methods and looking at the value attribute.

Woocommerce - change shipping cost via hooks (functions.php)

I'm using Woocommerce Subscriptions with Memberships and I would like to apply some discounts on the shipping cost according to the membership plan my user have. For example, silver members have a discount of -30% of all shipping method.
To check the current membership plan i use :
$user_id = get_current_user_id();// Get current user ID
if ( wc_memberships_is_user_active_member( $user_id, 'silver' ) ) {
//echo 'This is a special message only for Silver members!';
} else {
//echo 'I\'m sorry, you are not a silver member. There\'s nothing here for you!';
}
But I don't find any way to change the shipping cost with functions..
Is it possible ? Thanks
I have written some samples here: http://reigelgallarde.me/programming/woocommerce-shipping-fee-changes-condition-met/
add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates' );
function woocommerce_package_rates( $rates ) {
$user_id = get_current_user_id();
if ( ! wc_memberships_is_user_active_member( $user_id, 'silver' ) ) { return $rates; }
$discount_amount = 30; // 30%
foreach($rates as $key => $rate ) {
$rates[$key]->cost = $rates[$key]->cost - ( $rates[$key]->cost * ( $discount_amount/100 ) );
}
return $rates;
}
Above code will do what you want for all shipping methods.

How to Hide Payment Method in Woocommerce based on Postal Code

In this woocommerce setup, I have 2 Payment methods, Paypal and Cash on Delivery.
Now how can Cash on Delivery be hidden/disabled for certain Postal codes only.
This is the code I found on Gist
// Disable gateway based on country
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['ccavenue'] ) && $woocommerce->customer->get_country() <> 'IN' ) {
unset( $available_gateways['ccavenue'] );
} else if ( isset( $available_gateways['paypal'] ) && $woocommerce->customer->get_country() == 'IN' ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
Gist Link
To disable/hidden "Cash on Delivery", Place this code in your theme's function.php .
For more detail: woocommerce-hide-payment-gatway-based-on-visitors-country
// Disable gateway based on country
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_country() <> 'IN' ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
In the "checkout page" user can have two addresses - billing and shipping one.
To work correctly only with changes of Shipping one if it's filled I changed a bit the code. You have to test shipping countrycode if it's set, if not just user countrycode:
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
$country = !empty($woocommerce->customer->get_shipping_country()) ? $woocommerce->customer->get_shipping_country() : $woocommerce->customer->get_country();
if ( isset( $available_gateways['cod'] ) && $country <> 'CZ' ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
In the code above you used country code to disable payment gateway by, but you mentioned you would like to do it by Postal Code.
You're right about using woocommerce_available_payment_gateways, but instead of using $woocommerce->customer->get_country() you have to use WC()->customer->get_shipping_postcode() (or WC()->customer->get_billing_postcode() for some situations).
You mentioned PayPal and Cash on Delivery payment gateway, we need their IDs, there are paypal and cod accordingly.
In the code below let's deactivate Cash on Delivery for a couple postal codes, for example '1234' and '5678':
add_filter( 'woocommerce_available_payment_gateways', function( $available_gateways ) {
// if Cash on Delivery is already disabled, let's exit the function
if( empty( $available_gateways['cod'] ) ) {
return $available_gateways['cod'];
}
// get postal code
$postal_code = WC()->customer->get_billing_postcode();
// deactivate payment method
if( in_array( $postal_code, array( '1234', '5678' ) ) ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
} );
Code can be inserted to your current theme functions.php file or a custom plugin. More info you can find in this tutorial: https://rudrastyh.com/woocommerce/hide-payment-methods-based-on-postal-code.html

Resources