在WooCommerce中保存并显示自定义字段作为订单商品元数据

时间:2018-01-25 13:22:48

标签: php wordpress woocommerce product custom-fields

在woocommerce中,我有maisd wome自定义,我可以在我的产品中添加自定义文本字段,在购物车中显示值和结帐(请参阅下面的屏幕截图)

产品页面中的文字字段:

enter image description here

购物车中的价值:

enter image description here

结帐时的价值:

enter image description here

但我无法将其显示在购买详情或管理部分中(请参阅下面的屏幕截图)

没有值的结帐详情:

enter image description here

没有值的管理订单:

enter image description here

在我的下面的代码中,有人可以说出我做错了吗?

我在functions.php文件中使用的代码:

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

function my_custom_checkout_field() {
global $product;
$id = $product->get_id();


// Get the field name of InputText1
$InputText1Name = get_post_meta($id, 'InputText1', true);

 if ((!empty(get_post_meta($id, $InputText1, true)))){  
echo '<div id="InputText1">';
    echo '<label>'.__($InputText1Name).'</label> <input type="text" name="$InputText1V">';
    echo '</div>';
 }
}


// Store custom field
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {


if( isset( $_REQUEST['$InputText1V'] ) ) {
    $cart_item_data[ '$InputText1V' ] = $_REQUEST['$InputText1V'];
    /* 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 ){


// Get the product id inside the cart
foreach( WC()->cart->get_cart() as $cart_item ){
$product_id = $cart_item['product_id'];
} 


// Get the field name of InputText1
$InputText1Name = get_post_meta($product_id, 'InputText1', true);

$custom_items = array();
/* Woo 2.4.2 updates */
if( !empty( $cart_data ) ) {
    $custom_items = $cart_data;
}
if( isset( $cart_item['$InputText1V'] ) ) {
    $custom_items[] = array( "name" => $InputText1Name, "value" => $cart_item['$InputText1V'] );
}
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['$InputText1V'] ) ) {
    wc_add_order_item_meta( $product_id, "$InputText1V", $values['$InputText1V'] );
}
}



// 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['$InputText1V']) update_post_meta( $order_id, '$InputText1Name', esc_attr($_POST['$InputText1V']));
}

// 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['$InputText1V']) update_user_meta( $user_id, '$InputText1V', esc_attr($_POST['$InputText1V']) );
}

add_filter( 'woocommerce_hidden_order_itemmeta', 'hide_order_item_meta_fields' );


// 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>'.__($InputText1V).':</strong> ' . get_post_meta( $order_id, $InputText1V, true ) . '</p>';
}


function hide_order_item_meta_fields( $fields ) {
$fields[] = 'current_view';
$fields[] = 'custom_image';//Add all meta keys to this array,so that it will not be displayed in order meta box
return $fields;
}

add_action( 'woocommerce_after_order_itemmeta', 'order_meta_customized_display',10, 3 );

function order_meta_customized_display( $item_id, $item, $product ){


$order_product_id = $item['product_id'];
$field1name = get_post_meta($order_product_id, 'InputText1', true);

echo'<br>';
print_r($InputText1V);
echo'<br>';


echo  $field1name;
echo ': ';

}

1 个答案:

答案 0 :(得分:3)

代码中存在错误和缺失的内容。你不需要所有这些功能,你应该真的避免在自定义字段中使用$ (如果可以capitals slugs key slugs,甚至在函数名称中)代码。

我已经测试并重新访问了您的代码。这是您需要的,可以删除其他所有内容:

// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
    global $product;

    $product_id = $product->get_id();

    // Get the field name of InputText1
    $label = get_post_meta($product_id, 'InputText1', true);

    if( ! empty( $label ) ){
        echo '<div id="InputText1">
            <label>'.$label.':</label> <input type="text" name="custom_slug" value="">
        </div>';
    }
}

// Store custom field label and value in cart item data
add_filter( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
    if( isset( $_REQUEST['custom_slug'] ) ) {
        $cart_item_data['custom_data']['label'] = get_post_meta($product_id, 'InputText1', true);
        $cart_item_data['custom_data']['value'] = sanitize_text_field( $_REQUEST['custom_slug'] );
        $cart_item_data['custom_data']['ukey'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}

// Display items custom fields label and value in cart and checkout pages
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 ){

    $custom_items = array();
    /* Woo 2.4.2 updates */
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }
    if( isset( $cart_item['custom_data'] ) ) {
        $custom_items[] = array(
            'name' => $cart_item['custom_data']['label'],
            'value' => $cart_item['custom_data']['value'],
        );
    }
    return $custom_items;
}


// Save item custom fields label and value as order item meta data
add_action('woocommerce_add_order_item_meta','save_in_order_item_meta', 10, 3 );
function save_in_order_item_meta( $item_id, $values, $cart_item_key ) {
    if( isset( $values['custom_data'] ) ) {
        wc_add_order_item_meta( $item_id, $values['custom_data']['label'], $values['custom_data']['value'] );
    }
}

代码进入活动子主题(或活动主题)的function.php文件。

经过测试和工作。

  

通过这种方式,您将获得订单收到,订单视图,编辑订单(管理员)和电子邮件通知中的显示...