metaboxs.php,php – WooCommerce:将自定义Metabox添加到管理员订单页面

我目前正在向我的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 '

'.__('My Field').'

';

echo 'fill in this field ';

echo '

';

}

// 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调整为其他内容?

我已经尝试了所有东西,但似乎没有用.当我的字段在结帐页面上时,我可以让它工作,但是当我从产品页面拉出它时,我可以使用它.

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

解决方法:

UPDATE 2017/11/02 (Works perfectly in Woocommerce 3+)

首先,除了在订单页面中的后端“自定义字段”Metabox中获取my_field_name的值之外,我已经完成了所有工作.

经过一场真正的噩梦之后,我找到了一个非常好的工作解决方案,比以前更好.在后端,您现在有一个自定义元数据框,其自定义字段my_field_name显示正确的值,如此屏幕截图所示:

aeiSz.png

我的代码分为两部分.

1)订单页面中的后端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 '

}

}

// 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 '

' . __( 'My Field') . '


';

}

// 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 '

}

// 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 '

'. __("My Field", "woocommerce").': ' . get_post_meta( $order->id, '_my_field_slug', true ) . '

';

}

}

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

}

现在一切都按预期工作了.

标签:orders,custom-fields,php,wordpress,woocommerce

来源: https://codeday.me/bug/20190923/1813433.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值