【问题标题】:Add custom field data to WooCommerce order将自定义字段数据添加到 WooCommerce 订单
【发布时间】:2016-10-12 12:35:30
【问题描述】:

我的 WooCommerce 单品上有一个自定义字段。它可以很好地发送到购物车,在结帐时很好地显示,在仪表板中很好地显示在订单中。

我现在要做的是将值设置为订单页面中的自定义字段,以便我能够在需要时修改文本。由于某种原因,当我提交表单时,此步骤不起作用。

我在functions.phpfile 中使用的代码:

// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');

function my_custom_checkout_field() {
    echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
    echo  '<label>fill in this field</label> <input type="text" name="my_field_name">';
    echo '</div>';
}

// Store custom field
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
    if( isset( $_REQUEST['my_field_name'] ) ) {
        $cart_item_data[ 'my_field_name' ] = $_REQUEST['my_field_name'];
        /* below statement make sure every add to cart action as unique line item */
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );

// Render meta on cart and checkout
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
    $custom_items = array();
    /* Woo 2.4.2 updates */
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }
    if( isset( $cart_item['my_field_name'] ) ) {
        $custom_items[] = array( "name" => 'My Field', "value" => $cart_item['my_field_name'] );
    }
    return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );

// Display as order meta
function my_field_order_meta_handler( $item_id, $values, $cart_item_key ) {
    if( isset( $values['my_field_name'] ) ) {
        wc_add_order_item_meta( $item_id, "my_field_name", $values['my_field_name'] );
    }
}
add_action( 'woocommerce_add_order_item_meta', 'my_field_order_meta_handler', 1, 3 );

/** THIS IS WHERE I'M STUCK **/

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
    global $woocommerce;

    // Check if set, if its not set add an error. This one is only requite for companies
    if ($_POST['billing_company'])
        if (!$_POST['my_field_name']) 
            $woocommerce->add_error( __('Please enter your XXX.') );
}
// Update the user meta with field value
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
    if ($user_id && $_POST['my_field_name']) update_user_meta( $user_id, 'my_field_name', esc_attr($_POST['my_field_name']) );
}

// Update the order meta with field value

add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
}

当前发生的情况的屏幕截图:



我希望发生的事情:


任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce custom-fields orders


    【解决方案1】:

    更新:与 Woocommerce 版本 3+ 的兼容性

    您缺少在订单编辑页面上显示此自定义字段值的功能:

    /**
     * Display field value on the order edit page
     */
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
    function my_custom_checkout_field_display_admin_order_meta( $order ){
        $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
        echo '<p><strong>'.__('My Field Name').':</strong> ' . get_post_meta( $order_id, 'my_field_name', true ) . '</p>';
    }
    

    在下面的参考链接上,您拥有所有原始的 wooThemes 功能工作代码 sn-ps。这是一个功能齐全的优秀教程。

    参考:[使用操作和过滤器自定义结帐字段][1]


    编辑:在订单项元数据中获取与您的自定义字段值一起显示的自定义标签

    使用您的自定义字段值(按项目元顺序) 获得像“MY field name”这样的自定义标签,而不是像 my_field_name 这样的 slug strong>,请参考此踏板:

    【讨论】:

    • 谢谢,这绝对让我走上了正确的道路,我最不想做的事情是结帐处理阶段。因为我的字段实际上是在产品页面上插入的,我如何处理使用该字段而不是文档中显示的必填字段的结帐?
    猜你喜欢
    • 2021-02-22
    • 1970-01-01
    • 2021-05-07
    • 2017-09-17
    • 2018-06-30
    • 1970-01-01
    • 2020-12-30
    • 2018-02-04
    • 1970-01-01
    相关资源
    最近更新 更多