我遵循这个快速/布局良好的wordpress教程,为WooCommerce变体添加自定义字段。
http://www.remicorson.com/woocommerce-custom-fields-for-variations/
一切都很好。。。
但是,将字段插入仪表板(产品编辑)区域的方法没有提到how to change the order of the field as seen in the dashboard..
// Add Variation Settings
add_action( \'woocommerce_product_after_variable_attributes\', \'variation_settings_fields\', 10, 3 );
// Save Variation Settings
add_action( \'woocommerce_save_product_variation\', \'save_variation_settings_fields\', 10, 2 );
function variation_settings_fields( $loop, $variation_data, $variation ) {
// Text Field
woocommerce_wp_text_input(
array(
\'id\' => \'_text_field[\' . $variation->ID . \']\',
\'label\' => __( \'My Text Field\', \'woocommerce\' ),
\'placeholder\' => \'http://\',
\'desc_tip\' => \'true\',
\'description\' => __( \'Enter the custom value here.\', \'woocommerce\' ),
\'value\' => get_post_meta( $variation->ID, \'_text_field\', true )
)
);
}
function save_variation_settings_fields( $post_id ) {
// Text Field
$text_field = $_POST[\'_text_field\'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, \'_text_field\', esc_attr( $text_field ) );
}
}
任何帮助都将不胜感激!
最合适的回答,由SO网友:user141080 整理而成
变体的元框模板在文件中定义html-variation-admin.php. 在那里,您可以找到钩子“woocommerce\\u variation\\u options\\u pricing”。使用这个钩子,您可以将输入字段添加到所需的位置。
function variation_settings_fields($loop, $variation_data, $variation)
{
// Your Text Field
woocommerce_wp_text_input(
array(
\'id\' => \'_text_field[\' . $variation->ID . \']\',
\'label\' => __(\'My Text Field\', \'woocommerce\'),
\'placeholder\' => \'http://\',
\'wrapper_class\' => \'form-row\',
\'desc_tip\' => \'true\',
\'description\' => __(\'Enter the custom value here.\', \'woocommerce\'),
\'value\' => get_post_meta($variation->ID, \'_text_field\', true)
)
);
}
add_action(\'woocommerce_variation_options_pricing\',\'variation_settings_fields\');