我正在尝试使用
woocommerce_get_order_item_totals
过滤器挂钩在订单总计中添加一些额外信息(关于订单和电子邮件通知)。
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;
}
我可以访问
$order
,其中包含我想要显示的数据,但我不知道如何获取它。
下面是
var_dump
变量的 $order
(已格式化):
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
我想访问:
[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
)
)
通过
ticket_price
计算并得到ticket_qty
的成本。
这可能吗?有关如何获取此特定订单项元数据的任何想法吗?
要获取订单项自定义元数据,首先您需要循环遍历订单项,以便能够使用
WC_Data
方法 get_meta()
获取复杂的自定义受保护元数据。然后您就可以得到并计算您的“第二期分期付款费用如下:
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;
}
代码位于活动子主题(或活动主题)的functions.php 文件中。应该有效。
相关:
LoicTheAztec的回答帮助我解决了问题。此自定义元数据数组需要第二个 foreach 循环,因为 $event_ticket_data
是多维数组。这是工作代码:
add_filter( 'woocommerce_get_order_item_totals', 'display_email_topay', 10, 2 );
function display_email_topay( $total_rows, $order ) {
$total_ticket=0;
$total_service=0;
// Loop through order items: get ticket costs
foreach( $order->get_items() as $ticket ) {
// Try to get "_event_ticket_info" item meta value (array)
$event_ticket_data = $ticket->get_meta('_event_ticket_info');
// echo'<pre>';var_dump($event_ticket_data);echo '</pre><br><br>';
foreach($event_ticket_data as $item) {
extract($item);
$total_ticket += $ticket_price * $ticket_qty;
// echo '<br>'.$ticket_price.' x '.$ticket_qty;
}
$event_ticket_data = $ticket->get_meta('_event_extra_service');
// echo'<pre>';var_dump($event_ticket_data);echo '</pre><br><br>';
foreach($event_ticket_data as $item) {
extract($item);
$total_service += $service_price * $service_qty;
// echo '<br>'.$service_price.' x '.$service_qty;
}
}
// work out the cart_total and amount left to pay
$cart_total = $total_ticket+$total_service;
$second_installment_cost = $cart_total - $order->get_total();
if ( $second_installment_cost > 0 ) {
$total_rows['order_total'] = array(
'label' => __( 'Order Total', 'woocommerce' ),
'value' => wp_strip_all_tags( wc_price($cart_total) )
);
$total_rows['order_topay'] = array(
'label' => __( '2nd Installment', 'woocommerce' ),
'value' => wp_strip_all_tags( wc_price($second_installment_cost) )
);
$total_rows['cart_subtotal'] = array(
'label' => __( 'To Pay', 'woocommerce' ),
'value' => wp_strip_all_tags( wc_price($order->get_total() ) )
);
}
return $total_rows;
}