首页 文章

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

提问于
浏览
1

WooCommerce 3.0 出来之前,我的代码就像一个魅力,可以将自定义值从购物车保存到结帐时的订单中 . 但从那时起,我无法为订单创建自定义元 .

Environment: Wordpress 4.9.4和WooCommerce 3.3.3

钩子

  • add_action('woocommerce_checkout_update_order_meta', 'custom_meta_to_order', 20, 1);

  • add_action('woocommerce_checkout_create_order', 'custom_meta_to_order', 20, 1);

Hook编号1是我最常用的编号,2编号只是一个文字changes mentioned in this topic的实验 .

功能

以下函数代码与挂钩编号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();
    }
}

如果至少要创建两个 _TESTKEY* -meta-entrys,我也会检查mySQL表 table wp_woocommerce_order_itemmeta (因为它们没有条件) .

  • 但似乎没有通过此钩子和函数创建元键和值 .

  • 函数本身被调用,所以至少钩子本身正在工作 .

所以我的问题是:“我做错了什么?”

2 回答

  • 0

    UPDATED: 您的代码中存在一些错误...

    • 两个钩子只有1个参数(不是2,所以 $values 不存在)

    • 要获取自定义字段,您应该使用 $_POST['myValue'] .

    • 和每个钩子之类的其他东西有不同的参数:

    • $order_id for woocommerce_checkout_update_order_meta

    • $order for woocommerce_checkout_create_order

    下面我已经将 $_POST['myValue'] 替换为 $_POST['billing_country'] ,因为您没有提供此自定义结帐字段的代码...

    所以这两种方式:

    1)对我来说最好的方法,如_501715所述:

    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');
        }
    }
    

    代码位于活动子主题(或主题)的function.php文件中 . 经过测试和工作 .


    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();
        }
    }
    

    代码位于活动子主题(或主题)的function.php文件中 . 经过测试和工作 .

    证据:

    enter image description here

    并且(在此订单ID的数据库 wp_postmeta 表中):

    enter image description here

    在WooCommerce 3.3版中测试过


    您也可以使用旧方式(有效):

    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');
        }
    }
    

    代码位于活动子主题(或主题)的function.php文件中 . 经过测试和工作 .


    相关:Add extra meta for orders in Woocommerce

  • 2

    Because comments are really hard to read (because of to much restricted formatation), this answer is just a response to the answer from LoicTheAztec.

    我写了一个更长的答案,但它似乎已经消失了,所以我现在抱歉要短得多!

    First our misunderstanding

    您了解我想使用产品的自定义值,但在我的情况下,它有点其他 . 我写了一个包含 wp-load.php 的外部应用程序,然后将数据发布回产品页面到购物车中 .

    所以这里出现的问题是尝试在结账时将购物车中的数据写入订单 .

    Recommend ways doesn't worked at first

    您建议的所有建议方式都不起作用 . 我也把它们剥离了很多,以至于它们应该工作并且只是在元中写入一些内容 . 我不知道这次哪个插件/主题功能让我恶作剧 .

    But I was able to solve the problem

    还有很多!仅仅因为我找到了the blog-post我过去发现的地方,如何做到这一点,作为我个人运气的补充,作者已经写了changes for WP3.0,与这个过程有关 .

    Still your post helped me

    你告诉我的错误从那时起就给我带来了麻烦,因为很难用Sublime和CodeIntel来跟踪和检查所有内容(我开始使用Symfony本身)我决定购买PHPStorm,它显示并允许我修复所有我弃用的内容(遗产 - 使用)通过正确更新它们的功能 .

    (最后没有更多的全局变量:耶 . )

    我的意思是,显示参数内联和弃用笔画已经做得很好 . 但是一个没有错误的工作代码 - 英特尔/参考,并没有在大型项目上死亡,这真是太棒了 .

    这就是为什么我现在将你的答案标记为解决方案,谢谢 . 否则我可能已经解决了问题(感谢作者的博客文章),但仍然会坐在定时炸弹上 .

相关问题