我跟随Misha Rudrastyh的这个教程添加“自定义字段在WordPress快速编辑”功能。我已经成功地完成了教程的大部分内容(步骤1,2和3),但在最后一步4遇到了麻烦,这涉及到添加一个脚本来填充字段。
add_action( 'admin_footer', 'min_max_step_desc_quick_edit_populate');
function min_max_step_desc_quick_edit_populate(){
?>
<script>
jQuery( function($){
console.log('min max step code is enabled');
const wp_inline_edit_function = inlineEditPost.edit;
// we overwrite the it with our own
inlineEditPost.edit = function( post_id ) {
// let's merge arguments of the original function
wp_inline_edit_function.apply( this, arguments );
// get the post ID from the argument
if ( typeof( post_id ) == 'object' ) { // if it is object, get the ID number
post_id = parseInt( this.getId( post_id ) );
}
// add rows to variables
const edit_row = $( '#edit-' + post_id )
const post_row = $( '#post-' + post_id )
const minQty = $( '.column-min', post_row ).text().substring() // remove $ sign
const maxQty = $( '.column-max', post_row ).text().substring() // remove $ sign
const stepQty = $( '.column-step', post_row ).text().substring() // remove $ sign
const productDesc = $( '.column-desc', post_row ).text().substring() // remove $ sign
// populate the inputs with column data
$( ':input[name="min"]', edit_row ).val( minQty );
$( ':input[name="max"]', edit_row ).val( maxQty );
$( ':input[name="step"]', edit_row ).val( stepQty );
$( ':input[name="desc"]', edit_row ).val( productDesc );
}
});
</script>
<?php
}
我在最后几行中不断得到以下错误
- 错误,意外的'min'(T_STRING),期望';'或','. *
我做错了什么?
编辑*(所有其他工作相关功能)*:
// add new columns
add_filter( 'manage_edit-product_columns', 'min_max_step_desc_columns' );
// the above hook will add columns only for default 'post' post type, for CPT:
// manage_{POST TYPE NAME}_posts_columns
function min_max_step_desc_columns( $column_array ) {
$column_array[ 'min' ] = 'Min';
$column_array[ 'max' ] = 'Max';
$column_array[ 'step' ] = 'Step';
$column_array[ 'desc' ] = 'Description';
// the above code will add columns at the end of the array
// if you want columns to be added in another order, use array_slice()
return $column_array;
}
// Populate our new columns with data
add_action( 'manage_product_posts_custom_column', 'populate_custom_columns', 10, 2 );
function populate_custom_columns( $column_name, $post_id ) {
// if you have to populate more that one columns, use switch()
switch( $column_name ) {
case 'min': {
$min = get_post_meta( $post_id, '_alg_wc_pq_min', true );
echo $min;
break;
}
case 'max': {
$max = get_post_meta( $post_id, '_alg_wc_pq_max', true );
echo $max;
break;
}
case 'step': {
$step = get_post_meta( $post_id, '_alg_wc_pq_step', true );
echo $step;
break;
}
case 'desc': {
echo get_post_field( 'post_content', $post_id );
break;
}
}
}
// quick_edit_custom_box allows to add HTML in Quick Edit
add_action( 'quick_edit_custom_box', 'mix_max_step_desc_quick_edit_fields', 10, 2 );
function mix_max_step_desc_quick_edit_fields( $column_name, $post_type ) {
switch( $column_name ) {
case 'min': {
?>
<fieldset class="inline-edit-col-left">
<div class="inline-edit-col">
<label>
<span class="title">Min</span>
<input type="text" name="min">
</label>
</div>
<?php
break;
}
case 'max': {
?>
<div class="inline-edit-col">
<label>
<span class="title">Max</span>
<input type="text" name="max">
</label>
</div>
<?php
break;
}
case 'step': {
?>
<div class="inline-edit-col">
<label>
<span class="title">Step</span>
<input type="text" name="step">
</label>
</div>
<?php
break;
}
case 'desc': {
?>
<div class="inline-edit-col">
<label>
<span class="title">Description</span>
<input type="text" name="desc">
</label>
</div>
</fieldset>
<?php
break;
}
}
}
// save fields after quick edit
add_action( 'save_post', 'min_max_step_desc_quick_edit_save' );
function min_max_step_desc_quick_edit_save( $post_id ){
// check inlint edit nonce
if ( ! wp_verify_nonce( $_POST[ '_inline_edit' ], 'inlineeditnonce' ) ) {
return;
}
// update the price
$min = ! empty( $_POST[ 'min' ] ) ? absint( $_POST[ 'min' ] ) : '';
update_post_meta( $post_id, '_alg_wc_pq_min', $min );
$max = ! empty( $_POST[ 'max' ] ) ? absint( $_POST[ 'max' ] ) : '';
update_post_meta( $post_id, '_alg_wc_pq_max', $max );
$step = ! empty( $_POST[ 'step' ] ) ? absint( $_POST[ 'step' ] ) : '';
update_post_meta( $post_id, '_alg_wc_pq_step', $step );
$desc = ! empty( $_POST[ 'desc' ] ) ? absint( $_POST[ 'desc' ] ) : '';
update_post_meta( $post_id, 'post_content', $desc );
}
标签:Add custom fields to admin products Bulk Edit in WooCommerce 3.2+
1条答案
按热度按时间zf9nrax11#
更新:
您并不需要将这些字段值作为附加列添加,就可以通过jQuery使用它们来填充产品“快速编辑”功能中的表单字段。
相反,我们可以添加字段值作为隐藏数据,以填充快速编辑中的字段。此外,在WooCommerce中有专用的钩子来显示和保存快速编辑自定义字段。
代码:
代码放在子主题的functions.php文件中(或插件中)。测试和作品。
要在快速编辑中显示开头的字段,请使用:用途:
woocommerce_product_quick_edit_start
而不是:
woocommerce_product_quick_edit_end
如果您仍然希望在其他列中显示数据,请添加以下内容: