functions - add referrer to woo

admin2025-06-02  3

we just moved from Shopify to Woo and wanted to add a custom field "referrer" to an order, to track our ads.

I found this article, but somehow does not manage to make it work. maybe it is a bit outdated?

/

2019-02-27T10:30:22+00:00 CRITICAL Uncaught ArgumentCountError: Too few 

arguments to function add_referral_meta(), 1 passed in /www/myshop_210/public/wp-includes/class-wp-hook.php on line 288 and exactly 2 expected in /www/myshop_210/public/wp-content/themes/woondershop-pt-child/functions.php:47
Stack trace:
#0 /www/myshop_210/public/wp-includes/class-wp-hook.php(288): add_referral_meta(6758)
#1 /www/myshop_210/public/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
#2 /www/myshop_210/public/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#3 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(367): do_action('woocommerce_che...', 6758, Array)
#4 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(1067): WC_Checkout->create_order(Array)
#5 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-form-handler.php(334): WC_Checkout->process_checkout()
#6 /www/myshop_210/public/wp-includes/class-wp-hook.php(286) in /www/myshop_210/public/wp-content/themes/woondershop-pt-child/functions.php in Zeile 47

2019-02-27T10:31:42+00:00 CRITICAL Uncaught ArgumentCountError: Too few arguments to function add_referral_meta(), 1 passed in /www/myshop_210/public/wp-includes/class-wp-hook.php on line 288 and exactly 2 expected in /www/myshop_210/public/wp-content/themes/woondershop-pt-child/functions.php:47
Stack trace:
#0 /www/myshop_210/public/wp-includes/class-wp-hook.php(288): add_referral_meta(6760)
#1 /www/myshop_210/public/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
#2 /www/myshop_210/public/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#3 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(367): do_action('woocommerce_che...', 6760, Array)
#4 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(1067): WC_Checkout->create_order(Array)
#5 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-form-handler.php(334): WC_Checkout->process_checkout()
#6 /www/myshop_210/public/wp-includes/class-wp-hook.php(286) in /www/myshop_210/public/wp-content/themes/woondershop-pt-child/functions.php in Zeile 47

2019-02-27T11:46:53+00:00 CRITICAL Uncaught ArgumentCountError: Too few arguments to function add_referral_meta(), 1 passed in /www/myshop_210/public/wp-includes/class-wp-hook.php on line 288 and exactly 2 expected in /www/myshop_210/public/wp-content/themes/woondershop-pt-child/functions.php:52
Stack trace:
#0 /www/myshop_210/public/wp-includes/class-wp-hook.php(288): add_referral_meta(6762)
#1 /www/myshop_210/public/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
#2 /www/myshop_210/public/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#3 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(367): do_action('woocommerce_che...', 6762, Array)
#4 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(1067): WC_Checkout->create_order(Array)
#5 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-form-handler.php(334): WC_Checkout->process_checkout()
#6 /www/myshop_210/public/wp-includes/class-wp-hook.php(286) in /www/myshop_210/public/wp-content/themes/woondershop-pt-child/functions.php in Zeile 52

I did not find another newer version or plugin.. Could any of you help me out?

thanks, E.

we just moved from Shopify to Woo and wanted to add a custom field "referrer" to an order, to track our ads.

I found this article, but somehow does not manage to make it work. maybe it is a bit outdated?

https://blog.stevenkellow/add-referrer-info-to-woocommerce-order/

2019-02-27T10:30:22+00:00 CRITICAL Uncaught ArgumentCountError: Too few 

arguments to function add_referral_meta(), 1 passed in /www/myshop_210/public/wp-includes/class-wp-hook.php on line 288 and exactly 2 expected in /www/myshop_210/public/wp-content/themes/woondershop-pt-child/functions.php:47
Stack trace:
#0 /www/myshop_210/public/wp-includes/class-wp-hook.php(288): add_referral_meta(6758)
#1 /www/myshop_210/public/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
#2 /www/myshop_210/public/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#3 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(367): do_action('woocommerce_che...', 6758, Array)
#4 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(1067): WC_Checkout->create_order(Array)
#5 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-form-handler.php(334): WC_Checkout->process_checkout()
#6 /www/myshop_210/public/wp-includes/class-wp-hook.php(286) in /www/myshop_210/public/wp-content/themes/woondershop-pt-child/functions.php in Zeile 47

2019-02-27T10:31:42+00:00 CRITICAL Uncaught ArgumentCountError: Too few arguments to function add_referral_meta(), 1 passed in /www/myshop_210/public/wp-includes/class-wp-hook.php on line 288 and exactly 2 expected in /www/myshop_210/public/wp-content/themes/woondershop-pt-child/functions.php:47
Stack trace:
#0 /www/myshop_210/public/wp-includes/class-wp-hook.php(288): add_referral_meta(6760)
#1 /www/myshop_210/public/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
#2 /www/myshop_210/public/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#3 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(367): do_action('woocommerce_che...', 6760, Array)
#4 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(1067): WC_Checkout->create_order(Array)
#5 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-form-handler.php(334): WC_Checkout->process_checkout()
#6 /www/myshop_210/public/wp-includes/class-wp-hook.php(286) in /www/myshop_210/public/wp-content/themes/woondershop-pt-child/functions.php in Zeile 47

2019-02-27T11:46:53+00:00 CRITICAL Uncaught ArgumentCountError: Too few arguments to function add_referral_meta(), 1 passed in /www/myshop_210/public/wp-includes/class-wp-hook.php on line 288 and exactly 2 expected in /www/myshop_210/public/wp-content/themes/woondershop-pt-child/functions.php:52
Stack trace:
#0 /www/myshop_210/public/wp-includes/class-wp-hook.php(288): add_referral_meta(6762)
#1 /www/myshop_210/public/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
#2 /www/myshop_210/public/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#3 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(367): do_action('woocommerce_che...', 6762, Array)
#4 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(1067): WC_Checkout->create_order(Array)
#5 /www/myshop_210/public/wp-content/plugins/woocommerce/includes/class-wc-form-handler.php(334): WC_Checkout->process_checkout()
#6 /www/myshop_210/public/wp-includes/class-wp-hook.php(286) in /www/myshop_210/public/wp-content/themes/woondershop-pt-child/functions.php in Zeile 52

I did not find another newer version or plugin.. Could any of you help me out?

thanks, E.

Share Improve this question asked Feb 27, 2019 at 11:53 EleEle 1135 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The code in the original article is broken, and would've been broken when it was written. So being outdated is not the problem (2 years is nothing in WordPress time).

The issue is this line:

add_action('woocommerce_checkout_update_order_meta', 'add_referral_meta');

The callback function they've created, add_referral_meta(), accepts two arguments, $order_id and $posted:

function add_referral_meta( $order_id, $posted ) {

But if a callback function accepts more than one argument, the add_action() call needs to specify how many. So that first line of code needs to look like this:

add_action( 'woocommerce_checkout_update_order_meta', 'add_referral_meta', 10, 2 );

You can read more about how actions work in WordPress in the developer documentation.

That being said, this isn't a future-proof solution, since it's not using the proper WooCommerce methods for saving order meta. I suggest using this code instead:

function wpse_330106_set_origin_cookie() {
    $cookie_value = $_SERVER['HTTP_REFERER'];

    if ( ! is_admin() && ! isset( $_COOKIE['origin'] ) ) {
        setcookie( 'origin', $cookie_value, time() + 3600*24*30, COOKIEPATH, COOKIE_DOMAIN, false );
    }
}
add_action( 'init', 'wpse_330106_set_origin_cookie');

function wpse_330106_add_order_referrer( $order ) {
    $order->add_meta_data( 'referrer', $_COOKIE['origin'] );
}
add_action('woocommerce_checkout_create_order', 'wpse_330106_add_order_referrer', 10, 1 );

This solution uses the more modern $order->add_meta_data() method, which is the current proper way to add meta data to WooCommerce orders these days. This is because it will continue to work regardless of where WooCommerce stores its order data. This will be useful when WooCommerce inevitable starts storing orders in a custom table, rather than as posts.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748804431a313858.html

最新回复(0)