Search code examples
wordpressdrop-down-menuwoocommercee-commerceorders

Custom Order Action in WooCommerce


I am trying to add Custom Order Action in WooCommerce Orders Page.

I want to add two new options in Bulk Order Actions Dropdown in WooCommerce

  1. Mark Refunded
  2. Mark On- Hold

Any help in this regard is highly appreciated.


Solution

  • Here is an example for creating a custom order action working with latest WooCommerce (7.5.1 as of writing)

    add_filter('woocommerce_order_actions', 'my_custom_woocommerce_order_actions', 10, 2);
    add_action('woocommerce_process_shop_order_meta', 'my_custom_woocommerce_order_action_execute', 50, 2);
    
    /**
     * Filter: woocommerce_order_actions
     * Allows filtering of the available order actions for an order.
     *
     * @param array $actions The available order actions for the order.
     * @param WC_Order|null $order The order object or null if no order is available.
     * @since 2.1.0 Filter was added.
     * @since 5.8.0 The $order param was added.
     */
    function my_custom_woocommerce_order_actions($actions, $order)
    {
        $actions['my-custom-order-action'] = __('Execute my custom order action', 'my-custom-order-action');
        return $actions;
    }
    
    /**
     * Save meta box data.
     *
     * @param int $post_id Post ID.
     * @param WP_Post $post Post Object.
     */
    function my_custom_woocommerce_order_action_execute(int $post_id, WP_Post $post)
    {
        if (filter_input(INPUT_POST, 'wc_order_action') !== 'my-custom-order-action') {
            return;
        }
    
        $order = wc_get_order($post_id);
        $order->add_order_note(__('My Custom Order Action was executed', 'my-custom-order-action'));
    }
    
    

    enter image description here