Custom Order Action in WooCommerce
Asked Answered
F

2

8

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.

Fontainebleau answered 2/6, 2014 at 12:44 Comment(2)
possible duplicate of How to add custom bulk actions in WordPress list tables?Fraunhofer
It seems to be but not really. WooCommerce uses different structure.Fontainebleau
F
3

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

Ferrigno answered 31/3, 2023 at 14:49 Comment(0)
W
9

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.

Whaling answered 11/8, 2014 at 19:30 Comment(2)
It seems that it will use built in action to export, but what if i want to append other actions? How Refund and On-Hold status emails will be triggered once a bulk action is applied?Fontainebleau
This method still works, but is slightly outdated - Order props should be accessed using getter methods, such as $order->get_id();Fancyfree
F
3

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

Ferrigno answered 31/3, 2023 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.