WooCommerce Payment Gateway for Logged In users only - woocommerce

I would like logged in users to be able to use 'cheque' payment option.
I found the following relevant thread: WooCommerce Show Payment Gateways for Logged In Customers Only However, placing the following code in functions.php no longer works for Woocommerce version 2.3.7:
add_filter( "woocommerce_available_payment_gateways", "rp_filter_gateways", 9999 );
function rp_filter_gateways($args) {
if(!is_user_logged_in() && isset($args['cheque'])) {
unset($args['cheque']);
}
return $args;
}
Please can someone provide me with an updated solution?
Many thanks

woocommerce_available_payment_gateways is still a valid hook. The following code has been tested with the latest version of WooCommerce:
function rp_filter_gateways( $args ) {
if( !is_user_logged_in() && isset($args['cheque']) ) {
unset( $args['cheque'] );
}
return $args;
}
add_action( 'woocommerce_available_payment_gateways', 'rp_filter_gateways' );

Related

WordPress - woocommerce_process_shop_order_meta doesn´t work

I am trying to include a function after an order is updated (admin side), but it doesn´t work. I want to call a function when someone update an order (order status, etc...). I am trying this:
add_action( 'woocommerce_process_shop_order_meta', 'woocommerce_process_shop_order', 1, 1);
function woocommerce_process_shop_order () {
// Code
}
Ive been trying to redirect to other web with header("Location: www.example.com"), but the page doesn´t redirect when an order is update :(
Please, can you help me to solve it?
Thanks!
There you go. Tested and works. Goes into functions.php file of your child theme:
add_action( 'post_updated', 'post_updated_action', 20, 3 );
function post_updated_action( int $post_id, WP_Post $post_after, WP_Post $post_before ): void {
// To be sure it gets called when updated from the admin dashboard and is order
if ( $post_before->post_type !== 'shop_order' || ! is_admin() ) {
return;
}
wp_safe_redirect( home_url() );
exit;
}

how to make woocommerce payment for registered users

Requirement :
I have a custom requirement where anyone can enroll for a single course.
The tool I have to use is woocommerce with a custom woocommerce payment plugin.
These are the steps that a user will go through:-
Step 1: User registers
Step 2: The user is redirected to the payment page once payment is completed.
Step 3: User gets enrolled.
Now the issue is that woocommerce by default has payment for a virtual and physical product.
Please suggest a suitable method to implement this in woocommerce.
function control_payment( $args ) {
if( !is_user_logged_in() ) {
// unset your payment
}
return $args;
}
add_action( 'woocommerce_available_payment_gateways', 'control_payment' );
Or you can use something like this
add_filter( 'woocommerce_available_payment_gateways', 'my_paypal_disable_manager' );
function my_paypal_disable_manager( $available_gateways ) {
if ( isset( $available_gateways['paypal'] ) && current_user_can( 'manage_woocommerce' ) ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
(https://businessbloomer.com/disable-payment-gateway-specific-user-role-woocommerce/)

Woocommerce skip checkout for free products

I'm building webpage on WP and Woocommerce - I would like to skip cart and also checkout page for free products (or products which ID-s I can specify). These products are free and virtual (no payment needed, no shipping needed). The webpage is only used by registered users - so all the customer info is present.
The result I would like to have is that if you press ORDER button on product page - the order is done and customer is redirected to Thank-You page.
BR,
Kaspar
I applied the same concept, but found a major issue when processing order on the checkout; fields were still required.
The primary issue was processing the order via AJAX ( was using is_ajax() ), and even though it was on the checkout page, it wasn't returning as true. It's possible there was a recent change, or it could be the site's environment (theme).
Here are some of the conditional tags: https://docs.woocommerce.com/document/conditional-tags/
Seeing how things change, the answer can be edited here, but the original concept is located at: https://www.skyverge.com/blog/how-to-simplify-free-woocommerce-checkout/
function wc_free_checkout_fields() {
// Bail we're not at checkout, or if we're at checkout OR AJAX (payment process) but payment is needed.
if ( function_exists( 'is_checkout' ) && ( ! ( is_checkout() || is_ajax() ) || ( ( is_checkout() || is_ajax() ) && WC()->cart->needs_payment() ) ) ) {
return;
}
// Remove coupon forms since it's irrelevant with a free cart?
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
// Remove the "Additional Info" order notes.
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
// Unset the fields we don't want in a free checkout.
function wc_unset_unwanted_checkout_fields( $fields ) {
// Add or remove billing fields you do not want.
// #link http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2
$billing_keys = array(
'billing_company',
'billing_phone',
'billing_address_1',
'billing_address_2',
'billing_city',
'billing_postcode',
'billing_country',
'billing_state',
);
// For each unwanted billing key, unset.
foreach( $billing_keys as $key ) {
unset( $fields['billing'][$key] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'wc_unset_unwanted_checkout_fields' );
// A tiny CSS tweak for the account fields; this is optional.
function wc_print_custom_css() {
?>
<style>
.create-account {
margin-top: 6em;
}
</style>
<?php
}
add_action( 'wp_head', 'wc_print_custom_css' );
}
add_action( 'wp', 'wc_free_checkout_fields' );
Check if the checkout has no cost with the WC()->cart->needs_payment() check.
see this for more info:
https://www.skyverge.com/blog/how-to-simplify-free-woocommerce-checkout/

how to disable cart functionality from woocommerce?

How to disable cart functionality from woocommerce store. I want user can only see products available.customer can not purchase from store.
the easiest way is make the products not purchasable..
add_filter( 'woocommerce_is_purchasable','__return_false',10,2);
To fully disable woocommerce purchase functionality:
add_filter( 'woocommerce_is_purchasable', '__return_false'); // DISABLING PURCHASE FUNCTIONALITY AND REMOVING ADD TO CART BUTTON FROM NORMAL PRODUCTS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); // REMOVING PRICE FROM VARIATIONS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); // REMOVING ADD TO CART BUTTON FROM VARIATIONS
More option you can find here: https://react2wp.com/remove-hide-add-to-cart-button-in-woocommerce-while-disabling-keeping-purchase-functionality/
If you need conditions, you can use the following code:
function m3wc_woocommerce_is_purchasable( $is_purchasable, $product ) {
// Conditions here.
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'm3wc_woocommerce_is_purchasable', 10, 2 );
For example, if you need to check users:
// Disable purchase for non-logged-in users.
function m3wc_woocommerce_is_purchasable( $is_purchasable, $product ) {
if ( ! is_user_logged_in() ) {
return false;
}
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'm3wc_woocommerce_is_purchasable', 10, 2 );

WooCommerce - disable postcode validation

Does anybody know how to disable the Postcode validation on the checkout page in WooCommerce?
My country is set up as Switzerland, but I want also people from Austria and Germany allow to order.
So when I enter a German Postcode with 5 digits (in Switzerland there are only 4), the shop says it's an invalid postcode. (but in the settings I allowed every country).
Any idea how to fix that?
Adding this code to the functions.php file should work:
function custom_override_default_address_fields( $address_fields )
{
unset( $address_fields['postcode'] );
return $address_fields;
}
EDIT:
// Hook into the checkout fields (shipping & billing)
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Hook into the default fields
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_checkout_fields( $fields )
{
unset( $fields['billing']['billing_postcode'] );
unset( $fields['shipping']['shipping_postcode'] );
return $fields;
}
function custom_override_default_address_fields( $address_fields )
{
unset( $address_fields['postcode'] );
return $address_fields;
}
ANOTHER EDIT:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $address_fields )
{
$address_fields['postcode']['required'] = false;
return $address_fields;
}
So I didn't actually find a simple code solution for this one but I noticed that if I set
WooCommerce > Preferences > General > Geolocate address
it will work (if settings are set to "Sell to all countries", in my case)
This code only removes validation of address fields in my-account page, what you need:
add_filter( 'woocommerce_default_address_fields',
'custom_override_default_address_fields' );
function custom_override_default_address_fields($address_fields)
{
$address_fields['postcode']['validate'] = false;
return $address_fields;
}
for billing and shipping:
add_filter( 'woocommerce_checkout_fields' , 'remove_postcode_validation', 99 );
function remove_postcode_validation( $fields ) {
unset($fields['billing']['billing_postcode']['validate']);
unset($fields['shipping']['shipping_postcode']['validate']);
return $fields;
}
Also i think with removing "validate-required" class in wc-template-function.php, this feature will be deactivated (no test).
Sorry for bad English and hope this solutions solve your problem.
The previous answers don't seem to address the question! The postcode is still a required field, it's matter of whether other postcodes can be allowed that WooCommerce is saying is wrong.
For a checkout I'm building I've used this filter to allow any postcode to be considered valid regardless of country/postcode given.
add_filter( 'woocommerce_validate_postcode' , 'mycode_override_postcode_check', 99, 3 );
function mycode_override_postcode_check( $valid, $postcode, $country ) {
return true;
}
You can change that return true; for more complex code logic that reviews the postcode and country, this can help if your version of WooCommerce doesn't cover new postcode/zip code rules and you need them to be accepted.
Source: https://github.com/EloxZ/wordpresscrm/blob/main/wp-content/plugins/woocommerce/includes/class-wc-validation.php#L123

Resources