From 8f2b4c77dc79b26699c5e30517a5e2ee4494a7d0 Mon Sep 17 00:00:00 2001
From: AashikP <17475174+AashikP@users.noreply.github.com>
Date: Mon, 14 Feb 2022 20:51:32 +0530
Subject: [PATCH] Updated to version 1.2.0
* Update - Add support `shipping` line items
* Update - Add support `fee` line items
* Update - Item total under product line items is now sortable
* Fix - Item total in an order is doubled #3
* Tweak - Removed the tooltip from order details page
* Tweak - Item total is now next to te cost of the product for consistency sake
---
item-total-for-wc.php | 68 ++++++++++++++++++++++++++++---------------
1 file changed, 44 insertions(+), 24 deletions(-)
diff --git a/item-total-for-wc.php b/item-total-for-wc.php
index ff9bbf7..215012a 100644
--- a/item-total-for-wc.php
+++ b/item-total-for-wc.php
@@ -2,14 +2,14 @@
/**
* Plugin Name: Item total for WooCommerce
* Description: Simple plugin to display Item total in the order details page.
- * Version: 1.0.1
+ * Version: 1.2.0
* Author: AashikP
* Author URI: https://aashikp.com
* Text Domain: item-total-for-wc
- * Requires at least: 4.9.14
- * Requires PHP: 7.3.5
+ * Requires at least: 5.0
+ * Requires PHP: 7.4
* WC requires at least: 3.5.0
- * WC tested up to: 5.3.0
+ * WC tested up to: 6.2.0
*
* @package Item-Total-for-WC
*/
@@ -21,40 +21,60 @@
/**
* Check if WooCommerce is active
*/
-if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
+if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) {
/**
* Display tax included price under order details page.
*/
function ap_item_total_header() {
- echo '
' . esc_html__( 'Item Total', 'item-total-for-wc' ) . wc_help_tip( esc_html__( 'Item total = [Cost * Qty]+Tax total. This is the actual product total paid by the customer, and Cost = Price of the product minus Discounts (if any)', 'item-total-for-wc' ) ) . ' | ';
+ echo '' . esc_html__( 'Item Total', 'item-total-for-wc' )
+ . ' | '; // phpcs:ignore
}
add_action( 'woocommerce_admin_order_item_headers', 'ap_item_total_header' );
/**
- * Display the total tax for a line item.
+ * Calculate the total price for a line item.
*
- * @param object $product Product object.
+ * @param Object $order Order object.
+ * @param String $item_type Type of line item. line_item, shipping, or fee.
*/
- function ap_display_item_total( $product ) {
- global $post;
- if ( ! is_int( $thepostid ) ) {
- $thepostid = $post->ID;
+ function ap_calculate_item_total( $order, $item_type = 'line_item' ) {
+ $item_total = array();
+ foreach ( $order->get_items( $item_type ) as $item_id => $item ) {
+ $price = ( $item['total'] + $item['total_tax'] );
+ $item_total[ $item_id ] = $price;
}
+ return $item_total;
+ }
+
+ /**
+ * Display the total price for a line item.
+ *
+ * @param Int $item_id Order item id.
+ */
+ function ap_display_item_total( $item_id ) {
+ global $post;
+ $order = wc_get_order( $post->ID );
if ( ! wp_doing_ajax() ) {
- $order = wc_get_order( $thepostid );
- $order_items = $order->get_items();
- foreach ( $order->get_items() as $item_id => $item ) {
- if ( $item['product_id'] === $product->id ) {
- $price = $item['total_tax'] + $item['total'];
- }
+ if ( isset( $order->get_items()[ $item_id ] ) ) {
+ $item_total = ap_calculate_item_total( $order, $item_type = 'line_item' );
+ }
+ if ( ! empty( $order->get_items( 'fee' )[ $item_id ] ) ) {
+ $item_total = ap_calculate_item_total( $order, $item_type = 'fee' );
+ }
+ if ( ! empty( $order->get_items( 'shipping' )[ $item_id ] ) ) {
+ $item_total = ap_calculate_item_total( $order, $item_type = 'shipping' );
+ }
+
+ if ( isset( $item_total[ $item_id ] ) ) {
+ echo '
+ '
+ . wc_price( $item_total[ $item_id ], array( 'currency' => $order->get_currency() ) ) // phpcs:ignore
+ . ' | ';
}
- $test = '';
- $test .= ' ';
- $test .= wc_price( $price, array( 'currency' => $order->get_currency() ) );
- $test .= ' | ';
- echo $test;
+ return;
}
}
- add_action( 'woocommerce_admin_order_item_values', 'ap_display_item_total' );
+ add_action( 'woocommerce_after_order_fee_item_name', 'ap_display_item_total' );
+ add_action( 'woocommerce_after_order_itemmeta', 'ap_display_item_total' );
}