I'm creating a custom payment gateway for WooCommerce, but I'm struggling to display the settings in de admin area.
I've followed tutorials like:
https://docs.woocommerce.com/document/payment-gateway-api/
https://docs.woocommerce.com/document/settings-api/
https://rudrastyh.com/woocommerce/payment-gateway-plugin.html.
However in my case admin_options is just never called when I go to the settings page for my payment gateway.
The payment gateway is in the list of payment methods. It's also visible in the frontend as selectable payment method.
Inside my class that extends WC_Payment_Gateway I have put
public function admin_options() {
echo 'TEST';
die();
}
To check if it's called, but it isn't. Other methods like the constructor, init_form_fields are called, so at least some part is working.
Any ideas on what might be happening or how to tackle this are very welcome.
$this->id contained some uppercase letters....it seems that was not allowed...making it lower case resolved the issue.
I was triggered by the answer of #Vishal in this post:
WordPress Plugin WooCommerce, Custom Payment Gateway Settings Not Saving
Related
I've been struggling with this for a couple days now and have been through every post/comment/discussion/etc... I could find trying to find a working solution.
I want to send an email via a custom class that extends WC_Email whenever a woocommerce user updates their address. I've found various resources explaining how to create custom wc emails (skyverge was most useful) and I have successfully done that. I have a plugin that adds a custom email in WP-Admin->WooCommerce->Settings->Emails.
If I use an action that is already part of the woocommerce_email_actions such as add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ) ); and manually change the order status in the backend everything works just fine.
Problem is I want to use add_action( 'woocommerce_customer_save_address', array( $this, 'trigger' ) ); and unfortunately it never fires.
Based on some other threads I've tried adding the following to my main plugin file
function new_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_customer_save_address';
return $actions;
}
add_filter( 'woocommerce_email_actions', 'new_woocommerce_email_actions' );
Supposedly this should allow me to use the action in my custom class, but no luck. I've also tried adding other actions without any success. For instance using the filter to add woocommerce_order_status_cancelled won't fire when manually changing the order to cancelled. I'm struggling to figure out why this isn't working and most of the the threads I've found are 2+ years old and dead, so here I am. Any help or pointers would be greatly appreciated.
Posting as an answer in case others are trying to get this functionality working.
I found this article from Tyche Softwares after extensive digging and was able to create a plugin that will send a custom WC_Email whenever a customer updates their address information.
I'm still not 100% as to why the add_filter('woocommerce_email_actions'... wasn't working, and if anyone can tell me why I'd still be interested.
This plugin works by calling a custom action defined in my extended WC_Email class via do_action whenever the existing woocommerce_customer_save_address action happens.
I want to change the WooCommerce email header template with a new one so that i could add conditions in header template to get value from dashboard or database. (To change color of header based on user input from dashboard). Please note that i am using a custom plugin file to do so.
I have followed several tutorials and all i received was bunch of errors.
What i did is, i have a class with following code.
public function __construct(){
add_action('woocommerce_email',array($this,'woocommerce_email'));
}
Now I have added code to remove default reset the hook:
public function woocommerce_email($mailer){
remove_action('woocommerce_header',array($mailer,'email_header'));
add_action('woocommerce_header',array($this,'email_header'));
}
Now calling the template:
public function email_header() {
wc_get_template( 'emails/email-header.php');
}
I am not passing anything to the template file. So no parameters are passed to functions. I just wanted to see that my template is being taken.
Also i assume $mailer to be a part of WooCommerce class.
Any help would be appreciated.
Please note that this is a plugin functionality so I am not interested in replacing the WooCommerce email templates.
For those shipping methods, I would like to have:
"Free shipping" only for Cash-on-delivery (COD) payment method (I just renamed it to other label but I am using the COD gateway).
"Flat rate" for other payment gateways (excluding COD payment method of course).
My question: How do I make a specific payment gateway to be free shipping when selected?
For example like in this screenshot:
Similar unanswered question: Woocommerce free shipping based on payment gateway selected
WooCommerce requires that shipping is selected before a gateway, this is how COD works because it checks if an enabled shipping method is selected before providing COD as an option. So if the COD method does not work for you then there is no other way to accomplish this because you are asking the checkout process work backwards from how it was designed.
You can not modify shipping once a payment gateway has been selected due to the way WooCommerce works. You can only add extra fees within the code for each gateway.
After a while of thinking about it, I got curious and thought i'd have a play to see if it was truely impossible. It turns out that with a bit of crude hacking you can actually get this to work, here is a basic plugin that will accomplish the task:
<?php
/**
* Plugin Name: Free Shipping For BACS
* Description: Makes shipping for BACS free.
* Version: 0.0.1
* Author: Kodaloid
* Requires at least: 4.4
* Tested up to: 4.8
*/
if (!defined('ABSPATH')) exit;
add_action('init', 'fg_init');
function fg_init() {
add_action('woocommerce_cart_calculate_fees', 'fg_add_fee');
add_action('wp_footer', 'fg_footer', 9999);
}
function fg_footer() {
?>
<script type="text/javascript">
jQuery(function($) {
setInterval(function() {
$(".input-radio[name='payment_method']").off().change(function() {
console.log('triggered');
jQuery('body').trigger('update_checkout');
});
}, 500);
});
</script>
<?php
}
function fg_add_fee($the_cart) {
global $woocommerce;
if ($woocommerce->session->chosen_payment_method == 'bacs') {
$woocommerce->cart->add_fee('Free Shipping For BACS', -($the_cart->shipping_total), true, 'standard');
}
}
Save the code above as free_shipping_for_bacs.php and install the plugin using the Upload Plugin feature in WordPress.
Basically what this does is check the session to see which payment method has been picked, then if the bacs method is chosen adds a fee which is minus the total of the shipping. This works but because the cart updates using AJAX you need to trigger the update_checkout event attached to the body in JavaScript every time the payment method changes in order to see the change reflected in the cart above.
So as a hack I have added a loop that re-adds the change handler every 500ms to the footer event (if your theme does not implement wp_footer hook, make sure to add it), this can and should be improved upon if you decide to use this code as there are better methods to check if the change event needs re-adding, it's just I don't have a lot of time today.
Koda
I'm using Woocommerce Subscriptions and currently the below action hook fires when you update your address on your Account Page > Edit Shipping (my-account/edit-address/shipping/) - HOWEVER it does not fire when you update your address on the Account > View subscription page > Edit Shipping (/my-account/edit-address/shipping/?subscription=62400).
function kidstir_email_customer_address( $user_id ) {
// do stuff
}
add_action( 'woocommerce_customer_save_address','kidstir_email_customer_address', 20 );
I've been looking for hours, and can't figure out why it's not firing, or how to get a notification that a subscription address has changed. Has anyone else had this issue?
The version of WooCommerce Subscriptions (2.0.9) does not call the woocommerce_customer_save_address hook in file class-wc-subscriptions-addresses.php
To solve my problem I edited this file (I know editing core is not advisable) to call the hook (doaction etc) on line 137.
According to official documentation of the WooCommerce Subscriptions plugin, there is a updated_users_subscriptions hook.
I noticed that Woocommerce's reviews are managed through Wordpress Comments. But why is it that Wordpress isn't notifying my email when someone posted a review to a product. I have set the "Email me whenever anyone posted a comment".
Is this function available in Woocommerce or i'm missing something?
Please advise, thanks everyone!
Regards, Ven
By default Wordpress sends a notification to the author of the product/post (the person who created the product in your instance). The suggested site owner (settings > general) is not the recipient of these notifications. This is where the trouble might start. This author information is in Woocommerce hidden and is hard to figure out who that is in your user database. It might be that the original author of the product doesn't exist anymore, and especially if the original author has been deleted from the database and you didn't move the contents of that user to a new user.
The default comment notifications are produced in this Wordpress file: wp-includes/pluggable.php
Below is a trick to override the recipient for the comment/review notification, put this code in your child-theme's functions.php and change the part example#example.com to your desired email recipient and you will receive a notification every time someone adds a comment/review to your site.
function new_comment_moderation_recipients( $emails, $comment_id ) {
return array( 'example#example.com' );
}
add_filter( 'comment_moderation_recipients', 'new_comment_moderation_recipients', 24, 2 );
add_filter( 'comment_notification_recipients', 'new_comment_moderation_recipients', 24, 2 );
I tested it and it works.
On the Wordpress Dashboard navigate to Settings > General and the Email address listed here is where you will get notifications.