Skip to content

Commit

Permalink
Merge pull request #4 from AashikP/fixes-issue#3
Browse files Browse the repository at this point in the history
Updated to version 1.2.0
  • Loading branch information
AashikP authored Feb 14, 2022
2 parents 3db54b1 + 8f2b4c7 commit 620cc64
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions item-total-for-wc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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 '<th width=100%;">' . 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' ) ) . '</th>';
echo '<th class="item_total_cost sortable" data-sort="float" style="width:100%;text-align:right;">' . esc_html__( 'Item Total', 'item-total-for-wc' )
. '</th>'; // 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 '<td class="item_total_cost">
<div class="view alignright">'
. wc_price( $item_total[ $item_id ], array( 'currency' => $order->get_currency() ) ) // phpcs:ignore
. '</div></td>';
}
$test = '<td class="item_total_cost">';
$test .= '<div class="view alignleft">';
$test .= wc_price( $price, array( 'currency' => $order->get_currency() ) );
$test .= '</span></div></td>';
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' );
}

0 comments on commit 620cc64

Please sign in to comment.