Woocommerce Stripe - hide shipping methods in google and apple pay - wordpress

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.

Related

Wordpress ACF Google maps field API Key

I have a Wordpress project with Advanced Custom Fields Pro 5.3.5 plugin, where for one of my post types I set an address field using the Google Map field type. When I go to the admin page to add/edit a post this Google Maps field shows the map but in "For development purposes only" mode.
It's probably missing an API key. I'm trying to set it as described in their documentation:
// Method 1: Filter.
function my_acf_google_map_api( $api ){
$api['key'] = 'xxx';
return $api;
}
add_filter('acf/fields/google_map/api', 'my_acf_google_map_api');
// Method 2: Setting.
function my_acf_init() {
acf_update_setting('google_api_key', 'xxx');
}
add_action('acf/init', 'my_acf_init');
I tried both methods in the functions.php of my theme and none work. I made sure that the API Key I'm using is valid and it is, I can use it on Postman to make API keys without any problem.
I've seen similar questions here on StackOverflow and basically the answer is what I'm already trying. What else I'm missing?
Thanks for any help
I've just had similar issues and hopefully my solution/steps will help.
As mentioned in the comments by #evan multiple API's must be enabled in the Google Console otherwise you will get errors.
Read the Error/Console message(s)! It will point you to either Billing or which API is not available/active.
Troubleshooting
Must have Google Console Project hooked up to a billing account
Must enable multiple APIs
(optional) can restrict key to certain domain/IPs
and also restrict to certain API
For the admin to work I required Maps Javascript API, Places API and also Geolocation API to be 'Enabled'
With those enabled my Admin could lookup and correctly assign Map lat/lng fields to the ACF field and saved correctly.

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 shipping methods not working for logged-in users?

I have developed a custom build on woocommerce.
All is fine except for the checkout page.
The site ships to UK only, and i have Flat Rate shipping enabled.
However, on the checkout page there are two issues. The first is an invalid argument in a foreach loop:
Warning: Invalid argument supplied for foreach() in /home/****/public_html/wp-content/plugins/woocommerce/includes/class-wc-shipping.php on line 291
And the second problem is:
There doesn‘t seem to be any available shipping methods. Please double check your address, or contact us if you need any help.
I am bashing my head against a wall here. I have a shipping method enabled, so why is it continuing to tell me there is no method available?
All i want it to do there is simply list all the shipping methods i have enabled so the user can select one.
I also noticed that the shipping methods display when user is not logged in, but when they are logged in it shows Flat Rate - then a random price that isn't entered anywhere in admin.
To elaborate a little. This problem does not occur if i add a variable product to the art using the ?add-to-cart url, but as soon as i use the add_to_cart function it causes those errors.
woocommerce->cart->add_to_cart(522,1, 523,array("attribute_colour" => "colour","attribute_size" => "a3", "attribute_invoice-numbering" => "yes", "attribute_quantity-column" => "yes", "attribute_cc-type" => "duplicate"));
The above code works fine if it is used in a URL, but i want to call it via ajax with the function.
Go to woo commerce settings and add a shipping zone. See here.
On existing products and when adding new products always ensure u set the shipping location. See here
I had a similar problem, I got that message even if Shipping Zones and Shipping Methods were configured correctly. I expected just one flat rate, no choices available.
Doing more tests the bug disappeared when there were 2 or more methods available, it was weird. I found the issue in the theme template, the first control was a wrong:
if ( 1 < count( $available_methods ) ) :
instead of
if ( $available_methods ) :
Perhaps you have a bug in the theme template too. You can do a diff/compare between the original template (/wp-content/plugins/woocommerce/templates/cart/cart-shipping.php) and the custom file (/wp-content/themes/YOURTHEME/woocommerce/cart/cart-shipping.php)

Removing payment gateways from WooCommerce

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.

Resources