在WooCommerce中将产品自定义字段添加到管理产品批量修改表单中
问题描述:
我在我的WooCommerce产品中添加了一个自定义字段,例如在此问题/答案中:
Display a custom product field before short description in WooCommerce。在WooCommerce中将产品自定义字段添加到管理产品批量修改表单中
是否有可能这个自定义字段添加到产品批量编辑专页(从管理员的产品列表页面访问)?
答
是有可能大批量编辑产品,为您的自定义字段'_text_field'
(在你的链接提问/回答)。
您可以在编辑页面的开头或结尾处添加此自定义字段。
- 在开始阶段,你会使用这个钩子:
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文件中,或者也在任何插件文件中。
此代码已经过测试并可正常工作。你会得到这个:
+0
谢谢!!! :) – interlaw
你是什么意思的质量? – Reigel
@Reigel我选择了几种产品,我从水平菜单中选择编辑。你看:1. https://ibb.co/jqd0wv - 选择并点击“编辑”按钮。 2 - 我可以一次编辑多个产品 - https://ibb.co/cT0KpF – interlaw
在谷歌搜索'wordpress批量操作tut'。 – Reigel