簡體   English   中英

添加自定義列以訂購商品並使其在 WooCommerce 管理訂單詳細信息頁面中可排序

[英]Add a custom column to order items and make it sortable in WooCommerce admin order details page

我一直在努力向管理訂單詳細信息頁面添加項目產品屬性 - 我已經管理了該頁面。 您可以看到標題為“包裝重量”的額外列。

在此處輸入圖片說明

// Add custom column headers here
add_action('woocommerce_admin_order_item_headers', 'my_woocommerce_admin_order_item_headers');
function my_woocommerce_admin_order_item_headers() {
    // set the column name
    $column_name = 'Packing Weight';

    // display the column name
    echo '<th>' . $column_name . '</th>';
}

// Add custom column values here
add_action('woocommerce_admin_order_item_values', 'my_woocommerce_admin_order_item_values', 10, 3);
function my_woocommerce_admin_order_item_values($_product, $item, $item_id = null) {
    // get the post meta value from the associated product
    // $value = get_post_meta($_product->post->ID, '_custom_field_name', 1);
    $value = array_shift( wc_get_product_terms( $_product->post->ID, 'pa_packing-order', array( 'fields' => 'names' ) ) );

    // display the value
    echo '<td>' . $value . '</td>';
}

但是,我希望按照此列的順序訂購商品,例如非常柔軟、柔軟、堅硬。 如果這使訂購更容易,我很高興將包裝重量的值更改為 1 - 10 的數字范圍。

我該怎么做呢?

  • 請注意,我添加了一些 CSS 類,以使其可排序
  • 另請注意,我使用一組字符串作為輸出,並隨機顯示它們。 用您自己的代碼替換它。 這是因為我的產品不包含特定條款

因此,要使您的自定義列添加到管理訂單項可排序,您可以使用:

// Add header
function action_woocommerce_admin_order_item_headers( $order ) {
    // Set the column name
    $column_name = __( 'Packing Weight', 'woocommerce' );
    
    // Display the column name
    echo '<th class="line_packing_weight sortable" data-sort="string-ins">' . $column_name . '</th>';
}
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 1 );

//Add content
function action_woocommerce_admin_order_item_values( $product, $item, $item_id ) {
    // Only for "line_item" items type, to avoid errors
    if ( ! $item->is_type('line_item') ) return;
    
    // Replace this part with your own code
    $some_strings = array( 'Soft', 'Very soft', 'Hard' );
    
    // Replace this part with your own code
    $value = $some_strings[array_rand( $some_strings )];
    
    if ( $value ) { 
        echo '<td class="line_packing_weight" data-sort-value="' . $value . '">' . $value . '</td>';
    }
}
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM