Callback_handler won't fire WooCommerce - php

I am building a payment Gateway for WooCommerce where the payment takes place in an offsite URL. I need that page to be able to message back to the WooCommerce plugin, and a "callback" URL is really all I need.
WooCommerce seems to have this, but I can't get it to work. You're supposed to be able to ping:
http://yoursite/wc-api/WC_your_gateway
And then you're supposed to add
add_action( 'woocommerce_api_callback', 'callback_handler' );
And then it's supposed to fire a function like this
public function callback_handler() {}
But when I go to that URL, all I see is a 1 on my page - my handler should be redirecting to another page (that's what I set it to do to make it obvious). What I'd LOVE is if anyone has an example of this working. I've tried placing the add_action and the handler function lots of places, no luck.

I have the same problem. Try to add exit; or wp_die(); in the end of your callback function.
This works for me.

I had the same problem, so, this is what worked for me:
class WC_mygateway extends WC_Payment_Gateway {
public function __construct() {
//'woocommerce_api_'.strtolower(get_class($this)) will result in 'woocommerce_api_wc_mygateway'
add_action('woocommerce_api_'.strtolower(get_class($this)), array(&$this, 'handle_callback'));
}
function handle_callback() {
//Handle the thing here!
}
}
function woocommerce_mygateway_add_gateway( $methods ) {
$methods[] = 'WC_mygateway';
return $methods
}
add_filter( 'woocommerce_payment_gateways', 'woocommerce_mygateway_add_gateway');
Make sure you are not missing any of those details, other wise it wont work. Also you can call it using http://example.com/?wc-api=wc_mygateway or http://example.com/wc-api/wc_mygateway
Hope this work for everyone getting stuck with this issue!

Have you tried using http://yoursite/wc-api/WC_your_gateway/ (add slash at the end)?
Also the add_action should be "woocommerce_api_{class_name}" instead of" woocommerce_api_callback". So for your example, it should be "woocommerce_api_wc_your_gateway".

Related

override a function wrapped with if (!function_exists)

I'm using "Advanced Scripts plugin" to modify a function of other plugin, the fuction I'm trying to modify is wrappd with if( !function_exists('some_function') ).
the function inside the plugins is like this
if( !function_exists('send-invoice') ){
function send-invoice(){
//The Plugin Invoice
}
}
This is what I did
function send-invoice(){
//My Custom Invoice
}
add_action('init', 'send-invoice');
How can I make sure that my code runs before the plugin codes?
The plugin load before the theme, I tried plugin-loaded hook but nothing changed
You can to use the anonymous function for example:
add_action('init', function() {
//code here
});
More detail is here
Or use another hook muplugins_loaded:
function send-invoice(){
//My Custom Invoice
}
add_action('muplugins_loaded', 'send-invoice');
If nothing else work for you you can create a custom plugin, name it something like "aaamyplugin" and just insert there a single .php file with the function you are trying to override. This is the easiest (not cleanest) way to make sure your code overrides the plugin functions.
The reason for this is because Wordpress plugin loading order is simply alphabetical, that means that everything named before the plugin you are trying to override, get loaded first.
The cleanest way would be to look into the source code of the plugin to understand how it does what it does. Like: when does it load that file that contains the function you are trying to override? That's the important question to answer if you want to go with clean way

Wordpress add_filter('query_vars', 'my_new_vars'); not working as expected

I have an issue with Wordpress (there's a shocker), where it removes my get parameter, which i understand thats a WP feature for security and some other reasons.
What i'm trying to achieve is the following:
Load product page
When customer clicks book now they are redirected to an enquire now form
On enquire now form there is widget that retrieves what product the customer was looking at and using a GET parameter i can retrieve this product
I've tried to add the get parameter as follows:
# functions.php
function gpd_register_query_vars($vars)
{
$vars[0] = 'my_product_id';
return $vars;
}
add_filter('query_vars', 'gpd_register_query_vars');
Within my widget
class GPD_Get_Product_Widget extends WP_Widget
{
// ...
function widget($args, $instance)
{
global $wp_query;
var_dump($wp_query->query_vars['my_super_unique_var']);
extract($instance);
//output code
echo $args['before_widget'];
include 'widget.php';
echo $args['after_widget'];
}
}
//function to register the widget
function gpd_get_product_widget()
{
register_widget('GPD_Get_Product_Widget');
}
add_action('widgets_init', 'gpd_get_product_widget');
However, whenever i try to get the parameter it doesn't exist.
Wordpress isn't the easiest to navigate or work with. I'm really confused to why WP has made such a simple thing such as $_GET params so difficult.
Any help is much appreciated.
I found the answer and not entirely sure why this is but if you pass 2 params in your URL like so /my-page?a=1&b=2 and then use a plain old $_GET, you'll find that the the first element is a q and the second is your b param.
array(2) {
'q' =>
string(29) "/request-a-quote/a=1"
'b' =>
integer(1) "2"
}
It looks like the first param is occupied by q (reserved var) by Wordpress and anything after that is additional params which are yours (unless they are reserved by WP).
So if I was to build my URL like so:
add_query_arg(['type' => 'holiday', 'product_id' => 12345], get_permalink($page_id) );
You have to add a first param that will be ignored and then the second will be available as a $_GET.
I might be doing something wrong but this works for me for now. Any help and pointers to what I'm doing wrong would be great - as this feels wrong but works.

Trying to "add_action" to "create_order" function in Woocommerce/Wordpress

I'm trying to hook into the WooCommerce function that creates orders create_order or wc_create_order - and pull some of the variables from it. I've hooked into functions before, but they've never been this complicated. The functions are inside the WC_Checkout Class - and I can't seem to find the right tool to get over that wall.
The code inside the function is irrelevant at this point in time. I just want the function to fire. I've tried the basic add_action function.
add_action( 'create_order', 'my_function', 1, 1 );
I also tried a few more options that were a little complicated, but the resources for this were a little sketch. I'm pretty sure it's more complicated than this too:
function init() {
add_action('create_order', array(&$this, 'my_function'), 1, 1 );
}
I've seen some posts including __construct() but they weren't specific enough to work.

do_action inside class issue with add_meta_boxes hook

Ok, this is a little specific. I might be missing something but since it killed me enough time , even though I found a way around it, I need to know if there's a way to do it properly.
Basically I want to use add_meta_box (http://codex.wordpress.org/Function_Reference/add_meta_box ) inside a class.
What I am doing:
//an array variable I am trying to pass in the class to a callback function as a parameter
$the_array = array(
'something',
'my meta box'
);
//a class where everything happens
class some_class {
//public function that has the array and initiates the add_meta_boxes hook
public function add_box($class_array) {
//add meta boxes hook to add the meta box properly
add_action('add_meta_boxes', array($this, 'adding_custom_meta_boxes'), 10, 2);
//passing the array variable to the callback function
do_action('add_meta_boxes',$class_array);
}
//the callback function of the add_meta_boxes hook
public function adding_custom_meta_boxes($class_array) {
add_meta_box('my-meta-box', __($class_array[1]), 'render_my_meta_box', 'page', 'normal', 'default');
}
public function render_my_meta_box(){
//the code to generate the html of the meta box goes here
}
}
$class_var = new some_class();
$class_var->add_box($the_array);
I get this error:
Fatal error: Call to undefined function add_meta_box() in C:\xampp\ht.....
but only if I use do_action to pass the variables to the hook callback function
I found a way around it with global variables, but , does anybody know a correct way of doing this?
I am trying to create the meta box from inside a class and this happens. It works well from outside a class. Anybody any ideas?
your not too far off. to correct the above change to this:
public function adding_custom_meta_boxes($class_array) {
add_meta_box('my-meta-box', __($class_array[1]), array($this, 'render_my_meta_box'), 'page', 'normal', 'default');
}
remove "do_action"
do_action tells the script to perform the attached actions right now and the function add_meta_boxes has yet to load (google the load process for wp functions). Thats the whole point of add_actions / Filters!

Custom hooks in WordPress across plugins

I'm trying to create a hook in one Wordpress plugin that could be used by other plugins. First off, is this even possible? I'm also sending some additional args so this may be 2 questions in one since I've been having trouble finding definitive information on how to do this.
Here is what I've tried so far:
In the plugin that is creating the hook (call it Plugin 1) I added:
do_action('plugin1_hook', $customArg1, $customArg2, $customArg3);
at the point that I want the hook to fire. Then, in a different plugin (Plugin 2), I added:
add_action('plugin1_hook', 'my_function');
and
function my_function($customArg1, $customArg2, $customArg3) { //my code }
This does not seem to be firing the function, however.
My refence for this has been the Wordpress hook comment_post, which is defined by Wordpress as:
do_action('comment_post', $comment_ID, $commentdata['comment_approved']);
and I am using as:
add_action('comment_post', 'my_comment');
function my_comment($comment_id) { //my code }
The above snippet is functioning properly.
I thought I'd post this as an answer as it's a little clearer to explain :)
When you hook a function, but do not specify the number of arguments, WordPress will always pass back one argument.
You won't get errors for something like this;
function do_my_hook($arg1, $arg2 = '', $arg3 = '') {}
add_action('my_hook', 'do_my_hook');
But you will for something like this;
function do_my_hook($arg1, $arg2, $arg3) {}
add_action('my_hook', 'do_my_hook');
WordPress is trying to call do_my_hook(), but it's only passing back one argument. The first example uses PHP default function arguments, so that you can call a function without passing all available arguments, but without error.
The second example will trigger a 'missing argument(s)' PHP error, as all three arguments are required.
The fix?
add_action('my_hook', 'do_my_hook', 10, 3);
The idea behind defining how many arguments your function takes is to avoid errors like these (though technically they are as easily avoided using default arguments!).
My guess is the second plugin is loading after the first one, so the hook has already fired by the time you add an action to it. You might try this for the first plugin:
function my_custom_hook_insertion($arg1, $arg2, $arg3){
do_action('plugin1_hook', $arg1, $arg2, $arg3);
}
add_action('plugins_loaded', 'my_custom_hook_insertion');
That will wait until all plugins are loaded before firing the hook.
Changing my add_action to this fixed the problem:
add_action('plugin1_hook', 'my_function', 10, 3);
The 10 represents the priority, and the 3 represents the number of args that the function will take. I'm not exactly sure how the matching works, since the default is 1, and I use plenty of hooks without specifying 0 args and I've used hooks that pass more than 1 arg but only used 1 arg in my function signature. Source: WordPress Codex: Function Reference/add action
It is working though, so cross plugin hooks are possible.

Categories