I am trying to get a total for all discounts (in dollar amount) received by the logged in user to display in the Dashboard.
I aim to display a notice saying something like 'You have saved $XXX while shopping with us!', however the below code is not working. It just displays the total amount of orders the user has made.
I am looking for a PHP solution and hopefully to minimise the need to access MySQL as I am not very experienced with that language yet.
global $woocommerce;
$customer = wp_get_current_user();
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types(),
'post_status' => array('wc-completed', 'wc-processing'), //'post_status' => array_keys( wc_get_order_statuses() ),
) );
$total = 0;
if (!empty($customer_orders)) {
foreach ( $customer_orders as $customer_order ){
$order = new WC_Order( $customer_order->ID );
$total += $order->get_total_discount();
}
}
echo count( $total );