I'am trying to use woocommerce_get_order_item_totals
filter hook to add some extra information into the order totals (on orders and email notifications).
add_filter( 'woocommerce_get_order_item_totals', 'display_email_topay', 10, 2 );
function display_email_topay( $total_rows, $order ) {
$total_rows['recurr_not'] = array(
'label' => __( '2nd Installment:', 'woocommerce' ),
'value' => 'DATA NEEDS TO GO HERE' // <===
);
return $total_rows;
}
I have access to $order
which contains the data I want to display but I have no idea how to get it.
Below is a var_dump
(formatted) of $order
variable:
Automattic\WooCommerce\Admin\Overrides\Order Object
(
[refunded_line_items:protected] =>
[status_transition:protected] =>
[data:protected] => Array
(
[parent_id] => 0
[status] => on-hold
[currency] => GBP
[version] => 4.5.2
[prices_include_tax] =>
[date_created] => WC_DateTime Object
(
[utc_offset:protected] => 0
[date] => 2020-10-04 18:25:38.000000
[timezone_type] => 3
[timezone] => Europe/London
)
[date_modified] => WC_DateTime Object
(
[utc_offset:protected] => 0
[date] => 2020-10-04 18:25:38.000000
[timezone_type] => 3
[timezone] => Europe/London
)
[discount_total] => 0
[discount_tax] => 0
[shipping_total] => 0.00
[shipping_tax] => 0
[cart_tax] => 0
[total] => 2.00
[total_tax] => 0
[customer_id] => 1
[order_key] => wc_order_FeOmJ7jXgWF4R
[billing] => Array
(
[first_name] => Jon
[last_name] => Conway
[company] =>
[address_1] =>
[address_2] =>
[city] => Frome
[state] =>
[postcode] =>
[country] =>
[email] => <**hidden**>
[phone] =>
)
[shipping] => Array
(
[first_name] =>
[last_name] =>
[company] =>
[address_1] =>
[address_2] =>
[city] =>
[state] =>
[postcode] =>
[country] =>
)
[payment_method] => bacs
[payment_method_title] => Direct bank transfer
[transaction_id] =>
[customer_ip_address] => 127.0.0.1
[customer_user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15
[created_via] => checkout
[customer_note] =>
[date_completed] =>
[date_paid] =>
[cart_hash] => cb56eda51b920dc5fbffac1c03fe33b1
)
[items:protected] => Array
(
[line_items] => Array
(
[78] => WC_Order_Item_Product Object
(
[extra_data:protected] => Array
(
[product_id] => 0
[variation_id] => 0
[quantity] => 1
[tax_class] =>
[subtotal] => 0
[subtotal_tax] => 0
[total] => 0
[total_tax] => 0
[taxes] => Array
(
[subtotal] => Array
(
)
[total] => Array
(
)
)
)
[data:protected] => Array
(
[order_id] => 1433
[name] => Online concert
[product_id] => 746
[variation_id] => 0
[quantity] => 1
[tax_class] =>
[subtotal] => 2
[subtotal_tax] => 0
[total] => 2
[total_tax] => 0
[taxes] => Array
(
[total] => Array
(
)
[subtotal] => Array
(
)
)
)
[cache_group:protected] => order-items
[meta_type:protected] => order_item
[object_type:protected] => order_item
[id:protected] => 78
[changes:protected] => Array
(
)
[object_read:protected] => 1
[default_data:protected] => Array
(
[order_id] => 0
[name] =>
[product_id] => 0
[variation_id] => 0
[quantity] => 1
[tax_class] =>
[subtotal] => 0
[subtotal_tax] => 0
[total] => 0
[total_tax] => 0
[taxes] => Array
(
[subtotal] => Array
(
)
[total] => Array
(
)
)
)
[data_store:protected] => WC_Data_Store Object
(
[instance:WC_Data_Store:private] => WC_Order_Item_Product_Data_Store Object
(
[internal_meta_keys:protected] => Array
(
[0] => _order_id
[1] => _name
[2] => _product_id
[3] => _variation_id
[4] => _quantity
[5] => _tax_class
[6] => _subtotal
[7] => _subtotal_tax
[8] => _total
[9] => _total_tax
[10] => _taxes
[11] => _product_id
[12] => _variation_id
[13] => _qty
[14] => _tax_class
[15] => _line_subtotal
[16] => _line_subtotal_tax
[17] => _line_total
[18] => _line_tax
[19] => _line_tax_data
)
[meta_type:protected] => order_item
[object_id_field_for_meta:protected] => order_item_id
[must_exist_meta_keys:protected] => Array
(
)
)
[stores:WC_Data_Store:private] => Array
(
[coupon] => WC_Coupon_Data_Store_CPT
[customer] => WC_Customer_Data_Store
[customer-download] => WC_Customer_Download_Data_Store
[customer-download-log] => WC_Customer_Download_Log_Data_Store
[customer-session] => WC_Customer_Data_Store_Session
[order] => WC_Order_Data_Store_CPT
[order-refund] => WC_Order_Refund_Data_Store_CPT
[order-item] => WC_Order_Item_Data_Store
[order-item-coupon] => WC_Order_Item_Coupon_Data_Store
[order-item-fee] => WC_Order_Item_Fee_Data_Store
[order-item-product] => WC_Order_Item_Product_Data_Store
[order-item-shipping] => WC_Order_Item_Shipping_Data_Store
[order-item-tax] => WC_Order_Item_Tax_Data_Store
[payment-token] => WC_Payment_Token_Data_Store
[product] => MAGE_Product_Data_Store_CPT
[product-grouped] => WC_Product_Grouped_Data_Store_CPT
[product-variable] => WC_Product_Variable_Data_Store_CPT
[product-variation] => WC_Product_Variation_Data_Store_CPT
[shipping-zone] => WC_Shipping_Zone_Data_Store
[webhook] => WC_Webhook_Data_Store
[report-revenue-stats] => Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore
[report-orders] => Automattic\WooCommerce\Admin\API\Reports\Orders\DataStore
[report-orders-stats] => Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore
[report-products] => Automattic\WooCommerce\Admin\API\Reports\Products\DataStore
[report-variations] => Automattic\WooCommerce\Admin\API\Reports\Variations\DataStore
[report-products-stats] => Automattic\WooCommerce\Admin\API\Reports\Products\Stats\DataStore
[report-categories] => Automattic\WooCommerce\Admin\API\Reports\Categories\DataStore
[report-taxes] => Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore
[report-taxes-stats] => Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\DataStore
[report-coupons] => Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore
[report-coupons-stats] => Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\DataStore
[report-downloads] => Automattic\WooCommerce\Admin\API\Reports\Downloads\DataStore
[report-downloads-stats] => Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats\DataStore
[admin-note] => Automattic\WooCommerce\Admin\Notes\DataStore
[report-customers] => Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore
[report-customers-stats] => Automattic\WooCommerce\Admin\API\Reports\Customers\Stats\DataStore
[report-stock-stats] => Automattic\WooCommerce\Admin\API\Reports\Stock\Stats\DataStore
)
[current_class_name:WC_Data_Store:private] => WC_Order_Item_Product_Data_Store
[object_type:WC_Data_Store:private] => order-item-product
)
[meta_data:protected] => Array
(
[0] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 1496
[key] => Date
[value] => 2020-10-02 10:00:00
)
[data:protected] => Array
(
[id] => 1496
[key] => Date
[value] => 2020-10-02 10:00:00
)
)
[1] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 1497
[key] => Adult - £10.00 x 1 =
[value] => £10.00
)
[data:protected] => Array
(
[id] => 1497
[key] => Adult - £10.00 x 1 =
[value] => £10.00
)
)
[2] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 1498
[key] => Location
[value] => ,
)
[data:protected] => Array
(
[id] => 1498
[key] => Location
[value] => ,
)
)
[3] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 1499
[key] => _event_ticket_info
[value] => Array
(
[0] => Array
(
[ticket_name] => Adult
[ticket_price] => 10
[ticket_qty] => 1
[max_qty] =>
[event_date] => 2020-10-02 10:00:00
)
)
)
[data:protected] => Array
(
[id] => 1499
[key] => _event_ticket_info
[value] => Array
(
[0] => Array
(
[ticket_name] => Adult
[ticket_price] => 10
[ticket_qty] => 1
[max_qty] =>
[event_date] => 2020-10-02 10:00:00
)
)
)
)
[4] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 1500
[key] => _event_user_info
[value] => Array
(
)
)
[data:protected] => Array
(
[id] => 1500
[key] => _event_user_info
[value] => Array
(
)
)
)
[5] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 1501
[key] => event_id
[value] => 745
)
[data:protected] => Array
(
[id] => 1501
[key] => event_id
[value] => 745
)
)
[6] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 1503
[key] => _event_extra_service
[value] => Array
(
)
)
[data:protected] => Array
(
[id] => 1503
[key] => _event_extra_service
[value] => Array
(
)
)
)
)
)
)
... truncated
I want to access:
[id] => 1499
[key] => _event_ticket_info
[value] => Array
(
[0] => Array
(
[ticket_name] => Adult
[ticket_price] => 10
[ticket_qty] => 1
[max_qty] =>
[event_date] => 2020-10-02 10:00:00
)
)
To calculate and get the cost from ticket_price
by ticket_qty
.
Is this possible? Any idea on how to get this specific order item meta data?
To get order item custom meta data, first you need to loop through order items, to be able to get that complex custom protected meta data using WC_Data
method get_meta()
. Then you will be able to get and calculate your "2nd installment cost as follows:
add_filter( 'woocommerce_get_order_item_totals', 'display_email_topay', 10, 2 );
function display_email_topay( $total_rows, $order ) {
$second_installment_cost = 0; // Initializing
// Loop through order items
foreach( $order->get_items() as $item ) {
// Try to get "_event_ticket_info" item meta value (array)
$event_ticket_data = $item->get_meta('_event_ticket_info');
// Get the data from the array
if( ! empty($event_ticket_data) ) {
// extract / Import variables into the current symbol table from an array
extract($event_ticket_data);
}
// if cost and quantity variables exist and have a value more than zero
if( isset($ticket_price) && $ticket_price > 0 && isset($ticket_qty) && $ticket_qty > 0 ) {
// Calculating and adding 2nd Installment cost item
$second_installment_cost += $ticket_price * $ticket_qty;
}
}
if ( $second_installment_cost > 0 ) {
$total_rows['recurr_not'] = array(
'label' => __( '2nd Installment', 'woocommerce' ),
'value' => wp_strip_all_tags( wc_price($second_installment_cost) )
);
}
return $total_rows;
}
Code goes in functions.php file of your active child theme (or active theme). It should works.
Related: