在WooCommerce管理订单中显示产品自定义字段值的订单项目。

huangapple go评论73阅读模式
英文:

Display product custom field value on order items in WooCommerce admin orders

问题

在WooCommerce中,使用下面的代码,我已经能够在我的常规产品选项标签中添加一个自定义字段,其中包含一个文本框选项。我还需要在我的订单页面上显示这个自定义字段,显示在订单项目详细信息页面的自定义列中(在我的管理区域中,我可以查看客户购买的产品)。

以下是我的代码:

// 添加自定义列标题
add_action('woocommerce_admin_order_item_headers', 'my_woocommerce_admin_order_item_headers');
function my_woocommerce_admin_order_item_headers() {
    $column_name = 'Plek';  
    echo '<th data-sort="float" style="text-align: right;">' . $column_name . '</th>';
}

// 添加自定义列的值
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) {
    // 从关联产品获取文章元数据值
    // $value = get_post_meta($_product->post->ID, '_custom_field_name', 1);
    $value = array_shift(wc_get_product_terms($_product->post->ID, 'locatie', array('fields' => 'names')));
    echo '<td>' . $value . '</td>';
}

// 显示订单备注字段
add_action('woocommerce_product_options_general_product_data', 'pans_woo_add_custom_general_fields');
function pans_woo_add_custom_general_fields() {  
    echo '<div class="pans_ta_options_group">';
    woocommerce_wp_textarea_input(array(
        'id'          => 'plek',
        'label'       => __('plek', 'woocommerce'),
        'placeholder' => 'Alleen zichtbaar voor Admins.',   
    ));
    echo '</div>';
}

// 保存订单备注字段
add_action('woocommerce_process_product_meta', 'pans_woo_add_custom_general_fields_save');
function pans_woo_add_custom_general_fields_save($post_id){  
    $woocommerce_textarea = $_POST['plek']; 
    if(!empty($woocommerce_textarea)) 
        update_post_meta($post_id, 'plek', sanitize_textarea_field($woocommerce_textarea));
}

产品自定义字段的值已正确显示和保存在我的常规产品选项标签(产品管理页面)中。

我还成功在订单详细信息页面内添加了列名。

但我无法显示这个产品自定义字段的值。是否有人可以提供一些建议或帮助我修复这个问题?

我做错了什么?

英文:

On Woocommerce, with the code below I have been able to add a custom field inside my general product options tabs with a textbox option. I also need to display this custom field on my order pages in a custom column in my order items detail page (the page in my admin area where I can see which products my customer bought).

Here is my code:

// Add custom column headers here
add_action(&#39;woocommerce_admin_order_item_headers&#39;, &#39;my_woocommerce_admin_order_item_headers&#39;);
function my_woocommerce_admin_order_item_headers() {
    $column_name = &#39;Plek&#39;;  
    echo &#39;&lt;th data-sort=&quot;float&quot; style=&quot;text-align: right;&quot; &gt;&#39; . $column_name . &#39;&lt;/th&gt;&#39;;
}

// Add custom column values here
add_action(&#39;woocommerce_admin_order_item_values&#39;, &#39;my_woocommerce_admin_order_item_values&#39;, 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-&gt;post-&gt;ID, &#39;_custom_field_name&#39;, 1);
    $value = array_shift( wc_get_product_terms( $_product-&gt;post-&gt;ID, &#39;locatie&#39;, array( &#39;fields&#39; =&gt; &#39;names&#39; ) ) );
    echo &#39;&lt;td&gt;&#39; . $value . &#39;&lt;/td&gt;&#39;;
}

// Display order notes Fields
add_action( &#39;woocommerce_product_options_general_product_data&#39;, &#39;pans_woo_add_custom_general_fields&#39; );
function pans_woo_add_custom_general_fields() {  global $woocommerce, $post;
    echo &#39;&lt;div class=&quot;pans_ta_options_group&quot;&gt;&#39;;
    woocommerce_wp_textarea_input( array(
        &#39;id&#39;          =&gt; &#39;plek&#39;,
        &#39;label&#39;       =&gt; __( &#39;plek&#39;, &#39;woocommerce&#39; ),
        &#39;placeholder&#39; =&gt; &#39;Alleen zichtbaar voor Admins.&#39;,   
    )  );
    echo &#39;&lt;/div&gt;&#39;;
}

// Save order note Fields
add_action( &#39;woocommerce_process_product_meta&#39;, &#39;pans_woo_add_custom_general_fields_save&#39; );
function pans_woo_add_custom_general_fields_save( $post_id ){  
    $woocommerce_textarea = $_POST[&#39;plek&#39;]; 
    if( !empty( $woocommerce_textarea ) ) 
        update_post_meta( $post_id, &#39;plek&#39;, sanitize_textarea_field( $woocommerce_textarea ) );
}

The Product custom field value is displayed and saved correctly inside my general product options tabs (Product admin pages).

I also managed to get the column name done inside the order detail page.

But I am not able to display this product custom field value. Can anyone please give me some suggestions or help how to fix this?

What am I doing wrong?

答案1

得分: 1

我已经翻译好了你提供的代码部分:

我已经重新查看了您的代码,发现其中存在一些错误和一些已过时的内容(自 WooCommerce 3 以来)。因此,尝试使用以下代码,它将在管理订单页面上显示您的“Plek”产品自定义字段值:

// 将自定义列标题添加到管理订单页面
add_action('woocommerce_admin_order_item_headers', 'my_woocommerce_admin_order_item_headers');
function my_woocommerce_admin_order_item_headers() {
    $column_name = 'Plek';  
    echo '<th data-sort="float" style="text-align: right;">' . $column_name . '</th>';
}

// 将自定义列值添加到管理订单页面
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) {
    // 从关联产品获取产品元数据值
    $value = $product->get_meta('plek');
    echo '<td>' . $value . '</td>';
}

// 在管理产品页面上显示'plek'自定义字段
add_action('woocommerce_product_options_general_product_data', 'pans_woo_add_custom_general_fields');
function pans_woo_add_custom_general_fields() {  
    global $post;
    echo '<div class="pans_ta_options_group">';
    
    woocommerce_wp_textarea_input( array(
        'id'          => 'plek',
        'label'       => __('plek', 'woocommerce'),
        'placeholder' => '仅对管理员可见。',
    )  );
    
    echo '</div>';
}

// 保存'plek'产品自定义字段
add_action('woocommerce_admin_process_product_object', 'pans_woo_product_custom_field_save');
function pans_woo_product_custom_field_save( $product ) { 
    if( isset( $_POST['plek'] ) ) {
        $product->update_meta_data( 'plek', sanitize_textarea_field( $_POST['plek'] ) );
    }
}

这段代码应该放在你的活动子主题(或活动主题)的functions.php文件中。已经进行了测试,可以正常工作。

英文:

I Have revisited your code as there are some errors and some outdated things in your code (Since WooCommerce 3). So try instead the following that will display your 'Plek' product custom field value on admin order pages:

// Add custom column headers to admin order pages
add_action(&#39;woocommerce_admin_order_item_headers&#39;, &#39;my_woocommerce_admin_order_item_headers&#39;);
function my_woocommerce_admin_order_item_headers() {
    $column_name = &#39;Plek&#39;;  
    echo &#39;&lt;th data-sort=&quot;float&quot; style=&quot;text-align: right;&quot; &gt;&#39; . $column_name . &#39;&lt;/th&gt;&#39;;
}

// Add custom column values to admin order pages
add_action(&#39;woocommerce_admin_order_item_values&#39;, &#39;my_woocommerce_admin_order_item_values&#39;, 10, 3);
function my_woocommerce_admin_order_item_values($product, $item, $item_id = null) {
    // get the product meta value from the associated product
    $value = $product-&gt;get_meta(&#39;plek&#39;);
    echo &#39;&lt;td&gt;&#39; . $value . &#39;&lt;/td&gt;&#39;;
}

// Display &#39;plek&#39; custom fields on admin product pages
add_action( &#39;woocommerce_product_options_general_product_data&#39;, &#39;pans_woo_add_custom_general_fields&#39; );
function pans_woo_add_custom_general_fields() {  
	global $post;
    echo &#39;&lt;div class=&quot;pans_ta_options_group&quot;&gt;&#39;;
	
    woocommerce_wp_textarea_input( array(
        &#39;id&#39;          =&gt; &#39;plek&#39;,
        &#39;label&#39;       =&gt; __( &#39;plek&#39;, &#39;woocommerce&#39; ),
        &#39;placeholder&#39; =&gt; &#39;Alleen zichtbaar voor Admins.&#39;,   
    )  );
	
    echo &#39;&lt;/div&gt;&#39;;
}

// Save &#39;plek&#39; product custom Fields
add_action(&#39;woocommerce_admin_process_product_object&#39;, &#39;pans_woo_product_custom_field_save&#39;);
function pans_woo_product_custom_field_save( $product ) { 
    if( isset( $_POST[&#39;plek&#39;] ) ) {
        $product-&gt;update_meta_data( &#39;plek&#39;, sanitize_textarea_field( $_POST[&#39;plek&#39;] ) );
	}
}

Code goes in function.php file of your active child theme (or active theme). Tested and works

huangapple
  • 本文由 发表于 2023年5月25日 10:41:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定