3
votes

I'm looking to loop over all the products added to an order in the admin before the order is actually submitted. So far the only WooCommerce hooks I have found only allow you access the product items individually.

I was looking for a hook that would fire when a user clicks the recalculate button but actually it could trigger when a user adds a product, tax, shipping method, etc.. I just need to loop over all items added to the order so far.

At the moment I'm using woocommerce_admin_order_item_values hook but it's a self-contained loop so doesn't allow me to add all my '$item['product_id']' together.

function action_woocommerce_admin_order_item_values( $null, $item, $absint ) { 
$item_ids = array($item['product_id']);
}
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );

You can also use - woocommerce_before_order_itemmeta Hook but this only accesses each item individually whereas I need to loop over each item in the summary.

1

1 Answers

7
votes

There are number of hooks provided by WooCommerce, when you are clicking the Recalculate button. I'm listing here those hooks and it depends on you to choose among them according to your requirement.

$order = WC_Order object

    add_action("woocommerce_order_before_calculate_taxes", "custom_order_before_calculate_taxes", 10, 2);
    function custom_order_before_calculate_taxes($args, $order) {
        // Do something
    }

    add_action("woocommerce_order_item_after_calculate_taxes", "custom_order_item_after_calculate_taxes", 10, 2);
    function custom_order_item_after_calculate_taxes($order, $calculate_tax_for) {
       // Do something
    }

    add_action("woocommerce_before_order_object_save", "custom_before_order_object_save", 10, 2);
    function custom_before_order_object_save($order, $data_store) {
      // Do something
    }

    add_action( 'woocommerce_order_before_calculate_totals', "custom_order_before_calculate_totals", 10, 2);
    function custom_order_before_calculate_totals($and_taxes, $order ) {
      // Do something
    }
    add_action( 'woocommerce_order_after_calculate_totals', "custom_order_after_calculate_totals", 10, 2);
    function custom_order_after_calculate_totals($and_taxes, $order) {
      //Do something
    }

    add_filter("woocommerce_order_is_vat_exempt", function(){
       return $boolean;
    });

    add_filter("woocommerce_order_get_total", "custom_order_get_total", 10, 2);
    function custom_order_get_total($value, $order) {
      //do somethig
      return $value;
    }