Removing payment gateways from WooCommerce - wordpress

I have a WooCommerce shop (running local) but I want to remove the payment gateways. The customer should be able to place an order without paying any cent, I will send them an invoice manually.
I can not really find where to disable this, it seems not to be standard in WooCommerce.
Have tried disabling all the payment gateways in the backend, but you have to leave one payment gateway enabled.
Thanks in advance!

Just add this line in functions.php in your theme:
add_filter('woocommerce_cart_needs_payment', '__return_false');

Leave 'Cash on Delivery' enabled, and it won't take a payment at the checkout. You can easily change the 'Cash on Delivery' titles and labels to something like 'No Payment Required' or similar.

Something that the other answers to this question haven't addressed is the fact that you need a way for the customer to eventually pay the invoice. Using Cash on Delivery (renamed to suit your needs) perfectly accomplishes not having the user actually pay at checkout, but the problem is that if Cash on Delivery was your only payment method, it will still be the only payment method when you send them the invoice.
I think in most cases you're going to want only cash on delivery during the cart checkout, and a different payment method (like Stripe) for the invoice payment method.
Here's the full workflow to create a deferred payment setup.
Like #crdunst mentions, you should use Cash on Delivery and rename
it to "Wait for Invoice" or something.
Enable all of the payment gateways that you ever want to use (in this example, we'll just use Cash on Delivery and Stripe. Cash on Delivery will be our "checkout" payment gateway, and Stripe will be our invoice payment gateway.
Use the following filter to turn on and off gateways based on whether or not you're on the order-pay endpoint (the page used for invoice payments).
/**
* Only show Cash on Delivery for checkout, and only Stripe for order-pay
*
* #param array $available_gateways an array of the enabled gateways
* #return array the processed array of enabled gateways
*/
function so1809762_set_gateways_by_context($available_gateways) {
global $woocommerce;
$endpoint = $woocommerce->query->get_current_endpoint();
if ($endpoint == 'order-pay') {
unset($available_gateways['cod']);
} else {
unset($available_gateways['stripe']);
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'so1809762_set_gateways_by_context');
Of course, if you're using a gateway other than stripe for the order-pay page, you'll want to make sure that you update unset($available_gateways['stripe']); to the proper array key.
After that, you should be good to go! Your site will now display different gateways based on whether or not you're on the invoice pay page!

Other option would be using BACS payment method, where you could explain the client that he will be invoiced later.
You can even add some info at the email that is sent when BACS is used.

Related

processing payment for woocommerce order

I'm looking for some PHP logic code to process the payment for woocommerce order. My current logic is failling.
My scenario is customer places an online order on woocommerce enabled website by selecting braintree credit card payment method, enters credit card info and submits the order. The store receives the order until this stage, card payment is not authorized. Order may get changes as per their customers changes request or stores inability to process certain items from the order which is removed. Once order is finalized after changes, order is confirmed that is when I want to process the payment, capture the payment and settle it down. I have php code to process the payment, capture the payment amount and settle it down. But when I process the payment order is failing which I suspect due to lack of credit card information. How can I achieve the functionality?. I don't want to take customer to payment page and ask him or her to enter credit card info again.
NOTE: Woocommerce documentation states "Finally, you need to add payment code inside your process_payment( $order_id ) method. This takes the posted form data and attempts payment directly via the payment provider." (here the issue is how or where to find payment code?
Error message:Braintree (Credit Card) Payment Failed (Status code 91577, 81709, 81714, 81725, 81703: Merchant account does not support payment instrument., Expiration date is required., Credit card number is required., Credit card must include number, paymentMethodNonce, or venmoSdkPaymentMethodCode., Credit card type is not accepted by this merchant account.) Order status changed from On hold to Failed. (credit card info was entered during checkout)
Here is the code that actually failing
add_action( 'woocommerce_order_status_processing_to_on-hold', 'process_credit_card_authorize', 10, 2 );
function process_credit_card_authorize( $order_id, $order)
{
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
$result = $available_gateways[ 'braintree_credit_card' ]->process_payment( $order_id );
if ( $result['result'] == 'success' )
{
$result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id );
wp_redirect( $result['redirect'] );
exit;
}
}

Woocommerce Stripe - hide shipping methods in google and apple pay

I need to hide in google pay and apple pay certain delivery methods in plugin (https://wordpress.org/plugins/woocommerce-gateway-stripe/) Can I do it with a filter or something?
Some shipping methods i have can't use - they're unsupported with apple and google pay.
Support of WC Stripe plugin told me:"Google and Apple Pay uses whichever shipping method is available on the site for the Stripe payment gateway. There isn’t an inbuilt option to selectively hide certain shipping method from Google and Apple pay, and we’re not aware of a workaround as well. I recommend taking a lot at a similar discussion here (related to disabling shipping method based on the payment gateway)" but I don't know how I do that.
–
Thanks for help.
The answer of the above mentioned question is "Yes". We can hide the shipping methods only in Apple Pay Using Stripe. Currently, there is no settings both in Woo-commerce and Stripe to select the specific shipping methods. But you can achieve that functionality by adding custom code in the right file and function. Please follow all the steps:
Step1: The file url for Stripe is listed below:
woocommerce-gateway-stripe/includes/payment-methods/class-wc-stripe-payment-request.php
Step2: Look for the following function in the file
public function get_shipping_options( $shipping_address, $itemized_display_items = false )
Step3: Remember that "STRIPE" by default uses the first Shipping method by default. So you need to check in that particular function, where that thing is happening.
Step4: Right before that specific IF condition --
"if ( isset( $data['shipping_options'][0] ) )"
Step5: Insert the Following Code before that IF condition
foreach($data['shipping_options'] as $index => $myshipping){
if(strpos($myshipping['label'],'Select Future Date') !== false){
unset($data['shipping_options'][$index]);
$data['shipping_options']= array_values($data['shipping_options']);
}
}
Note: You can do by ID or any other element in an Array.
You just need to match the shipping method ID and then Unset that ID. Please don't forget to update the array by using array_values() function.

WooCommerce Subscriptions - Action on initial payment

I am using the WooCommerce subscriptions plugin, in particular the woocommerce_subscription_payment_complete function.
I am using it like this:
add_action('woocommerce_subscription_payment_complete','subscription_created');
function subscription_created($subscription) {
echo 'Run when subscription payment is complete';
}
This works, but it also fires when a renewal payment completes. Does anybody know of a way to determine if the payment was for an initial subscription payment rather than a renewal?
You could use woocommerce_checkout_subscription_created, however the problem here is that it will fire before payment is processed - and I'm assuming you need to fire your even after payment has been successful.
One way to approach this is to set meta on the subscription post, that denotes whether your custom function has been run, and checking that meta with an if statement like this:
add_action('woocommerce_subscription_payment_complete','subscription_created');
function subscription_created($subscription) {
//check if meta exists/is not true
if (!get_post_meta($subscription->id, 'has_my_function_run', true)) {
//update meta to bool(true)
update_post_meta($subscription->id, 'has_my_function_run', true);
//run your function
echo 'Run when subscription payment is complete';
}
}
I am sure there is a better way to approach this though, so keep an eye out for other answers. It might be a good idea to look into hooking into woocommerce_order_status_processing, checking if it contains a subscription product, and then running your function, but that won't work if WooCommerce generates a new order for every subscription renewal.

woocommerce checkout update shipping value more than once

Woocommerce allows the use of the code below to update the shipping cost.
$('body').trigger('update_checkout', { update_shipping_method: true });
Am using a custom shipping plugin and am able to update the cost through ajax and eventually update my total.
The problem is, the update_checkout can only work when the billing_address_1, billing_city, shipping_city and a few other fields have been changed. So I have to do something like below:
$("#billing_address_1").trigger("keypress").val(function(i,val){return val + ' -';});
$('body').trigger('update_checkout', { update_shipping_method: true });
Is there a better way to achieve this, other than make the form dirty for woocommerce to update the shipping cost?
Thanks in advance!!
This by design of woocommerce. This scripts assumes that update is needed when changing address or country.
jQuery('body').trigger('update_checkout');
/* what this does is update the order review table but what it doesn't do is update shipping costs;
the calculate_shipping function of your shipping class will not be called again;
so if you were like me and you made a shipping method plugin and you had to change costs based on payment method then
this is the only way to ensure it does just that
*/
If you want to make things work add this (to plugin file or functions.php):
function action_woocommerce_checkout_update_order_review($array, $int)
{
WC()->cart->calculate_shipping();
return;
}
add_action('woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 2);
Quotation from: https://gist.github.com/neamtua/bfdc4521f5a1582f5ad169646f42fcdf
For reson why, read this ticet:
https://github.com/woocommerce/woocommerce/issues/12349

WooCommerce error when removing billing fields: "Please enter an address to continue."

I've removed billing fields on the WooCommerce checkout page by adding the following lines to functions.php:
add_filter("woocommerce_checkout_fields", "remove_billing_fields");
function remove_billing_fields($fields) {
unset($fields["billing"]["billing_first_name"]);
unset($fields["billing"]["billing_last_name"]);
unset($fields["billing"]["billing_company"]);
unset($fields["billing"]["billing_address_1"]);
unset($fields["billing"]["billing_address_2"]);
unset($fields["billing"]["billing_city"]);
unset($fields["billing"]["billing_postcode"]);
unset($fields["billing"]["billing_country"]);
unset($fields["billing"]["billing_state"]);
unset($fields["billing"]["billing_email"]);
unset($fields["billing"]["billing_phone"]);
return $fields;
}
This does remove the billing fields and leaves the shipping fields in-tact, as desired. However, now I get an error on checkout:
Please enter an address to continue.
However, all shipping fields are filled out. The request is being sent via AJAX (/shop/checkout?wc-ajax=checkout). Upon inspecting the request, I see the following fields are being sent:
billing_email:john#example.com
shipping_first_name:John
shipping_last_name:Doe
shipping_address_1:123 Easy St
shipping_address_2:
shipping_country:US
shipping_state:NY
shipping_city:New York
shipping_postcode:12345
billing_phone:123-456-7890
payment_method:stripe
wc-stripe-payment-token:abc123
_wpnonce:abc123
_wp_http_referer:/shop/checkout
Note that the request does go through when billing fields are set, so I believe everything else is set up correctly. Any ideas why this error is being thrown?
I found that although I can hide the billing_country field, it is required in order to calculate taxes and shipping, even if WooCommerce is configured to use the shipping fields by default.

Resources