$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - add referrer to woo|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - add referrer to woo

matteradmin10PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far