• Hello,

    I need to create a custom field that substracts an existing custom field from an existing order total field. How would I achieve that?

    The result would be :

    [custom_field] = [order_subtotal_minus_discount] – [ecopart]

    I tried using a calculated order field, but sadly it did not work.

    Thanks a lot for the help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author algol.plus

    (@algolplus)

    hello

    please share your PHP code

    Thread Starter Julien Vandermeersch

    (@jvkonfi)

    I did not get very far using PHP since I’m a novice, but, to start, I tried getting the order total and it threw me an error:

    add_filter('woe_get_order_value_order_subtotal_minus_discount_and_ecotax',function ($value, $order, $fieldname) {
    $value = $order->get_line_subtotal();
    return $value;
    },10,3);

    That’s why I was wondering if it was possible to substract one field from another? It would be much simpler this way, since the fields already exists on the order export.

    Plugin Author algol.plus

    (@algolplus)

    $order is https://woocommerce.github.io/code-reference/classes/WC-Order.html

    so you should use

    add_filter('woe_get_order_value_order_subtotal_minus_discount_and_ecotax',function ($value, $order, $fieldname) {
    $value = $order->get_subtotal() - $order->get_discount_total() - $order->get_meta("ecopart");
    return $value;
    },10,3);

    • This reply was modified 3 years, 1 month ago by algol.plus.
    • This reply was modified 3 years, 1 month ago by algol.plus.
    Thread Starter Julien Vandermeersch

    (@jvkonfi)

    That did the trick! Thank you 🙂

    Plugin Author algol.plus

    (@algolplus)

    You’re welcome

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Calculated custom field question’ is closed to new replies.