我想在前端的訂單中添加重量元資料:我的帳戶 - 訂單。我嘗試了一些東西,但它不起作用。
我要添加的是 $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 ) : '–';
}
希望有人可以幫助我朝著正確的方向前進。
uj5u.com熱心網友回復:
此代碼段在“我的帳戶”>“訂單”中顯示的訂單表中插入了一個新的自定義列,其中填充了訂單的總重量,以便客戶知道他們的訂單有多重。
具體來說,這個片段有兩個代碼塊。第一個塊插入列。在本例中,我們在 Order Total 和 Order Actions 列之間插入了這一列。這可以通過更改代碼中的列鍵來更改。您的自定義將出現在您定義的列鍵之后。
第二個代碼塊是魔法發生的地方。它首先遍歷訂單中的每個專案,并根據該產品的數量計算其重量和時間。然后它將每個產品的權重添加到我們稱為 $total_weight 的變數中。然后將總重量輸出到新列,后跟您在 WordPress 儀表板 > WooCommerce > 設定 > 產品 > 常規 > 測量 > 重量單位下的商店設定中定義的重量單位。
/**
* 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;
}
uj5u.com熱心網友回復:
剛遇到這個,你試過嗎?https://gist.github.com/kloon/5299119?permalink_comment_id=1415838
以下是鏈接最終失效的代碼副本:
<?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';
}
}
}
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/486613.html
標籤:php WordPress woocommerce 前端 元数据