定制完全的WooCommerce我的账户订单部分。

huangapple go评论113阅读模式
英文:

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&#39;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文件中(或者放在一个插件中)。

您将会得到类似于:

定制完全的WooCommerce我的账户订单部分。

英文:

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( &#39;woocommerce_account_orders_columns&#39;, &#39;custom_account_orders_columns&#39; );
function custom_account_orders_columns( $columns ) {
    return array(
        &#39;date-time&#39;         =&gt; __( &#39;Date time&#39;, &#39;woocommerce&#39; ),
        &#39;billing-name&#39;      =&gt; __( &#39;Name&#39;, &#39;woocommerce&#39; ),
        &#39;billing-email&#39;     =&gt; __( &#39;Email&#39;, &#39;woocommerce&#39; ),
        &#39;total-order&#39;       =&gt; __( &#39;Total&#39;, &#39;woocommerce&#39; ),
        &#39;order-actions&#39;     =&gt; __( &#39;Actions&#39;, &#39;woocommerce&#39; ),
    );
}

Then for the new columns content, we will use the following hooked functions:

add_action( &#39;woocommerce_my_account_my_orders_column_date-time&#39;, &#39;my_account_orders_column_date_time&#39; );
function my_account_orders_column_date_time( $order ) {
    echo $order-&gt;get_date_created()-&gt;format(&#39;Y-m-d H:i:s&#39;);
}

add_action( &#39;woocommerce_my_account_my_orders_column_billing-name&#39;, &#39;my_account_orders_column_billing_name&#39; );
function my_account_orders_column_billing_name( $order ) {
    echo $order-&gt;get_billing_first_name();
}

add_action( &#39;woocommerce_my_account_my_orders_column_billing-email&#39;, &#39;my_account_orders_column_billing_email&#39; );
function my_account_orders_column_billing_email( $order ) {
    echo $order-&gt;get_billing_email();
}

add_action( &#39;woocommerce_my_account_my_orders_column_total-order&#39;, &#39;my_account_orders_column_total_order&#39; );
function my_account_orders_column_total_order( $order ) {
    echo wc_price($order-&gt;get_total());
}

The code goes in the functions.php file of your child theme (or in a plugin).

You will get something like:

定制完全的WooCommerce我的账户订单部分。

huangapple
  • 本文由 发表于 2023年8月10日 15:31:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定