Currently I'm working on a WC-Project and I'm stucked. With this snippet it's showing the payment method "bacs" only when the subtotal is over 100. This works fine!
add_filter( 'woocommerce_available_payment_gateways', 'mmx_remove_bacs', 1 );
function mmx_remove_bacs( $gateways ){
global $woocommerce;
if ( $woocommerce->cart->subtotal < 100 ) {
unset($gateways['bacs']);
}
return $gateways;
}
Now this snippet above should only work when a customer is in the Checkout that has the userrole "customer". For this I have this seperate snippet:
Function enable_payment( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['bacs'] ) && !current_user_can('customer') ) {
unset( $available_gateways['bacs'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'enable_payment' );
My question now is, how can I bring this to snippets together, that it works as I wish?
Related
we are using WooCommerce Subscription and we want, that after a subscription is added to the cart redirect the customer to the checkout page. In the past I think there was a setting to set for this but I'm not able to find it. However, now I try to do it via code.
We use AJAX in order to add to the cart. But I think this will not work. And I have to disable ajax add to cart only for some products? Like check for a category?
So I have to disable ajax add to cart only for specific products and then redirect the customer when adding such a product to the cart to checkout. The code below works if I deactivate ajax add to cart for all products and also all product will redirect to the checkout. However, we only need it for specific products.
// redirect customer to checkout page
add_filter( 'woocommerce_add_to_cart_redirect', 'custom_skip_cart_redirect_checkout' );
function custom_skip_cart_redirect_checkout( $url ) {
return wc_get_checkout_url();
}
// Fix for “Sold Individually” Products
add_filter( 'woocommerce_product_add_to_cart_url', 'custom_fix_for_individual_products', 10, 2 );
function custom_fix_for_individual_products( $add_to_cart_url, $product ){
if( $product->get_sold_individually() // if individual product
&& WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->id ) ) // if in the cart
&& $product->is_purchasable() // we also need these two conditions
&& $product->is_in_stock() ) {
$add_to_cart_url = wc_get_checkout_url();
}
return $add_to_cart_url;
}
// Remove “The product has been added to your cart” message
add_filter( 'wc_add_to_cart_message_html', 'custom_remove_add_to_cart_message' );
function custom_remove_add_to_cart_message( $message ){
return '';
}
Maybe it could help:
add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_to_checkout_if_product_is_subscription', 99, 1 );
function redirect_to_checkout_if_product_is_subscription( $url ) {
if ( ! isset( $_REQUEST['add-to-cart'] ) ) {
return $url;
}
$product_id = intval( $_REQUEST['add-to-cart'] );
if ( $product_id <= 0 ) {
return $url;
}
$product = wc_get_product( $product_id );
if ( WC_Subscriptions_Product::is_subscription( $product ) ) {
$url = wc_get_checkout_url();
}
return $url;
}
Please Guys,
I use two payment gateways on the checkout page:
1° bacs = Bank transfer
2° cod = Cash on Delivery
i need to hide the payment gateway COD = Cash on Delivery, If the user has No Admin Profile (!is_user_admin()), || or it's not logged, || ! is_user_logged_in() then hide.
This is the code that i'm using and it's working.
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_cod_hide' );
function bbloomer_cod_hide( $available_gateways ) {
if ( isset( $available_gateways['cod']) && !is_user_admin() ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
The problem is...the payment gateway bacs = Bank transfer is also affected and hidden : )
so i tried with this another hook
add_filter( 'woocommerce_available_payment_gateways', 'transfer_enable_bacs' );
function transfer_enable_bacs( $available_gateways ) {
if ( isset( $available_gateways['bacs']) && !is_user_admin() ) {
//unset( $available_gateways['bacs'] );
}
return $available_gateways;
}
But this is not the correct solution. Both payment gateways are hidden.
What am i doing wrong please?
Gratitude!
I think you need to replace is_user_admin with current_user_can
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_cod_hide' );
function bbloomer_cod_hide( $available_gateways ) {
if ( isset( $available_gateways['cod'] ) && ! current_user_can( 'administrator' ) ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
is_user_admin does not check if the user is an administrator; use current_user_can() for checking roles and capabilities.
https://developer.wordpress.org/reference/functions/is_user_admin/
try the following code:
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_cod_hide' );
function bbloomer_cod_hide( $available_gateways ) {
if ( !is_user_logged_in() ) || !current_user_can( 'install_themes' ) ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
In Woocommerce I am showing Cash on delivery payment method only for a particular city using the following code:
function payment_gateway_disable_city( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_shipping_city() == 'New York') {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_city' );
It works fine. Now I need to handle multiple cities like Washington, San Francisco etc...
So I tried the following:
function payment_gateway_disable_city( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_shipping_city() == 'New York', 'San Fransisco' ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_city' );
But it doesn't work… I get "WordPress experiencing technical difficulties".
Any help is appreciated.
Updated
The following code will enable COD payments for specific defined cities.
You need to use an array of allowed cities with in_array() PHP conditional function like:
add_filter( 'woocommerce_available_payment_gateways', 'cities_based_payment_gateway_cod' );
function cities_based_payment_gateway_cod( $available_gateways ) {
if ( is_admin() ) return $available_gateways; // Only on frontend
// HERE define the allowed cities in this array
$cities = array( 'New York', 'San Francisco' );
if ( isset( $available_gateways['cod'] ) && ! in_array( WC()->customer->get_shipping_city(), $cities ) ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
This is the code I am using:
if (!is_admin()):
add_action( 'woocommerce_before_cart', 'apply_matched_coupons' );
//add_action('woocommerce_before_cart_table', 'apply_matched_coupons');
//add_action('woocommerce_before_checkout_form', 'apply_matched_coupons');
function apply_matched_coupons() {
global $woocommerce;
$coupon_code = 'somecodehere';
if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;
if ( $woocommerce->cart->cart_contents_total >= 1 ) {
$woocommerce->cart->add_discount( $coupon_code );
wc_print_notices();
}
}
endif;
The issue I am having is that when I go to the checkout page that the coupon still gets applied. It's not applied on the cart which is the desired result but I don't want it applied at all in this condition.
Any help?
Based on your explanation, it sounds like you should be using the woocommerce_add_to_cart hook, which is run when a product is successfully added to the cart. I also don't think you should be using is_admin(), since that just checks if you're on an admin page...not if the current user is an admin.
I would do something like the following:
add_action( 'woocommerce_add_to_cart', 'apply_matched_coupons' );
function apply_matched_coupons() {
// If the current user is a shop admin
if ( current_user_can( 'manage_woocommerce' ) ) return;
// If the user is on the cart or checkout page
if ( is_cart() || is_checkout() ) return;
$coupon_code = 'somecodehere';
if ( WC()->cart->has_discount( $coupon_code ) ) return;
WC()->cart->add_discount( $coupon_code );
}
I am trying to hide some payment options in Woocommerce in case of specific delivery option. I tried to put this to my functions.php but It´s not working and I don´t know why.
Can you help me please?
function payment_gateway_disable_country( $available_gateways, $available_methods )
{
global $woocommerce;
if ( isset( $available_methods['local_delivery'] ) ){
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
My research:
link 1
link 2
link 3
link 4
The available delivery methods do not get passed as a parameter in the filter woocommerce_available_payment_gateways - you need to load them in and check them.
The code below should remove the paypal payment option one if the user selects local delivery. If your checkout page is the AJAX based one then as the user changes the delivery method, the available payment options should also change.
function payment_gateway_disable_country($available_gateways) {
global $woocommerce;
$packages = $woocommerce->shipping->get_packages();
foreach ( $packages as $i => $package ) {
$chosen_method = isset( $woocommerce->session->chosen_shipping_methods[ $i ] ) ?
$woocommerce->session->chosen_shipping_methods[ $i ] : '';
if ('local_delivery' == $chosen_method) {
unset($available_gateways['paypal']);
break;
}
}
return $available_gateways;
}
add_filter(
'woocommerce_available_payment_gateways',
'payment_gateway_disable_country'
);
Let me know if you have any issues with the code; I did not have a chance to test it with woocommerce.
$available_methods will not be accessible inside your function. So first define it globally & access as global variable inside function, somewhat like this:
global $available_methods;
$available_methods = array( 'local_delivery' => 'yes' );
function payment_gateway_disable_country( $available_gateways )
{
global $woocommerce, $available_methods;
if ( isset( $available_methods['local_delivery'] ) ){
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );