如何在管理面板订单详细信息页面添加自定义文本

问题描述 投票:0回答:1

after qty column i add custom data

woocommerce_admin_order_item_values() 这个钩子尝试但不起作用这是我的代码

add_action('woocommerce_admin_order_item_values', 'replace_quantity_td_content', 99999, 3);

function replace_quantity_td_content($product, $item, $item_id) {
    $quantity = $item->get_quantity();
    $meta_data_array = $item->get_meta_data(); 
    
    foreach ($meta_data_array as $meta) {
        $meta_value_array = $meta->value;
        if (isset($meta_value_array['tabNumber'])) {
            $tab_number = $meta_value_array['tabNumber'];   
            if ($tab_number === 'square-meter-li') {
                $quantity_display = $quantity . ' m2'; 
            }
            echo  $quantity_display ; 
        }
    }
}
wordpress woocommerce hook-woocommerce
1个回答
0
投票

要将自定义文本添加到 WooCommerce 管理面板中的订单详细信息页面,您可以使用

woocommerce_admin_order_item_values
挂钩。但是,您的代码中有一些问题需要解决。这是更正后的代码:

add_action('woocommerce_admin_order_item_values', 'replace_quantity_td_content', 10, 3);

function replace_quantity_td_content( $product, $item, $item_id ) {
    $quantity = $item->get_quantity();
    $meta_data_array = $item->get_meta_data(); 
    
    // Loop through the meta data
    foreach ($meta_data_array as $meta) {
        // Get the value of the meta data
        $meta_value = $meta->value;
        
        // Check if the meta data contains 'tabNumber' key
        if (is_array($meta_value) && isset($meta_value['tabNumber'])) {
            // Get the tab number
            $tab_number = $meta_value['tabNumber'];   
            
            // Check if the tab number is 'square-meter-li'
            if ($tab_number === 'square-meter-li') {
                // Modify the quantity display
                $quantity_display = $quantity . ' m2'; 
                
                // Output the modified quantity display
                echo $quantity_display; 
                
                // Break the loop as we found the desired meta data
                break;
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.