Hassan
Hassan

Reputation: 110

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.

Upvotes: 8

Views: 10867

Answers (2)

Adrian
Adrian

Reputation: 2388

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

Upvotes: 3

Andrew
Andrew

Reputation: 311

There are two parts to solve with this objective.

The first part is to get a custom Order Action in the metabox of the individual Orders Page. I was trying to accomplish the same thing, but didn't find anything definitive on it, so I created a tutorial here:

http://neversettle.it/add-custom-order-action-woocommerce/

The second part is to add a custom order action in the Bulk Actions drop-down of the main Orders page. Skyverge has an excellent tutorial on that here:

http://www.skyverge.com/blog/add-custom-bulk-action/

The only specific thing you will need to take note of is to use the correct post_type. For WooCommerce orders you will need to use this in place of the first example on that tutorial:

add_action('admin_footer-edit.php', 'custom_bulk_admin_footer');

function custom_bulk_admin_footer() {

    global $post_type;

    if($post_type == 'shop_order') {
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery('<option>').val('export').text('<?php _e('Export')?>').appendTo("select[name='action']");
            jQuery('<option>').val('export').text('<?php _e('Export')?>').appendTo("select[name='action2']");
        });
    </script>
    <?php
    }
}

Notice the shop_order replaces the post for the condition checking which post_type to add the bulk actions to.

But fundamentally, @brasofilo is correct - for the most part WooCommerce uses standard WordPress structures, post_type mechanisms, and taxonomies. The process is the same for adding a bulk action to the Orders page as it is to the Posts page.

However, you are correct about the custom Order Actions on the individual Orders page - that is WooCommerce only and you'll need to reference the first tutorial on how to solve that part.

Upvotes: 9

Related Questions