add referrer to woo

时间:2019-02-27 作者:Ele

我们刚从Shopify转到Woo,希望在订单中添加一个自定义字段“Referer”,以跟踪我们的广告。

我找到了这篇文章,但不知怎么的,它并没有发挥作用。也许有点过时了?

https://blog.stevenkellow.com/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
我没有找到其他更新的版本或插件。。你们谁能帮帮我吗?

谢谢,E。

1 个回复
SO网友:Jacob Peattie

原始文章中的代码已损坏,在编写时可能已损坏。因此,过时不是问题(2年在WordPress时间内算不上什么)。

问题是这一行:

add_action(\'woocommerce_checkout_update_order_meta\', \'add_referral_meta\');
他们创建的回调函数,add_referral_meta(), 接受两个参数,$order_id$posted:

function add_referral_meta( $order_id, $posted ) {
但如果回调函数接受多个参数,则add_action() 调用需要指定数量。因此,第一行代码需要如下所示:

add_action( \'woocommerce_checkout_update_order_meta\', \'add_referral_meta\', 10, 2 );
您可以在developer documentation.

尽管如此,这并不是一个经得起未来考验的解决方案,因为它没有使用适当的WooCommerce方法来保存订单元数据。我建议使用以下代码:

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 );
此解决方案使用更现代的$order->add_meta_data() 方法,这是当前向WooCommerce订单添加元数据的正确方法。这是因为无论WooCommerce将订单数据存储在何处,它都将继续工作。当WooCommerce不可避免地开始将订单存储在自定义表中而不是作为POST时,这将非常有用。