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

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

Customize completely WooCommerce My account Orders section

问题

  1. 基本上,我正在尝试更改WooCommerce“我的账户”中的“订单”部分。我已经能够做出如下更改:
  2. **截图1**,是我得到的结果。
  3. [![在此输入图片描述][1]][1]
  4. **截图2**,是我想要的期望结果:
  5. [![在此输入图片描述][2]][2]
  6. 在*wc-accounts-function.php*文件中,我找到了以下相关内容:
  7. ```php
  8. /**
  9. * 获取“我的账户” > “订单”栏目。
  10. *
  11. * @since 2.6.0
  12. * @return array
  13. */
  14. function wc_get_account_orders_columns() {
  15. $columns = apply_filters(
  16. 'woocommerce_account_orders_columns',
  17. array(
  18. 'order-number' => __( '订单', 'woocommerce' ),
  19. 'order-date' => __( '日期', 'woocommerce' ),
  20. 'order-status' => __( '状态', 'woocommerce' ),
  21. 'order-total' => __( '总计', 'woocommerce' ),
  22. 'order-actions' => __( '操作', 'woocommerce' ),
  23. )
  24. );
  25. // 2.6.0版本后弃用的过滤器。
  26. return apply_filters( 'woocommerce_my_account_my_orders_columns', $columns );
  27. }

此外,我还找到了另一段代码,我猜它定义在文件my-orders.php中的功能:

  1. <?php elseif ( 'order-number' === $column_id ) : ?>
  2. <a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
  3. <?php echo _x( '#', '订单号前的标识', 'woocommerce' ) . $order->get_order_number(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
  4. </a>
  5. <?php elseif ( 'order-date' === $column_id ) : ?>
  6. <time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>">
  7. <?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time>
  8. <?php elseif ( 'order-status' === $column_id ) : ?>
  9. <?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?>
  10. <?php elseif ( 'order-total' === $column_id ) : ?>
  11. <?php
  12. /* 翻译器:1:格式化后的订单总额 2:订单中的商品总数 */
  13. 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
  14. ?>
  15. <?php elseif ( 'order-actions' === $column_id ) : ?>
  16. <?php
  17. $actions = wc_get_account_orders_actions( $order );

但是我无法获得我需要的期望输出:

  • 日期和时间
  • 名字
  • 电子邮件
  • 总金额

我正在使用最新版本的WooCommerce与DIVI主题。

任何帮助将不胜感激。

  1. <details>
  2. <summary>英文:</summary>
  3. Basically, I am trying to change the WooCommerce My account Orders section. I have been able to do so changes:
  4. **Screenshot 1**, is the result I am getting.
  5. [![enter image description here][1]][1]
  6. **Screenshot 2**, is the desired result I want:
  7. [![enter image description here][2]][2]
  8. 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 );
    }

  1. 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 );

  1. But I can&#39;t get the desired output for the things that I need:
  2. - Date and Time
  3. - First Name
  4. - Email
  5. - Total amount
  6. I am using last WooCommerce version with the DIVI theme.
  7. Any help will be highly appreciated.
  8. [1]: https://i.stack.imgur.com/yLPiy.png
  9. [2]: https://i.stack.imgur.com/EZZH6.png
  10. </details>
  11. # 答案1
  12. **得分**: 0
  13. 你不需要编辑模板文件来更改“我的账户订单”部分。
  14. 首先,要编辑“我的账户 > 订单”菜单,您将使用以下代码:
  15. ```php
  16. add_filter( 'woocommerce_account_orders_columns', 'custom_account_orders_columns' );
  17. function custom_account_orders_columns( $columns ) {
  18. return array(
  19. 'date-time' => __( '日期时间', 'woocommerce' ),
  20. 'billing-name' => __( '姓名', 'woocommerce' ),
  21. 'billing-email' => __( '电子邮件', 'woocommerce' ),
  22. 'total-order' => __( '总计', 'woocommerce' ),
  23. 'order-actions' => __( '操作', 'woocommerce' ),
  24. );
  25. }

然后,对于新列的内容,我们将使用以下挂钩函数:

  1. add_action( 'woocommerce_my_account_my_orders_column_date-time', 'my_account_orders_column_date_time' );
  2. function my_account_orders_column_date_time( $order ) {
  3. echo $order->get_date_created()->format('Y-m-d H:i:s');
  4. }
  5. add_action( 'woocommerce_my_account_my_orders_column_billing-name', 'my_account_orders_column_billing_name' );
  6. function my_account_orders_column_billing_name( $order ) {
  7. echo $order->get_billing_first_name();
  8. }
  9. add_action( 'woocommerce_my_account_my_orders_column_billing-email', 'my_account_orders_column_billing_email' );
  10. function my_account_orders_column_billing_email( $order ) {
  11. echo $order->get_billing_email();
  12. }
  13. add_action( 'woocommerce_my_account_my_orders_column_total-order', 'my_account_orders_column_total_order' );
  14. function my_account_orders_column_total_order( $order ) {
  15. echo wc_price($order->get_total());
  16. }

这些代码应该放在您子主题的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:

  1. add_filter( &#39;woocommerce_account_orders_columns&#39;, &#39;custom_account_orders_columns&#39; );
  2. function custom_account_orders_columns( $columns ) {
  3. return array(
  4. &#39;date-time&#39; =&gt; __( &#39;Date time&#39;, &#39;woocommerce&#39; ),
  5. &#39;billing-name&#39; =&gt; __( &#39;Name&#39;, &#39;woocommerce&#39; ),
  6. &#39;billing-email&#39; =&gt; __( &#39;Email&#39;, &#39;woocommerce&#39; ),
  7. &#39;total-order&#39; =&gt; __( &#39;Total&#39;, &#39;woocommerce&#39; ),
  8. &#39;order-actions&#39; =&gt; __( &#39;Actions&#39;, &#39;woocommerce&#39; ),
  9. );
  10. }

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

  1. add_action( &#39;woocommerce_my_account_my_orders_column_date-time&#39;, &#39;my_account_orders_column_date_time&#39; );
  2. function my_account_orders_column_date_time( $order ) {
  3. echo $order-&gt;get_date_created()-&gt;format(&#39;Y-m-d H:i:s&#39;);
  4. }
  5. add_action( &#39;woocommerce_my_account_my_orders_column_billing-name&#39;, &#39;my_account_orders_column_billing_name&#39; );
  6. function my_account_orders_column_billing_name( $order ) {
  7. echo $order-&gt;get_billing_first_name();
  8. }
  9. add_action( &#39;woocommerce_my_account_my_orders_column_billing-email&#39;, &#39;my_account_orders_column_billing_email&#39; );
  10. function my_account_orders_column_billing_email( $order ) {
  11. echo $order-&gt;get_billing_email();
  12. }
  13. add_action( &#39;woocommerce_my_account_my_orders_column_total-order&#39;, &#39;my_account_orders_column_total_order&#39; );
  14. function my_account_orders_column_total_order( $order ) {
  15. echo wc_price($order-&gt;get_total());
  16. }

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:

确定