英文:
Customize completely WooCommerce My account Orders section
问题
基本上,我正在尝试更改WooCommerce“我的账户”中的“订单”部分。我已经能够做出如下更改:
**截图1**,是我得到的结果。
[![在此输入图片描述][1]][1]
**截图2**,是我想要的期望结果:
[![在此输入图片描述][2]][2]
在*wc-accounts-function.php*文件中,我找到了以下相关内容:
```php
/**
* 获取“我的账户” > “订单”栏目。
*
* @since 2.6.0
* @return array
*/
function wc_get_account_orders_columns() {
$columns = apply_filters(
'woocommerce_account_orders_columns',
array(
'order-number' => __( '订单', 'woocommerce' ),
'order-date' => __( '日期', 'woocommerce' ),
'order-status' => __( '状态', 'woocommerce' ),
'order-total' => __( '总计', 'woocommerce' ),
'order-actions' => __( '操作', 'woocommerce' ),
)
);
// 2.6.0版本后弃用的过滤器。
return apply_filters( 'woocommerce_my_account_my_orders_columns', $columns );
}
此外,我还找到了另一段代码,我猜它定义在文件my-orders.php
中的功能:
<?php elseif ( 'order-number' === $column_id ) : ?>
<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
<?php echo _x( '#', '订单号前的标识', 'woocommerce' ) . $order->get_order_number(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</a>
<?php elseif ( 'order-date' === $column_id ) : ?>
<time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>">
<?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?>
<?php elseif ( 'order-total' === $column_id ) : ?>
<?php
/* 翻译器:1:格式化后的订单总额 2:订单中的商品总数 */
printf( _n( '%1$s,共 %2$s 件商品', '%1$s,共 %2$s 件商品', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
?>
<?php elseif ( 'order-actions' === $column_id ) : ?>
<?php
$actions = wc_get_account_orders_actions( $order );
但是我无法获得我需要的期望输出:
- 日期和时间
- 名字
- 电子邮件
- 总金额
我正在使用最新版本的WooCommerce与DIVI主题。
任何帮助将不胜感激。
<details>
<summary>英文:</summary>
Basically, I am trying to change the WooCommerce My account Orders section. I have been able to do so changes:
**Screenshot 1**, is the result I am getting.
[![enter image description here][1]][1]
**Screenshot 2**, is the desired result I want:
[![enter image description here][2]][2]
Here I found in *wc-accounts-function.php* file the following related:
/**
-
Get My Account > Orders columns.
* -
@since 2.6.0
-
@return array
*/
function wc_get_account_orders_columns() {
$columns = apply_filters(
'woocommerce_account_orders_columns',
array(
'order-number' => __( 'Order', 'woocommerce' ),
'order-date' => __( 'Date', 'woocommerce' ),
'order-status' => __( 'Status', 'woocommerce' ),
'order-total' => __( 'Total', 'woocommerce' ),
'order-actions' => __( 'Actions', 'woocommerce' ),
)
);// Deprecated filter since 2.6.0.
return apply_filters( 'woocommerce_my_account_my_orders_columns', $columns );
}
Moreover I also found another piece of code that is i guess defining the fuctionality in the file `my-orders.php`:
<?php elseif ( 'order-number' === $column_id ) : ?>
<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
<?php echo _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</a>
<?php elseif ( 'order-date' === $column_id ) : ?>
<time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?>
<?php elseif ( 'order-total' === $column_id ) : ?>
<?php
/* translators: 1: formatted order total 2: total order items */
printf( _n( '%1$s for %2$s item', '%1$s for %2$s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
?>
<?php elseif ( 'order-actions' === $column_id ) : ?>
<?php
$actions = wc_get_account_orders_actions( $order );
But I can't get the desired output for the things that I need:
- Date and Time
- First Name
- Email
- Total amount
I am using last WooCommerce version with the DIVI theme.
Any help will be highly appreciated.
[1]: https://i.stack.imgur.com/yLPiy.png
[2]: https://i.stack.imgur.com/EZZH6.png
</details>
# 答案1
**得分**: 0
你不需要编辑模板文件来更改“我的账户订单”部分。
首先,要编辑“我的账户 > 订单”菜单,您将使用以下代码:
```php
add_filter( 'woocommerce_account_orders_columns', 'custom_account_orders_columns' );
function custom_account_orders_columns( $columns ) {
return array(
'date-time' => __( '日期时间', 'woocommerce' ),
'billing-name' => __( '姓名', 'woocommerce' ),
'billing-email' => __( '电子邮件', 'woocommerce' ),
'total-order' => __( '总计', 'woocommerce' ),
'order-actions' => __( '操作', 'woocommerce' ),
);
}
然后,对于新列的内容,我们将使用以下挂钩函数:
add_action( 'woocommerce_my_account_my_orders_column_date-time', 'my_account_orders_column_date_time' );
function my_account_orders_column_date_time( $order ) {
echo $order->get_date_created()->format('Y-m-d H:i:s');
}
add_action( 'woocommerce_my_account_my_orders_column_billing-name', 'my_account_orders_column_billing_name' );
function my_account_orders_column_billing_name( $order ) {
echo $order->get_billing_first_name();
}
add_action( 'woocommerce_my_account_my_orders_column_billing-email', 'my_account_orders_column_billing_email' );
function my_account_orders_column_billing_email( $order ) {
echo $order->get_billing_email();
}
add_action( 'woocommerce_my_account_my_orders_column_total-order', 'my_account_orders_column_total_order' );
function my_account_orders_column_total_order( $order ) {
echo wc_price($order->get_total());
}
这些代码应该放在您子主题的functions.php文件中(或者放在一个插件中)。
您将会得到类似于:
英文:
You don't need to edit the template files to change the My Account Orders section.
First to edit the My Account > Orders menu you will use the following:
add_filter( 'woocommerce_account_orders_columns', 'custom_account_orders_columns' );
function custom_account_orders_columns( $columns ) {
return array(
'date-time' => __( 'Date time', 'woocommerce' ),
'billing-name' => __( 'Name', 'woocommerce' ),
'billing-email' => __( 'Email', 'woocommerce' ),
'total-order' => __( 'Total', 'woocommerce' ),
'order-actions' => __( 'Actions', 'woocommerce' ),
);
}
Then for the new columns content, we will use the following hooked functions:
add_action( 'woocommerce_my_account_my_orders_column_date-time', 'my_account_orders_column_date_time' );
function my_account_orders_column_date_time( $order ) {
echo $order->get_date_created()->format('Y-m-d H:i:s');
}
add_action( 'woocommerce_my_account_my_orders_column_billing-name', 'my_account_orders_column_billing_name' );
function my_account_orders_column_billing_name( $order ) {
echo $order->get_billing_first_name();
}
add_action( 'woocommerce_my_account_my_orders_column_billing-email', 'my_account_orders_column_billing_email' );
function my_account_orders_column_billing_email( $order ) {
echo $order->get_billing_email();
}
add_action( 'woocommerce_my_account_my_orders_column_total-order', 'my_account_orders_column_total_order' );
function my_account_orders_column_total_order( $order ) {
echo wc_price($order->get_total());
}
The code goes in the functions.php file of your child theme (or in a plugin).
You will get something like:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论