Custom Bulk Actions handler not firing

I’m pretty new to WordPress and PHP and am trying to add a custom field to a bulk edit list for a custom post. I’ve followed the following blog post but my handle_bulk_actions callback is not firing: https://make.wordpress.org/core/2016/10/04/custom-bulk-actions/

Here’s my code:

/**
 * Adds a new item into the Bulk Actions dropdown for custom products 
list.
 */
function register_my_bulk_actions( $bulk_actions ) {
    $bulk_actions['out_of_stock'] = 'Mark as Out of Stock';
    $bulk_actions['in_stock'] = 'Mark as In Stock';
    debug_to_console( 'Hello console' );
    return $bulk_actions;
}
add_filter( 'bulk_actions-edit-custom_product', 'register_my_bulk_actions' );

/**
 * Handles the bulk action above.
 * NOT FIRING!!
 */
function my_bulk_action_handler( $redirect_to, $action, $post_ids ) {
    debug_to_console( 'Running handler' );
    if ( $action !== 'out_of_stock' || $action !== 'in_stock') {
        return $redirect_to;
    }

    // let's remove query args first
    $redirect_to = remove_query_arg( array( 'out_of_stock_done', 'in_stock_done' ), $redirect );

    foreach ( $post_ids as $post_id ) {
        if ($action === 'out_of_stock ') {
            wp_update_post( array(
                'ID' => $post_id,
                'in_stock' => 'no',
            ) );
            $redirect_to = add_query_arg( 'out_of_stock_done', count( $post_ids ), $redirect_to );
        }
        if ($action === 'in_stock ') {
            wp_update_post( array(
                'ID' => $post_id,
                'in_stock' => 'yes',
            ) );
            $redirect_to = add_query_arg( 'in_stock_done', count( $post_ids ), $redirect_to );
        }
    }

    return $redirect_to;
}
add_filter( 'handle_bulk_actions-edit-custom_product', 'my_bulk_action_handler', 10, 3 );

/**


* Shows a notice in the admin once the bulk action is completed.
 */
function my_bulk_action_admin_notice() {
    debug_to_console( 'Running notifier' );
    if ( ! empty( $_REQUEST['bulk_out_of_stock'] ) ) {
        $success_oos = intval( $_REQUEST['bulk_out_of_stock'] );

        printf(
            '<div id="message" class="updated fade">' .
            _n( '%s product updated!', '%s products updated!', $drafts_count, 'domain' )
            . '</div>',
            $success_oos
        );
    }
}
add_action( 'admin_notices', 'my_bulk_action_admin_notice' );

THANKS!

New User Asked on September 19, 2018 in Customizations.
Add Comment
1 Answer(s)

Hi first fix your condition

if ( $action !== 'out_of_stock' || $action !== 'in_stock') {

this condition will always run because your action is in contrast with “out_of_stock” or “in_stock”. So I think if you change the condition from OR to AND it will work.

Then find out if the screen id is the one your looking for, and then modify your filter: to understand what is your screen id, add this to your plugin or your theme function.php, and remember to remove it later:

add_filter( 'wc_product_has_unique_sku', '__return_false' );

add_action('init', 'test_function');
function test_function(){
    $value = get_current_screen();
    echo '<pre>';
    print_r($value);
    echo '</pre>';
}

then when you get the correct screen id, for instance SCREEN_ID, modify your filter like this:

add_filter( 'bulk_actions-SCREEN_ID', 'register_my_bulk_actions' );

 

New User Answered on September 19, 2018.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service. | Learn how to add/edit code.