将产品自定义字段添加到 WooCommerce 中的管理产品批量编辑表单
Add a product custom field to Admin product bulk edit form in WooCommerce
我在我的 WooCommerce 产品中添加了一个自定义字段,例如 question/answer:
。
是否可以将此自定义字段添加到产品批量编辑特殊页面(可从管理产品列表页面访问)?
是的,可以为您的自定义字段批量编辑产品 '_text_field'
(如您链接的 question/answer).
您可以在编辑页面的开头或结尾添加此自定义字段。
- 一开始你将使用这个钩子:
woocommerce_product_bulk_edit_start
- 结束本篇:
woocommerce_product_bulk_edit_end
代码(自定义字段在此处开头):
// Add a custom field to product bulk edit special page
add_action( 'woocommerce_product_bulk_edit_start', 'custom_field_product_bulk_edit', 10, 0 );
function custom_field_product_bulk_edit() {
?>
<div class="inline-edit-group">
<label class="alignleft">
<span class="title"><?php _e('T. dostawy', 'woocommerce'); ?></span>
<span class="input-text-wrap">
<select class="change_t_dostawy change_to" name="change_t_dostawy">
<?php
$options = array(
'' => __( '— No change —', 'woocommerce' ),
'1' => __( 'Change to:', 'woocommerce' ),
);
foreach ( $options as $key => $value ) {
echo '<option value="' . esc_attr( $key ) . '">' . $value . '</option>';
}
?>
</select>
</span>
</label>
<label class="change-input">
<input type="text" name="_t_dostawy" class="text t_dostawy" placeholder="<?php _e( 'Enter Termin dostawy', 'woocommerce' ); ?>" value="" />
</label>
</div>
<?php
}
// Save the custom fields data when submitted for product bulk edit
add_action('woocommerce_product_bulk_edit_save', 'save_custom_field_product_bulk_edit', 10, 1);
function save_custom_field_product_bulk_edit( $product ){
if ( $product->is_type('simple') || $product->is_type('external') ){
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
if ( isset( $_REQUEST['_t_dostawy'] ) )
update_post_meta( $product_id, '_text_field', sanitize_text_field( $_REQUEST['_t_dostawy'] ) );
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且可以工作。你会得到这个:
是的,这是可能的,而且没有任何钩子和代码:
- 安装插件 WooCommerce 批量编辑器 (WOOBE):https://wordpress.org/plugins/woo-bulk-editor/
- 转到“元字段”选项卡并在那里添加元键,然后按“保存”按钮
- 在选项卡设置中使用新的元键激活列
- 使用“批量编辑”选项卡进行操作
就这些:)
我在我的 WooCommerce 产品中添加了一个自定义字段,例如 question/answer:
是否可以将此自定义字段添加到产品批量编辑特殊页面(可从管理产品列表页面访问)?
是的,可以为您的自定义字段批量编辑产品 '_text_field'
(如您链接的 question/answer).
您可以在编辑页面的开头或结尾添加此自定义字段。
- 一开始你将使用这个钩子:
woocommerce_product_bulk_edit_start
- 结束本篇:
woocommerce_product_bulk_edit_end
代码(自定义字段在此处开头):
// Add a custom field to product bulk edit special page
add_action( 'woocommerce_product_bulk_edit_start', 'custom_field_product_bulk_edit', 10, 0 );
function custom_field_product_bulk_edit() {
?>
<div class="inline-edit-group">
<label class="alignleft">
<span class="title"><?php _e('T. dostawy', 'woocommerce'); ?></span>
<span class="input-text-wrap">
<select class="change_t_dostawy change_to" name="change_t_dostawy">
<?php
$options = array(
'' => __( '— No change —', 'woocommerce' ),
'1' => __( 'Change to:', 'woocommerce' ),
);
foreach ( $options as $key => $value ) {
echo '<option value="' . esc_attr( $key ) . '">' . $value . '</option>';
}
?>
</select>
</span>
</label>
<label class="change-input">
<input type="text" name="_t_dostawy" class="text t_dostawy" placeholder="<?php _e( 'Enter Termin dostawy', 'woocommerce' ); ?>" value="" />
</label>
</div>
<?php
}
// Save the custom fields data when submitted for product bulk edit
add_action('woocommerce_product_bulk_edit_save', 'save_custom_field_product_bulk_edit', 10, 1);
function save_custom_field_product_bulk_edit( $product ){
if ( $product->is_type('simple') || $product->is_type('external') ){
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
if ( isset( $_REQUEST['_t_dostawy'] ) )
update_post_meta( $product_id, '_text_field', sanitize_text_field( $_REQUEST['_t_dostawy'] ) );
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且可以工作。你会得到这个:
是的,这是可能的,而且没有任何钩子和代码:
- 安装插件 WooCommerce 批量编辑器 (WOOBE):https://wordpress.org/plugins/woo-bulk-editor/
- 转到“元字段”选项卡并在那里添加元键,然后按“保存”按钮
- 在选项卡设置中使用新的元键激活列
- 使用“批量编辑”选项卡进行操作
就这些:)