Answer a question

I would like to move the "Discount:" field below the Shipping field in Order details. I also want this to reflect in woocommerce emails as well.

Please see the attached image for better understanding.

Order details screenshot

I tried to find the file in the woocommerce plugin to make the changes, but there were so many entries as Discount, it totally confused me. Any help would be appreciated. Hopefully a child theme solution would be a blessing. Thanks in advance.

Answers

Code with inline explanation:

/**
 * Filter to rearrange the order details template's footer part.
 */
function reordering_order_item_totals( $total_rows, $wc_order, $tax_display ) {

    $total_rows = move_key_before( $total_rows, 'discount', 'shipping' );

    return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'reordering_order_item_totals', 10, 3 );

/**
 * Rearrange associative array.
 *
 * @see https://codereview.stackexchange.com/a/166491
 *
 * @param array  $arr  Associative array.
 * @param string $find Key of the element to be found out.
 * @param string $move Key of the element to be moved before found element.
 */
function move_key_before( $arr, $find, $move ) {
    if ( ! isset( $arr[ $find ], $arr[ $move ] ) ) { // Check both keys exists.
        return $arr;
    }

    $elem  = array( $move => $arr[ $move ] ); // Cache the element to be moved.
    $start = array_splice( $arr, 0, array_search( $find, array_keys( $arr ), true ) ); // Chunked array holds elements before the $move element.
    unset( $start[ $move ] );  // Only important if $move is in $start.
    return $start + $elem + $arr; // Utilized union operator: https://www.php.net/manual/en/language.operators.array.php click the link to know more.
}

Before:

Before modification


After:

After modification

Logo

WooCommerce社区为您提供最前沿的新闻资讯和知识内容

更多推荐