我目前正在向我的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( …Run Code Online (Sandbox Code Playgroud) 我已经在产品页面的常规设置选项卡上的WooCommerce Admin中创建了自定义字段,以便为制造插入一些天.我想在每个产品名称上方的购物车和结帐页面上显示此自定义字段值.
这是我的代码:
// Insert a Custom Admin Field
function woo_add_custom_general_fields() {
echo '<div class="options_group">';
woocommerce_wp_text_input( array(
'id' => 'days_manufacture',
'label' => __( 'Days for Manufacture', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Insert here', 'woocommerce' ),
'type' => 'number',
'custom_attributes' =>
array(
'step' => 'any',
'min' => '1'
)
) );
echo '</div>';
}
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save field
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_number_field = $_POST['days_manufacture'];
if( !empty( $woocommerce_number_field ) )
update_post_meta( $post_id, 'days_manufacture', esc_attr( $woocommerce_number_field …Run Code Online (Sandbox Code Playgroud) 我需要为订单商品添加自定义列,并在此列中显示特定的产品元数据。我的意思是如下图所示,我找不到woocommerce的任何操作来添加此列!
