简体   繁体   English

将列创建日期和订单号添加到 WooCommerce 我的帐户订单

[英]Add columns date created and order number to WooCommerce My account Orders

I want to add two types of metadata to an order and its order_items when it is created.我想在创建订单时向订单及其 order_items 添加两种类型的元数据。 I tried some things but it is not working.我尝试了一些东西,但它不起作用。

The first I want to add is $order->get_date_created();我要添加的第一个是 $order->get_date_created(); as meta data to the order.作为订单的元数据。

// Add order date as order meta data
add_action( 'woocommerce_checkout_create_order', 'add_order_date_to_order_meta_data', 10, 2 );
function add_order_date_to_order_meta_data( $order, $data ) {
  $order_date = $order->get_date_created();

        $order->update_meta_data( 'order_created', $order_date );
}

The second is $order->get_id();第二个是 $order->get_id(); that I want to add to both the order itself as to the order_items我想将 order_items 添加到订单本身

// Add order number as order meta data
add_action( 'woocommerce_checkout_create_order', 'add_order_number_to_order_meta_data', 10, 2 );
function add_order_number_to_order_meta_data( $order, $data ) {
  $order_id = $order->get_id();

        $order->update_meta_data( 'order_number', $order_id );
}

I am blank about about how to add it to the order lines.我对如何将其添加到订单行一无所知。 No clue on how to do that.不知道如何做到这一点。 I found the following but I believe that is adding information before a order is created?我发现了以下内容,但我认为这是在创建订单之前添加信息?

add_action( 'woocommerce_checkout_create_order_line_item', 'add_meta_to_line_item', 20, 4 );
 
function add_meta_to_line_item( $item, $cart_item_key, $values, $order )
{
    $_p = $item->get_product();
    $key = $order->get_id();
 
    if ( false !== ( $value = $_p->get_meta( $key, true ) ) )
        $item->add_meta_data( $key, true);
    }

Then after I created these meta fields, I also want to add them to a column in the order overview on the front-end.然后在我创建了这些元字段之后,我还想将它们添加到前端订单概览的列中。

/**
 * Adds a new column to the "My Orders" table in the account.
 *
 * @param string[] $columns the columns in the orders table
 * @return string[] updated columns
 */
function metavalue_add_my_account_orders_column( $columns ) {

    $new_columns = array();

    foreach ( $columns as $key => $name ) {

        $new_columns[ $key ] = $name;

        // add meta_value after order status column
        if ( 'order-status' === $key ) {
            $new_columns['order_number'] = __( 'Order Number', 'textdomain' );
            $new_columns['order_created'] = __( 'Order Created', 'textdomain' );
        }
    }

    return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'metavalue_add_my_account_orders_column' );

I hope someone can help me in the right direction.我希望有人可以在正确的方向上帮助我。

Updated - There are some mistakes and missing things in your code.更新- 您的代码中有一些错误和遗漏的东西。 Try the following instead:请尝试以下操作:

// Add order number and order date creation as order meta data
add_action( 'woocommerce_checkout_create_order', 'add_some_order_meta_data', 10, 2 );
function add_some_order_meta_data( $order, $data ) {
    $order->update_meta_data( '_order_number', $order->get_id() );
    $order->update_meta_data( 'order_date', current_time('Y-m-d H:i:s') );
}

// Add order Id as custom order item meta data
add_action( 'woocommerce_checkout_create_order_line_item', 'add_meta_to_line_item', 10, 4 );
function add_meta_to_line_item( $item, $cart_item_key, $values, $order ) {
    $item->add_meta_data( '_order_id', $order->get_id() );
}

// Include order number meta data for the method get_order_number()
add_filter( 'woocommerce_order_number', 'wc_set_order_number', 10, 2 );
function wc_set_order_number( $order_id, $order ) {
    // Get the order number (custom meta data)
    $order_number = $order->get_meta('_order_number');

    return $order_number ? $order_number : $order_id;
}

// Add columns to my account orders
add_filter( 'woocommerce_my_account_my_orders_columns', 'add_my_account_orders_columns' );
function add_my_account_orders_columns( $columns ) {
    $new_columns = array();

    foreach ( $columns as $key => $name ) {

        $new_columns[ $key ] = $name;

        // add meta_value after order status column
        if ( 'order-status' === $key ) {
            $new_columns['number'] = __( 'Order Number', 'textdomain' );
            $new_columns['date-created'] = __( 'Date Created', 'textdomain' );
        }
    }

    return $new_columns;
}

// Column order number content
add_action( 'woocommerce_my_account_my_orders_column_number', 'my_account_order_number_column_content', 10, 1 );
function my_account_order_number_column_content( $order ) {
    echo $order->get_order_number();
}

// Column order date created content
add_action( 'woocommerce_my_account_my_orders_column_date-created', 'my_account_order_date_created_column_content', 10, 1 );
function my_account_order_date_created_column_content( $order ) {
    echo $order->get_meta('order_date') ? $order->get_meta('order_date') : $order->get_date_created()->date('Y-m-d H:i:s');
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.测试和工作。

在此处输入图像描述


Notes:笔记:

To get the order number, you will use the WC_Order method get_order_number() from order object.要获取订单号,您将使用订单 object 中的WC_Order方法get_order_number()

You can change the saved date format as explained in here for WC_Date_time date() method.您可以按照WC_Date_time date()方法的说明更改保存的日期格式。

For the order Id stored as custom order item meta data the meta key needs to start with an underscore, to make it not visible in customer order and email notifications.对于存储为自定义订单项目元数据的订单 ID,元键需要以下划线开头,以使其在客户订单和 email 通知中不可见。 So to get it from order items, you will use WC_Data get_meta() method on the WC_Order_Item_Product object $item variable like $item->get_meta( '_order_id') or you can also use the method get_order_id() like $item->get_order_id()因此,要从订单项目中获取它,您将在WC_Order_Item_Product object $item 变量上使用 WC_Data get_meta() 方法,例如$item->get_meta( '_order_id')或者您也可以使用get_order_id()方法,例如$item->get_order_id()


Related:有关的:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce 我的账户订单自定义栏目中添加“支付订单”按钮 - Add “Pay for order” Button in WooCommerce My account Orders Custom Column 将列总订单重量添加到 WooCommerce 我的帐户订单 - Add column total order weight to WooCommerce My account Orders WooCommerce“我的账户”订单表添加多个自定义列 - Add multiple custom columns to WooCommerce "My account" orders table 在WooCommerce上添加支付订单按钮我的账户查看订单挂单 - Add a pay order button on WooCommerce My account view order for pending orders 在 WooCommerce 我的帐户订单列表中添加发送给客户的管理员订单备注 - Add Admin order notes sent to customer in WooCommerce My Account Orders list 添加到 WooCommerce“我的帐户”订单表中的订单状态列 - Add <HTML> to order-status column in WooCommerce "My account" orders table 在WooCommerce我的帐户-订单页面中摆脱订单状态 - Getting rid of order status within the WooCommerce My Account - Orders Page 根据产品 ID 向 WooCommerce“我的帐户”订单添加操作按钮 - Add an action button to WooCommerce “my account” orders according to product id 将产品缩略图添加到我的帐户-最近的订单-Woocommerce - Add Product Thumbnail to My Account - Recent Orders - Woocommerce 向 Woocommerce 添加在新窗口中打开的我的帐户订单的操作按钮 - Add an action button to Woocommerce my account orders that open in a new window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM