简体   繁体   English

在Woocommerce 3中添加自定义结帐字段作为订单自定义元数据

[英]Add custom checkout field as Order custom-meta data in Woocommerce 3

before WooCommerce 3.0 came out my code had worked like a charm to save custom values from the cart into the order on checkout. WooCommerce 3.0问世之前,我的代码就像一个魅力,可以在结账时将购物车中的自定义值保存到订单中。 But since then I'm not able to create custom meta for orders. 但从那时起,我无法为订单创建自定义元。

Environment: Wordpress 4.9.4 & WooCommerce 3.3.3 环境: Wordpress 4.9.4和WooCommerce 3.3.3

Hooks

  1. add_action('woocommerce_checkout_update_order_meta', 'custom_meta_to_order', 20, 1);
  2. add_action('woocommerce_checkout_create_order', 'custom_meta_to_order', 20, 1);

The Hook number 1 is the one I tried the most, the 2 one was just an experiment with some literal changes mentioned in this topic . Hook编号1是我最常尝试的编号,2编号只是一个实验,其中包含本主题中提到的一些字面更改

Function 功能

The following functions-code is related to hook number 1 : 以下函数代码与挂钩编号1相关

if (!function_exists('custom_meta_to_order')) {
    function custom_meta_to_order($order_id, $values) {
        $order = wc_get_order( $order_id );

        $order->update_meta_data('_TESTKEYstart', 'Hello');

        if (isset($values['myValue'])) {
            $myValue = $values['myValue'];
            if (!empty($myValue)) $order->update_meta_data('_myKey', $myValue);
        }

        $order->update_meta_data('_TESTKEYend', 'Bye');

        $order->save();
    }
}

I've checked also in the mySQL table table wp_woocommerce_order_itemmeta if at least the two _TESTKEY* -meta-entrys will be created (because they don't have a condition). 如果至少会创建两个_TESTKEY * -meta - entrys (因为它们没有条件),我也在mySQL表格table wp_woocommerce_order_itemmetawp_woocommerce_order_itemmeta

  • But it seems that the meta-keys and values don't getting created via this hook and function. 但似乎元键和值不会通过此钩子和函数创建。
  • The function itself getting called, so at least the hooks itselfs are working. 函数本身被调用,所以至少钩子本身是有效的。

So my question is: "What am I doing wrong?" 所以我的问题是:“我做错了什么?”

UPDATED: There is some errors in your code… 更新:您的代码中存在一些错误......

  • Both hooks have only 1 argument (not 2, so $values doesn't exist) 两个钩子只有1个参数(不是2,所以$values不存在)
  • To get your custom field you should use $_POST['myValue'] instead. 要获得自定义字段,您应该使用$_POST['myValue']
  • and other things like each hook has a different argument: 和每个钩子之类的其他东西有不同的参数:
    • $order_id for woocommerce_checkout_update_order_meta $order_id for woocommerce_checkout_update_order_meta
    • $order for woocommerce_checkout_create_order $order woocommerce_checkout_create_order

Below I have replaced $_POST['myValue'] by $_POST['billing_country'] as you don't give the code for this custom checkout field… 下面,我已经取代$_POST['myValue']$_POST['billing_country']你不给这个定制结账领域代码...

So here are both ways: 所以这两种方式:

1) The best way for me, as explained here : 1)对我来说,最好的办法,为解释在这里

if ( ! function_exists('custom_meta_to_order') ) {
    add_action( 'woocommerce_checkout_create_order', 'custom_meta_to_order', 20, 1 );
    function custom_meta_to_order( $order ) {

        $order->update_meta_data('_TESTKEYstart', 'Hello');

        if (isset($_POST['billing_country'])) {
            $myValue = $_POST['billing_country'];
            if (!empty($myValue)) $order->update_meta_data('_my_key', $myValue);
        }

        $order->update_meta_data('_TESTKEYend', 'Bye');
    }
}

Code goes in function.php file of your active child theme (or theme) . 代码位于活动子主题(或主题)的function.php文件中 Tested and works. 经过测试和工作。


2) The other way: 2)另一种方式:

if ( ! function_exists('custom_meta_to_order') ) {
    add_action('woocommerce_checkout_update_order_meta', 'custom_meta_to_order', 20, 1);
    function custom_meta_to_order( $order_id ) {
        // get an instance of the WC_Order object
        $order = wc_get_order( $order_id );

        $order->update_meta_data('_TESTKEYstart', 'Hello');

        if (isset($_POST['billing_country'])) {
            $myValue = $_POST['billing_country'];
            if (!empty($myValue)) $order->update_meta_data('_my_key', $myValue);
        }

        $order->update_meta_data('_TESTKEYend', 'Bye');

        // Save the order data and meta data
        $order->save();
    }
}

Code goes in function.php file of your active child theme (or theme) . 代码位于活动子主题(或主题)的function.php文件中 Tested and works. 经过测试和工作。

The proof: 证据:

在此输入图像描述

And (in database wp_postmeta table for this order ID) : 并且(在此订单ID的数据库wp_postmeta表中)

在此输入图像描述

Tested in WooCommerce version 3.3+ 在WooCommerce版本3.3+中测试过


You can use the old way too (which works) : 您也可以使用旧方法(有效)

if ( ! function_exists('custom_meta_to_order') ) {
    add_action('woocommerce_checkout_update_order_meta', 'custom_meta_to_order', 20, 1);
    function custom_meta_to_order( $order_id ) {

        update_post_meta( $order_id, '_TESTKEYstart', 'Hello' );

        if ( isset( $_POST['billing_country'] ) ) {
            $myValue = $_POST['billing_country'];
            if (!empty($myValue)) 
                update_post_meta( $order_id, '_my_key', $myValue);
        }

        update_post_meta( $order_id, '_TESTKEYend', 'Bye');
    }
}

Code goes in function.php file of your active child theme (or theme) . 代码位于活动子主题(或主题)的function.php文件中 Tested and works. 经过测试和工作。


Related: Add extra meta for orders in Woocommerce 相关: 为Woocommerce中的订单添加额外的元数据

Because comments are really hard to read (because of to much restricted formatation), this answer is just a response to the answer from LoicTheAztec . 因为评论很难阅读(因为格式化程度很大),这个答案只是对LoicTheAztec答案的回应

I wrote a longer answer but it seems gone, so I sorry now for the much shorter one! 我写了一个更长的答案,但似乎已经不见了,所以我现在抱歉要短得多!

First our misunderstanding 首先是我们的误解

You understood that I would like to use custom values from products but in my case it was a little bit other. 您了解我想使用产品的自定义值,但在我的情况下,它有点其他。 I wrote an external application which included the wp-load.php and posted then data back to the product-page into the cart. 我写了一个外部应用程序,其中包括wp-load.php ,然后将数据发布回产品页面到购物车中。

So the problem showed up here was the attempt to write the data from the cart into the order on checkout. 所以这里出现的问题是尝试在结账时将购物车中的数据写入订单。

Recommend ways doesn't worked at first 推荐方法起初并不起作用

The recommend ways you suggested all doesn't worked. 您建议的所有建议方法都不起作用。 I also stripped them so much down that they have should work and just write something into the meta. 我也把他们剥了很多,以至于他们应该工作,只是在元中写点东西。 I've no clue which plugin/theme-function pranked me this time here. 我不知道哪个插件/主题功能这次恶作剧我。

But I was able to solve the problem 但我能够解决问题

And many more! 还有很多! Just because I found the blog-post where I found out in the past, how to do this and as addition to my personal luck the author wrote already the changes for WP3.0 , related to this process. 仅仅因为我找到我过去发现的博客帖子 ,如何做到这一点,作为我个人运气的补充,作者已经写了WP3.0变化 ,与此过程有关。

Still your post helped me 你的帖子仍然帮助了我

The errors you showed me bugged me since then and because it was hard to follow and inspect everything with Sublime and CodeIntel (and my start with Symfony itself) I decided to buy PHPStorm which showed and allowed me to fix all of my deprecated (legacy-using) functions by updating them properly. 你告诉我的错误从那时起就给我带来了麻烦,因为很难用Sublime和CodeIntel来跟踪和检查所有内容(我开始使用Symfony本身)我决定购买PHPStorm,它显示并允许我修复所有我弃用的内容(遗产 - 使用)通过正确更新它们的功能。

( Finally no more global-variables: Yay. ) (最后没有更多全局变量:耶。)

I mean, showing up parameters inline and deprecation-strokes already did a great job. 我的意思是,显示参数内联和弃用笔画已经做得很好。 But a bug-free working code-intel/reference which doesn't dies on big projects is just awesome. 但是一个没有错误的工作代码 - 英特尔/参考,并没有在大型项目上死亡,这真是太棒了。

That's why I marked your answer now as solution, thanks. 这就是为什么我现在将你的答案标记为解决方案,谢谢。 Otherwise I would have just fixed the problem maybe (thanks to the authors blog-post) but still would sit on a ticking time bomb. 否则我可能只是解决了问题(感谢作者的博客文章),但仍然会坐在定时炸弹上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将自定义结帐字段(用户输入)添加到订单数据和用户元数据 - Add Custom Checkout Field (user input) to Order Data and User Meta 将Woocommerce结帐中的自定义值另存为自定义订单元数据 - Save custom values from Woocommerce checkout as custom order meta data 将自定义结帐字段添加到WooCommerce中的订单 - Add Custom checkout field to Order in WooCommerce 在 Woocommerce 中添加自定义结帐多选字段并更新订单元 - Add a custom checkout multi-select field and update order meta in Woocommerce 将多个 WooCommerce 自定义结帐字段保存为订单元数据 - Save multiple WooCommerce custom checkout fields as order meta data 将 WooCommerce 结帐自定义字段保存为用户元数据 - Save WooCommerce checkout custom field as user meta data WooCommerce:添加自定义字段以订购元和管理订单 - WooCommerce: add custom field to order meta and admin order 在结帐中添加自定义字段并在 WooCommerce 管理订单页面中显示 - Add custom field in checkout and display it in WooCommerce admin order pages 在 WooCommerce 中的客户订单备注中添加自定义结帐字段值 - Add a custom checkout field value to customer order notes in WooCommerce woocommerce 自定义结帐字段以添加订购 ajax 的费用 - woocommerce custom checkout field to add fee to order ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM