我目前已成功向我的 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
调整为其他内容?
我已经尝试了一切,但似乎不起作用。当我的字段位于结账页面上时,我可以让它工作,但当我从产品页面上拉出它时,它就不行了。
也许我缺少结账流程片段?
更新 2017/11/02 (在 Woocommerce 3+ 中完美运行)
首先,我已经让一切按预期工作,除了在订单页面的后端“自定义字段”元框中获取 my_field_name
的值。
在经历了一场真正的噩梦之后,我找到了一个非常好的工作解决方案,比以前更好。在后端,您现在有一个自定义元框,其中自定义字段
my_field_name
显示正确的值,如以下屏幕截图所示:
我的代码分为两部分。
// Adding Meta container admin shop_order pages
add_action( 'add_meta_boxes', 'add_shop_order_meta_boxes' );
if ( ! function_exists( 'add_shop_order_meta_boxes' ) )
{
function add_shop_order_meta_boxes()
{
add_meta_box( 'custom_other_field', __('My Field','woocommerce'), 'add_custom_other_field_content', 'shop_order', 'side', 'core' );
}
}
// Adding Meta field in the meta container on admin shop_order pages
if ( ! function_exists( 'add_custom_other_field_content' ) )
{
function add_custom_other_field_content( $post )
{
$order = wc_get_order($post->ID); // Get the WC_Order object
printf('<p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
<input type="hidden" name="custom_other_field_nonce" value="%s">
<input type="text" style="width:250px;" name="my_field_slug" placeholder="%s" value="%s">
</p>',
wp_create_nonce(),
__('some placeholder text', 'woocommerce'),
$order->get_meta('_my_field_slug')
);
}
}
// Save the Meta field value
add_action( 'woocommerce_process_shop_order_meta', 'save_custom_metabox_field_value', 10, 2 );
if ( ! function_exists( 'save_custom_metabox_field_value' ) )
{
function save_custom_metabox_field_value( $order_id, $post) {
// Check if our nonce is set.
if ( ! isset($_REQUEST['custom_other_field_nonce']) && ! wp_verify_nonce($_REQUEST['custom_other_field_nonce']) ) {
return $order_id;
}
$order = wc_get_order($order_id); // Get the WC_Order object
// Sanitize text input value and update the meta field in the database.
$order->update_meta_data('_my_field_slug', sanitize_text_field($_POST['my_field_slug']));
$order->save();
}
}
• 产品页面自定义字段(前端)。
• 在购物车、结帐页面和感谢订单(前端)上显示此数据。
• 在订单页面(后端)显示数据
// 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);
}
现在一切都按预期进行。