0

I added two columns and custom fields in quick edit. One based on text and one as dropdown. They work just fine in quick edit and I rely on

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

My script when changed is the following

(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);

And the Function is the following

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' );

My problem is that Update button seems not working, I am confused with variable names and slugs (I believe they are correct)

Why is not closing the bulk edit when update is finished??

The changes are saved by the following instead the above BUT the bulk edit stays open and the update button it does not show the little spinning circle, so I need to refresh the page for the changes to be shown.

// 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['test_column'] ) )
        update_post_meta( $product_id, '_text_field', sanitize_text_field( $_REQUEST['test_column'] ) );

    if ( isset( $_REQUEST['test_column2'] ) )
        update_post_meta( $product_id, '_select', sanitize_text_field( $_REQUEST['test_column2'] ) );
}
}
mujuonly
  • 11,370
  • 5
  • 45
  • 75
Evridiki
  • 323
  • 3
  • 16
  • Note that **products** are a woocommerce **custom post type** not a normal Wordpress **post** type. So you should first search [in this related threads](https://stackoverflow.com/search?q=%5Bwoocommerce%5D+bulk+edit+products). – LoicTheAztec Sep 03 '18 at 10:13
  • You mean the hook is different? – Evridiki Sep 03 '18 at 10:15
  • 1
    For example: https://stackoverflow.com/questions/46051525/add-a-product-custom-field-to-admin-product-bulk-edit-form-in-woocommerce or https://stackoverflow.com/questions/46747572/custom-stock-options-doesnt-show-up-in-bulk-edit-woocommerce-3-2-1 – LoicTheAztec Sep 03 '18 at 10:24
  • I dont need a javascript ? – Evridiki Sep 03 '18 at 10:58
  • If I use example https://stackoverflow.com/questions/46051525/add-a-product-custom-field-to-admin-product-bulk-edit-form-in-woocommerce , its updating BUT the bulk edit is not closing and I need to refresh the page – Evridiki Sep 03 '18 at 11:11
  • 1
    You should need to edit/update your question with the actual **the complete testable code** that you are using (including fields)… – LoicTheAztec Sep 03 '18 at 11:19

0 Answers0