WooCommerce:将自定义Metabox添加到管理订单页面

MV-123

我目前已成功在我的WooCommerce产品页面上添加了一个显示值的字段:

  • 在购物车(前端)中,
  • 在结帐页面(前端)上,
  • 在订单页面(前端)上,
  • 并在管理员个人订单页面(后端)中。

问题:它没有在管理订单“自定义字段” Metabox中显示为带有自定义字段的自定义字段,而是在订单页面中显示为文本。

这是我的工作代码:

// 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 );

// This is what I think needs changing?

function subscription_order_meta_handler( $item_id, $values, $cart_item_key ) {
    if( isset( $values['my_field_name'] ) ) {
        wc_add_order_item_meta( $item_id, "My Field", $values['my_field_name'] );
    }
}
add_action( 'woocommerce_add_order_item_meta', 'subscription_order_meta_handler', 1, 3 );

我认为这是代码的最后一部分需要更改。它当前在订单项下显示文本,所以也许我需要调整wc_add_order_item_meta其他内容?

我已经尝试了一切,但似乎没有用。当我的字段在结帐页面上时,我可以使它工作,但是当我从产品页面中将其拉出时,它不能工作。

也许我错过了结帐流程代码段?

LoicTheAztec

更新2017/11/02 (在Woocommerce 3+中完美运行)

首先我得到的一切工作已经预计,除所获得的价值my_field_name在后端“自定义字段” Metabox订单页面内。

然后经过一场噩梦,我找到了一个比以前更好的不错的工作解决方案在后端,您现在有了一个Custom元框,其中的custom字段my_field_name显示正确的值,如以下屏幕截图所示:

屏幕截图


我的代码分为2部分。

1)Order页面中后端Metabox,其中一个可编辑字段显示了来自产品页面(前端)中的自定义字段的正确值:

// Adding Meta container admin shop_order pages
add_action( 'add_meta_boxes', 'mv_add_meta_boxes' );
if ( ! function_exists( 'mv_add_meta_boxes' ) )
{
    function mv_add_meta_boxes()
    {
        add_meta_box( 'mv_other_fields', __('My Field','woocommerce'), 'mv_add_other_fields_for_packaging', 'shop_order', 'side', 'core' );
    }
}

// Adding Meta field in the meta container admin shop_order pages
if ( ! function_exists( 'mv_add_other_fields_for_packaging' ) )
{
    function mv_add_other_fields_for_packaging()
    {
        global $post;

        $meta_field_data = get_post_meta( $post->ID, '_my_field_slug', true ) ? get_post_meta( $post->ID, '_my_field_slug', true ) : '';

        echo '<input type="hidden" name="mv_other_meta_field_nonce" value="' . wp_create_nonce() . '">
        <p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
            <input type="text" style="width:250px;";" name="my_field_name" placeholder="' . $meta_field_data . '" value="' . $meta_field_data . '"></p>';

    }
}

// Save the data of the Meta field
add_action( 'save_post', 'mv_save_wc_order_other_fields', 10, 1 );
if ( ! function_exists( 'mv_save_wc_order_other_fields' ) )
{

    function mv_save_wc_order_other_fields( $post_id ) {

        // We need to verify this with the proper authorization (security stuff).

        // Check if our nonce is set.
        if ( ! isset( $_POST[ 'mv_other_meta_field_nonce' ] ) ) {
            return $post_id;
        }
        $nonce = $_REQUEST[ 'mv_other_meta_field_nonce' ];

        //Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $nonce ) ) {
            return $post_id;
        }

        // If this is an autosave, our form has not been submitted, so we don't want to do anything.
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }

        // Check the user's permissions.
        if ( 'page' == $_POST[ 'post_type' ] ) {

            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return $post_id;
            }
        } else {

            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        }
        // --- Its safe for us to save the data ! --- //

        // Sanitize user input  and update the meta field in the database.
        update_post_meta( $post_id, '_my_field_slug', $_POST[ 'my_field_name' ] );
    }
}

2)前端/后端:

•产品页面自定义字段(前端)。
•在购物车,结帐页面和谢谢您的订单上显示此数据(前端)。
•在订单页面上显示数据(后端)

// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_product_field');
function my_custom_product_field() {
    echo '<div id="my_custom_field">
        <label>' . __( 'My Field') . ' </label>
        <input type="text" name="my_field_name" value="">
    </div><br>';
}

// Store custom field
add_filter( 'woocommerce_add_cart_item_data', 'save_my_custom_product_field', 10, 2 );
function save_my_custom_product_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() );
        WC()->session->set( 'my_order_data', $_REQUEST['my_field_name'] );
    }
    return $cart_item_data;
}

// Add a hidden field with the correct value to the checkout
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
    $value = WC()->session->get( 'my_order_data' );
    echo '<div id="my_custom_checkout_field">
            <input type="hidden" class="input-hidden" name="my_field_name" id="my_field_name" value="' . $value . '">
    </div>';
}

// Save the order meta with hidden 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 ( ! empty( $_POST['my_field_name'] ) ) {
        update_post_meta( $order_id, '_my_field_slug', $_POST['my_field_name'] );
    }
}

// Display field value on the order edit page (not in custom fields metabox)
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){
    $my_custom_field = get_post_meta( $order->id, '_my_field_slug', true );
    if ( ! empty( $my_custom_field ) ) {
        echo '<p><strong>'. __("My Field", "woocommerce").':</strong> ' . get_post_meta( $order->id, '_my_field_slug', true ) . '</p>';
    }
}

// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
    $custom_items = array();
    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 the information as meta data so that it can be seen as part of the order
add_action('woocommerce_add_order_item_meta','add_values_to_order_item_meta', 10, 3 );
function add_values_to_order_item_meta( $item_id, $cart_item, $cart_item_key ) {
    // lets add the meta data to the order (with a label as key slug)
    if( ! empty( $cart_item['my_field_name'] ) )
        wc_add_order_item_meta($item_id, __('My field label name'), $cart_item['my_field_name'], true);
}

现在一切都按预期进行。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在WooCommerce中将自定义URL链接添加到管理订单列表页面

在Woocommerce 3中将自定义元数据添加到管理订单编辑页面

将自定义ajax按钮添加到WooCommerce管理订单列表

在Woocommerce 3中将自定义批量操作添加到管理订单列表

将自定义帐单数据添加到Woocommerce管理订单中的帐单格式地址中

将自定义字段数据添加到WooCommerce订单

在WooCommerce中将自定义结帐字段值添加到客户订单注释中

将自定义订单状态添加到WooCommerce管理员订单列表中的过滤器菜单

Metabox具有WooCommerce管理订单页面的多个自定义字段

在Woocommerce中将自定义多选字段添加到管理产品选项设置

在WooCommerce 3中将自定义列添加到管理产品列表

如何将自定义视图页面添加到 django 管理员?

如何将自定义的左链接模型添加到Wagtail管理页面

WooCommerce:将自定义模板添加到客户帐户页面

将自定义字段添加到WooCommerce新帐户页面

将img添加到Woocommerce的管理订单列表中的自定义操作按钮

将自定义字体添加到GitHub页面

将自定义页面添加到Spartacus

将自定义页脚添加到页面模板

将自定义购物车商品值添加到WooCommerce订单商品元数据

将自定义文本添加到本地取货 Woocommerce 订单的特定电子邮件通知

如何将自定义字段作为抄送添加到WooCommerce客户订单电子邮件

如何将自定义文件添加到自定义文章列表页面

在Woocommerce编辑订单页面自定义metabox中获取自定义字段值

在Woocommerce中将自定义“销售价格”列添加到管理产品列表

如何将自定义 CSS 属性(如字体大小)添加到所有 Django 管理页面?

在WooCommerce中将自定义标签添加到特定产品类型的单个产品页面

Magento:将自定义属性添加到订单网格

在Woocommerce管理员订单编辑页面中以编程方式添加自定义订单注释