Reputation: 13
I have made a copy of the Simple invoice(this is the default), and created a new one, the problem I have is that I can't cast the string value to float. Here is the code:
<?php foreach( $this->get_woocommerce_totals() as $key => $total ) : ?>
<tr class="<?php echo $key; ?>">
<td class="no-borders"></td>
<th class="description"><?php echo $total['label']; ?></th>
<td class="price"><span class="totals-price"><?php echo $total['value']; ?></span></td>
</tr>
<?php endforeach; ?>
the $total['value'];
displays the price in a string format.
I tried using (float)$total['label']
and floatval($total['label'])
to cast it but did not work, It gives back a value of 0.
Upvotes: 1
Views: 3084
Reputation: 254363
You can't really get that, but you can use the $key
to target the the total line type
and you can use $this->order
that is the instance of the WC_Order
object with any WC_Order
method.
To get the gran total unformatted amount (including taxes) you will use $this->order->get_total()
…
Some of the available
$keys
are:
cart_subtotal
discount
(if there is coupons applied)shipping
fee_1234
(if there is a fee and where1234
is the item ID… they can be many different)tax
(depending on how taxes are displayed on your settings)payment_method
(it seems hidden by some CSS)order_total
So in this code:
<?php
// The line totals data array (in a variable)
$wc_totals = $this->get_woocommerce_totals();
// Some order unformatted amounts
$order_total = $this->order->get_total();
$order_total_tax = $this->order->get_total_tax();
## Manipulating amounts (example) ##
// Order Total (rounding total amount)
$wc_totals['order_total']['value'] = strip_tags( wc_price( round( $order_total ) ) );
?>
<?php foreach( $wc_totals as $key => $total ) : ?>
<tr class="<?php echo $key; ?>">
<td class="no-borders"></td>
<th class="description"><?php echo $total['label']; ?></th>
<td class="price"><span class="totals-price"><?php echo $total['value']; ?></span></td>
</tr>
<?php endforeach; ?>
Tested and works.
You can also add additional totals lines or remove them with unset() and the correct key…
To add a line after the shipping total line (example):
<?php
// The line totals data array (in a variable)
$wc_totals = $this->get_woocommerce_totals();
$wc_new_totals = []; // Initialising
foreach ( $wc_totals as $key => $total ) {
$wc_new_totals[$key] = $total;
if ( $key === 'shipping' ) {
$wc_new_totals['shipping_avg'] = [
'label' => __( "Shipping average" ),
'value' => __( "15 days" ),
];
}
}
?>
<?php foreach( $wc_new_totals as $key => $total ) : ?>
<tr class="<?php echo $key; ?>">
<td class="no-borders"></td>
<th class="description"><?php echo $total['label']; ?></th>
<td class="price"><span class="totals-price"><?php echo $total['value']; ?></span></td>
</tr>
<?php endforeach; ?>
Upvotes: 0