将列总订单重量添加到WooCommerce我的帐户订单

发布于 2025-02-04 16:23:00 字数 1083 浏览 5 评论 0原文

我想将重量元数据添加到前端的订单中:我的帐户 - 订单。我尝试了一些事情,但它不起作用。

我想添加是$ order-> get_weight();作为元数据到订单,但我会遇到错误。

我已经使用此代码添加新列并显示产品描述和数量了:

add_filter( 'woocommerce_my_account_my_orders_columns', 'additional_my_account_orders_column', 10, 1 );
function additional_my_account_orders_column( $columns ) {
    $new_columns = [];

    foreach ( $columns as $key => $name ) {
        $new_columns[ $key ] = $name;

        if ( 'order-status' === $key ) {
            $new_columns['order-items'] = __( 'Descripción', 'woocommerce' );
        }
    }
    return $new_columns;
}

add_action( 'woocommerce_my_account_my_orders_column_order-items', 'additional_my_account_orders_column_content', 10, 1 );
function additional_my_account_orders_column_content( $order ) {
    $details = array();

    foreach( $order->get_items() as $item )
        $details[] = $item->get_name() . ' × ' . $item->get_quantity();

    echo count( $details ) > 0 ? implode( '<br>', $details ) : '&ndash;';
}

希望有人可以帮助我朝着正确的方向发展。

I want to add weight metadata to an order in the Frontend: My Account - Orders. I tried some things but it is not working.

I want to add is $order->get_weight(); as meta data to the order but I am getting an error.

I am already half way using this code to add a new column and show product description and quantity:

add_filter( 'woocommerce_my_account_my_orders_columns', 'additional_my_account_orders_column', 10, 1 );
function additional_my_account_orders_column( $columns ) {
    $new_columns = [];

    foreach ( $columns as $key => $name ) {
        $new_columns[ $key ] = $name;

        if ( 'order-status' === $key ) {
            $new_columns['order-items'] = __( 'Descripción', 'woocommerce' );
        }
    }
    return $new_columns;
}

add_action( 'woocommerce_my_account_my_orders_column_order-items', 'additional_my_account_orders_column_content', 10, 1 );
function additional_my_account_orders_column_content( $order ) {
    $details = array();

    foreach( $order->get_items() as $item )
        $details[] = $item->get_name() . ' × ' . $item->get_quantity();

    echo count( $details ) > 0 ? implode( '<br>', $details ) : '–';
}

Hope someone can help me get in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚笙 2025-02-11 16:23:01

该片段在我的帐户中显示的订单表中插入了一个新的自定义列。订单的总重量填充了订单,因此客户知道他们的订单有多沉。

具体来说,该片段具有两个代码块。第一个块插入列。在此示例中,我们在订单总计和订单操作列之间插入了此列。可以通过更改代码中的列键来更改。您的自定义将在您定义的列键之后出现。

第二个代码是魔术发生的地方。它首先以订单中的每个项目进行循环,并通过此产品的数量来使其重量,并以此为例。然后,它将每个产品的重量添加到我们称为$ total_weight的变量中。然后将总重量输出到新的列,然后输出您在WordPress仪表板下的商店设置中定义的重量单元&gt; WooCommerce&gt;设置&gt;产品&GT;一般&gt;测量和GT;重量单位。

/**
 * Snippet Name:    WooCommerce Show Order Weight Column In My Account Order View Table
 * Snippet Author:  ecommercehints.com
 */

// First, create the new table column between Total and Actions columns
add_filter( 'woocommerce_my_account_my_orders_columns', 'ecommercehints_weight_column_my_account_orders_table', 10, 1 );
function ecommercehints_weight_column_my_account_orders_table( $columns ) {
    $weight_column = [];
    foreach ( $columns as $key => $name ) {
        $weight_column[ $key ] = $name;
        if ( 'order-total' === $key ) { // Insert new column after Total column
            $weight_column['order-items'] = __( 'Order Weight', 'woocommerce' );
        }
    }
    return $weight_column;
}

// Second, insert the data from the order into the new column
add_action( 'woocommerce_my_account_my_orders_column_order-items', 'ecommercehints_get_order_weight', 10, 1 );
function ecommercehints_get_order_weight( $order ) {
    $weight_unit = get_option('woocommerce_weight_unit');
    $total_weight = 0;
    foreach( $order->get_items() as $item_id => $item ){
        $quantity = $item->get_quantity();
        $product = $item->get_product();
        $product_weight = $product->get_weight();
        $total_weight += floatval( $product_weight * $quantity );
    }
    echo $total_weight . $weight_unit;
}

This snippet inserts a new, custom column in the table of orders shown in My Account > Orders populated with the total weight of the order so the customer is aware how heavy their order was.

Specifically, this snippet has two blocks of code. The first block inserts the column. In this example, we have inserted this column between the Order Total and Order Actions column. This can be changed by changing the column key in the code. Your custom will appear after the column key you define.

The second block of code is where the magic happens. It first loops through each item in the order and gets it weight and times this by the quantity of this product. It then adds this weight of each product to a variable we have called $total_weight. The total weight is then output to the new column followed by the weight unit you have defined in your store settings under WordPress Dashboard > WooCommerce > Settings > Products > General > Measurements > Weight Unit.

/**
 * Snippet Name:    WooCommerce Show Order Weight Column In My Account Order View Table
 * Snippet Author:  ecommercehints.com
 */

// First, create the new table column between Total and Actions columns
add_filter( 'woocommerce_my_account_my_orders_columns', 'ecommercehints_weight_column_my_account_orders_table', 10, 1 );
function ecommercehints_weight_column_my_account_orders_table( $columns ) {
    $weight_column = [];
    foreach ( $columns as $key => $name ) {
        $weight_column[ $key ] = $name;
        if ( 'order-total' === $key ) { // Insert new column after Total column
            $weight_column['order-items'] = __( 'Order Weight', 'woocommerce' );
        }
    }
    return $weight_column;
}

// Second, insert the data from the order into the new column
add_action( 'woocommerce_my_account_my_orders_column_order-items', 'ecommercehints_get_order_weight', 10, 1 );
function ecommercehints_get_order_weight( $order ) {
    $weight_unit = get_option('woocommerce_weight_unit');
    $total_weight = 0;
    foreach( $order->get_items() as $item_id => $item ){
        $quantity = $item->get_quantity();
        $product = $item->get_product();
        $product_weight = $product->get_weight();
        $total_weight += floatval( $product_weight * $quantity );
    }
    echo $total_weight . $weight_unit;
}
凉月流沐 2025-02-11 16:23:01

刚遇到了这个,您尝试过吗?

这是代码的副本,以防该链接最终死亡:

<?php
add_filter( 'manage_edit-shop_order_columns', 'woo_order_weight_column' );
function woo_order_weight_column( $columns ) {
  $columns['total_weight'] = __( 'Weight', 'woocommerce' );
    return $columns;
}

add_action( 'manage_shop_order_posts_custom_column', 'woo_custom_order_weight_column', 2 );
function woo_custom_order_weight_column( $column ) {
    global $post, $woocommerce, $the_order;

    if ( empty( $the_order ) || $the_order->get_id() !== $post->ID )
        $the_order = new WC_Order( $post->ID );

    if ( $column == 'total_weight' ) {
        $weight = 0;
        if ( sizeof( $the_order->get_items() ) > 0 ) {
            foreach( $the_order->get_items() as $item ) {
                if ( $item['product_id'] > 0 ) {
                    $_product = $item->get_product();
                    if ( ! $_product->is_virtual() ) {
                        $weight += $_product->get_weight() * $item['qty'];
                    }
                }
            }
        }
        if ( $weight > 0 ) {
            print $weight . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) );
        } else {
            print 'N/A';
        }
    }
}
?>

Just came across this, have you tried it? https://gist.github.com/kloon/5299119?permalink_comment_id=1415838

Here is a copy of the code in case the link eventually dies:

<?php
add_filter( 'manage_edit-shop_order_columns', 'woo_order_weight_column' );
function woo_order_weight_column( $columns ) {
  $columns['total_weight'] = __( 'Weight', 'woocommerce' );
    return $columns;
}

add_action( 'manage_shop_order_posts_custom_column', 'woo_custom_order_weight_column', 2 );
function woo_custom_order_weight_column( $column ) {
    global $post, $woocommerce, $the_order;

    if ( empty( $the_order ) || $the_order->get_id() !== $post->ID )
        $the_order = new WC_Order( $post->ID );

    if ( $column == 'total_weight' ) {
        $weight = 0;
        if ( sizeof( $the_order->get_items() ) > 0 ) {
            foreach( $the_order->get_items() as $item ) {
                if ( $item['product_id'] > 0 ) {
                    $_product = $item->get_product();
                    if ( ! $_product->is_virtual() ) {
                        $weight += $_product->get_weight() * $item['qty'];
                    }
                }
            }
        }
        if ( $weight > 0 ) {
            print $weight . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) );
        } else {
            print 'N/A';
        }
    }
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文