从WordPress中的自定义元字段更新批量修改值不会关闭批量修改

时间:2018-09-03 09:52:52

标签: javascript php ajax wordpress woocommerce

我在快速编辑中添加了两列和自定义字段。一种基于文本,另一种作为下拉列表。它们在快速编辑中工作得很好,我依靠

https://github.com/bamadesigner/manage-wordpress-posts-using-bulk-edit-and-quick-edit/blob/master/bulk_quick_edit.js

更改后的脚本如下

select(year, Total_US_received, Total_US_required)

函数如下

(function($) {

// we create a copy
var $wp_inline_edit = inlineEditPost.edit;

//overwrite the function
inlineEditPost.edit = function( id ) {


    $wp_inline_edit.apply( this, arguments );


    // get the post ID
    var $post_id = 0;
    if ( typeof( id ) == 'object' )
        $post_id = parseInt( this.getId( id ) );

    if ( $post_id > 0 ) {

        // define the edit row
        var $edit_row = $( '#edit-' + $post_id );

        // get the sku
        var $bfsku = $( '#_text_field-' + $post_id ).text();

        // set the sku
        $edit_row.find( 'input[name="test_column"]' ).val( $bfsku );

        // get the supplier
        var $supplier = $( '#_select-' + $post_id ).text();

        // set the supplier
        $edit_row.find( 'select[name="test_column2"]' ).val( $supplier );

    }

};






$( '#bulk_edit' ).live( 'click', function() {

    // define the bulk edit row
    var $bulk_row = $( '#bulk-edit' );

    // get the selected post ids that are being edited
    var $post_ids = new Array();
    $bulk_row.find( '#bulk-titles' ).children().each( function() {
        $post_ids.push( $( this ).attr( 'id' ).replace( /^(ttle)/i, '' ) );
    });

    // get the custom fields
    var $bfsku = $bulk_row.find( 'input[name="test_column"]' ).val();

    var $supplier = $bulk_row.find( 'select[name="test_column2"]' ).val();

    // save the data
    $.ajax({
        url: ajaxurl, // this is a variable that WordPress has already defined for us
        type: 'POST',
        async: false,
        cache: false,
        data: {
            action: 'manage_wp_posts_using_bulk_quick_save_bulk_edit', // this is the name of our WP AJAX function that we'll set up next
            post_ids: $post_ids, // and these are the 2 parameters we're passing to our function
            test_column: $bfsku,
            test_column2: $supplier
        }
    });

});

})(jQuery);

我的问题是“更新”按钮似乎不起作用,我对变量名和子句感到困惑(我相信它们是正确的)

为什么更新完成后不关闭批量编辑?

更改将通过以下内容保存,而不是上面的内容,但批量编辑将保持打开状态,并且更新按钮不会显示旋转的小圆圈,因此,我需要刷新页面以显示更改。

function manage_wp_posts_using_bulk_quick_save_bulk_edit() {
// we need the post IDs
$post_ids = ( isset( $_POST[ 'post_ids' ] ) && !empty( $_POST[ 'post_ids' ] ) ) ? $_POST[ 'post_ids' ] : NULL;

// if we have post IDs
if ( ! empty( $post_ids ) && is_array( $post_ids ) ) {

    // get the custom fields
    $custom_fields = array( 'test_column', 'test_column2' );

    foreach( $custom_fields as $field ) {

        // if it has a value, doesn't update if empty on bulk
        if ( isset( $_POST[ $field ] ) && !empty( $_POST[ $field ] ) ) {

            // update the price
            if ( isset( $_POST['test_column'] ) ) {
                update_post_meta( $post_id, '_text_field', $_POST['test_column'] );

            }

            // update the price
            if ( isset( $_POST['test_column2'] ) ) {
                update_post_meta( $post_id, '_select', $_POST['test_column2'] );

            }

        }

    }

}

}
add_action( 'wp_ajax_manage_wp_posts_using_bulk_quick_save_bulk_edit', 'manage_wp_posts_using_bulk_quick_save_bulk_edit' );

0 个答案:

没有答案