• Resolved cirrus123

    (@cirrus123)


    I cannot for the life of me figure out why this is happening, but it did not happen on older version of WooCommerce.

    I have a custom order status that I create using the following code:

    function register_awaiting_shipment_order_status() {
        register_post_status( 'wc-shipment', array(
            'label'                     => 'Preparing Shipment',
            'public'                    => true,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop( 'Preparing Shipment <span class="count">(%s)</span>', 'Preparing Shipment <span class="count">(%s)</span>' )
        ) );
    }
    add_action( 'init', 'register_awaiting_shipment_order_status' );
    
    // Add to list of WC Order statuses
    function add_awaiting_shipment_to_order_statuses( $order_statuses ) {
    
        $new_order_statuses = array();
    
        // add new order status after processing
        foreach ( $order_statuses as $key => $status ) {
    
            $new_order_statuses[ $key ] = $status;
    
            if ( 'wc-processing' === $key ) {
                $new_order_statuses['wc-shipment'] = 'Preparing Shipment';
            }
        }
    
        return $new_order_statuses;
    }
    add_filter( 'wc_order_statuses', 'add_awaiting_shipment_to_order_statuses' );

    And if I go into an order and manually change it to ‘Preparing Shipment’, there is no issue and everything works as expected.

    However, I have a button that I have coded to update the status programmatically via AJAX using this:

    $order = new WC_Order($orderid);
    if (!$order) exit();
    $order->update_status('wc-shipment');

    It all works, but when it is done programmatically, the order vanishes from the orders list, and I can’t even search for it by the exact order number. The only way to access that order is to manually enter the order ID in the URL to edit it. When editing it, the order status has been successfully updated to ‘Preparing Shipment’. Then, if I manually save/update the order without changing anything, it shows up in the order list fine.

    Finally, if I run the exact same update_status code for a built in WooCommerce status, it all works as expected.

    So what is it about the way I am updating or creating the custom status that is somehow missing some step that allows WooCommerce to notice it? This never happened in older versions, and I think it stopped working once WooCommerce 4 came out.

    Any advice would be greatly appreciated, as this is preventing me from being able to update stuff.

    • This topic was modified 2 years, 7 months ago by cirrus123.
    • This topic was modified 2 years, 7 months ago by cirrus123.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Kaushik S. a11n

    (@kaushiksomaiya)

    Hi there!

    Thank you for contacting us! ??

    I understand the code you’ve put for updating the order status is not working.

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I found this question here: https://stackoverflow.com/questions/22935358/woocommerce-change-order-status-with-php-code, some of the users have described alternative ways to update order status on newer versions of WooCommerce, you can give it a try.

    You can observe the Ajax call on browser console to see if the call is successfully returning a 2xx response.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Thread Starter cirrus123

    (@cirrus123)

    The AJAX call is definitely working, as it is updating the status. The issue is that it is not showing up in the order list if it is a custom status that has been updated programmatically.

    I am beginning to perhaps diagnose where the issue may lie.

    It relates to the actual status name:

    1) If I create a custom order status in WooCommerce that does not have the “wc-” prefix, it does not work as expected – when I try to manually update the order status to the custom status via the order edit page, it does not update.

    2) If I do use the “wc-” prefix, I can manually update the order status. However, if I try to use the $order->has_status function, I need to remove the “wc-” in order to get a positive result.

    So my initial hypothesis is that the update_status function that is called programmatically is somehow different than the one called when the order is updated via the order edit page, and this is where an issue is arising. It has something to do with the wc- prefix, maybe.

    $order->set_status('shipment');
    $order->save();

    or

    $order->set_status('wc-shipment');
    $order->save();

    have exact same issue, as does:

    $order->update_status('shipment');

    • This reply was modified 2 years, 7 months ago by cirrus123.
    Plugin Support con

    (@conschneider)

    Engineer

    Hi again,

    So my initial hypothesis is that the update_status function that is called programmatically is somehow different than the one called when the order is updated via the order edit page, and this is where an issue is arising. It has something to do with the wc- prefix, maybe

    .

    I was about to suggest $order->save(); when I saw you already are trying this.

    However, if I try to use the $order->has_status function, I need to remove the “wc-” in order to get a positive result.

    Just to be clear. This means that:

    $order->set_status('shipment');
    $order->save();

    works, correct?

    I also did a quick search and found this: https://rudrastyh.com/woocommerce/order-statuses.html – maybe it is helpful.

    Kind regards,

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom status orders do not show in Orders list’ is closed to new replies.