Support » Plugin: WooCommerce » Issue with shop_order post status

  • I’ve got to update a theme that someone else coded around the time WC 2.1 came out. The impetus for this was a custom function attached to wp_head that is designed to look at the orders submitted and completed to make sure there is sufficient stock to fill the order.

    The line that was triggering a fatal error was as follows:

        $query_args = array(
            'post_status' => 'publish', // <-- this
            'post_type' => 'shop_order',
            'posts_per_page' => -1
        );
    

    Per the instructions published here, I updated the offending line to the following:

    
        $query_args = array(
            'post_status' => array( 'wc-processing', 'wc-completed' ), // <-- updated
            'post_type' => 'shop_order',
            'posts_per_page' => -1
        );
    

    That was enough to fix the issue and get the site up and running for the time being.

    That said, I decided it’s probably time to do a tune-up on the broader theme to make sure the templates were being brought up-to-date and so forth so that another deprecated item doesn’t knock it offline.

    Currently, putting an item in the cart and going to the checkout page ends up with a fatal error in wp_head. Going through things narrows the issue down to that one line again (post-fix!). Commenting it out allows the function to complete and the page loads normally. Leaving it in throws a fatal error and produces a white page.

    While I can’t link to the site for privacy reasons, here is the full code for the function causing the issue:

    
    function bcm_stock_check() {
        $max_orders = (get_option('bcm_max_orders')) ? get_option('bcm_max_orders') : 25;
    
        $args = array(
            'post_status' => array( 'wc-processing', 'wc-completed' ),
            'post_type' => 'shop_order',
            'posts_per_page' => -1
        );
    
        $orders = new WP_Query($args);
    
        $num_orders = 0;
    
        if ($orders->have_posts()):
            while ($orders->have_posts()):$orders->the_post();
                $order_id = get_the_ID();
    
                if ($order_id):
                    $order = new WC_Order($order_id);
    
                    if ($order->status == 'processing'):
                        $num_orders++;
                    endif;
                endif;
            endwhile;
        endif;
    
        if ($num_orders >= $max_orders):
            update_post_meta(21, '_stock_status', 'outofstock'); // outofstock
        endif;
    }
    
    add_action('wp_head', 'bcm_stock_check');
    

    Is there something I’m missing about the order status that’s causing the error?

  • The topic ‘Issue with shop_order post status’ is closed to new replies.