我按照下面的解决方案将自定义字段添加到批量编辑器。它有效,自定义字段_min_order_field
正在批量编辑。我遇到的问题是,当另一个字段被批量编辑(并且自定义字段保持不变)时,自定义字段中的值将被删除。例如,如果我批量编辑价格,则自定义字段中的值会消失。这就是我所拥有的,朝着正确方向的任何指针将不胜感激。
这是我遵循的指南: Add a product custom field to Admin product bulk edit form in WooCommerce
//Add the minimum order field to woocomerce product bulk edit
add_action( 'woocommerce_product_bulk_edit_start', 'minimum_order_field_bulk_edit', 10, 0 );
function minimum_order_field_bulk_edit() {
?>
<div class="inline-edit-group">
<label class="alignleft">
<span class="title"><?php _e('Minimum', 'woocommerce'); ?></span>
<span class="input-text-wrap">
<select class="change_minimumo change_to" name="change_minimumo">
<?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="_minimumo" class="text minimumo" placeholder="<?php _e( 'Enter minimum order', 'woocommerce' ); ?>" value="" />
</label>
</div>
<?php
}
// Save the minimum order fields data when submitted for product bulk edit
add_action('woocommerce_product_bulk_edit_save', 'save_minimum_order_field_bulk_edit', 10, 1);
function save_minimum_order_field_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['_minimumo'] ) )
update_post_meta( $product_id, '_min_order_field', sanitize_text_field( $_REQUEST['_minimumo'] ) );
}
}
答案 0 :(得分:0)
您的代码中有一个小错误。在if条件下,您可以使用isset()
函数检查该值。在这种情况下,您始终为true,并使用空字符串更新值。但是,如果使用!empty()
函数检查该值,则如果输入为空,则将得到false,因此将保留以前保存的数据。
所以您的代码应该是这样的:
if ( !empty( $_REQUEST['_minimumo'] ) )
update_post_meta( $product_id, '_min_order_field', sanitize_text_field( $_REQUEST['_minimumo'] ) );