問題描述
我已經(jīng)通過 function.php 在我的產(chǎn)品頁面中創(chuàng)建了一個自定義的保修字段.
add_action( 'woocommerce_product_options_general_product_data', 'test_custom_fields' );函數(shù) test_custom_fields() {//打印自定義文本字段woocommerce_wp_text_input(數(shù)組('id' =>'_保修單','標簽' =>'IE.15年','說明' =>'','desc_tip' =>'真的','占位符' =>'IE.15年') );}add_action('woocommerce_process_product_meta', 'test_save_custom_fields');功能 test_save_custom_fields( $post_id ) {如果(!空($_POST['_warranty'])){update_post_meta( $post_id, '_warranty', esc_attr( $_POST['_warranty'] ) );}}
我想根據(jù)購物車/訂單中的產(chǎn)品(無插件)在管理訂單頁面上的一個自生成的自定義字段中使用鍵和值復制"這個自定義字段.
因此,通過訂單頁面上的這個自定義字段,我終于可以使用 WooCommerce PDF Invoice 插件在我的 pdf 發(fā)票中顯示保修"了.
另一種解釋:
- 作為管理員,我在product1"頁面填寫 _warranty 值
- 當product1"在訂單中時,在管理訂單視圖中,我希望從 product1 頁面看到一個包含_warranty + value"的自定義字段.
- 因此,作為管理員,我可以在 WooCommerce PDF Invoice 插件中設置
{{_warranty}}
以顯示保修:15 年"
非常感謝您的幫助.
我剛剛測試了以下案例:在訂單詳情中的訂單項表中顯示產(chǎn)品元數(shù)據(jù)
但這并沒有給我一個自定義字段,所以我無法得到我的 {{_warranty}}
值寬度它.
我做錯了什么?
我怎樣才能做到這一點?
謝謝.
第一:在管理訂單頁面上的一個自生成的自定義字段中復制帶有鍵和值的自定義字段" 不是好方法.
為了實現(xiàn)您的期望,您只是錯過了一些小事.您需要:
- 在購物車中存儲自定義字段(將產(chǎn)品添加到購物車時)
- 在購物車和結帳頁面上呈現(xiàn)此內容
- 將訂單中的信息添加為元數(shù)據(jù)(使其成為訂單的一部分)
<塊引用>
使用第 3 點,您將能夠在 WooCommerce PDF Invoice 插件上獲得此信息,以顯示保修:15 年".
所以你需要的代碼是:
//在產(chǎn)品管理選項卡上創(chuàng)建自定義字段add_action('woocommerce_product_options_general_product_data', 'create_warranty_custom_field');函數(shù) create_warranty_custom_field() {//創(chuàng)建自定義文本框woocommerce_wp_text_input(數(shù)組('id' =>'_保修單','類型' =>'文本','標簽' =>__('保修', 'woocommerce'),'說明' =>'','desc_tip' =>'真的','占位符' =>__('即 15 年', 'woocommerce'),) );}//將此自定義字段中的數(shù)據(jù)值保存在產(chǎn)品管理選項卡上add_action('woocommerce_process_product_meta', 'save_warranty_custom_field');功能 save_warranty_custom_field( $post_id ) {$wc_text_field = $_POST['_warranty'];如果(!空($wc_text_field)){update_post_meta( $post_id, '_warranty', esc_attr( $wc_text_field ) );}}//將自定義字段存儲在購物車中add_filter( 'woocommerce_add_cart_item_data', 'store_warranty_custom_field', 10, 2 );函數(shù) store_warranty_custom_field( $cart_item_data, $product_id ) {$warranty_item = get_post_meta( $product_id , '_warranty', true );如果(!空($warranty_item)){$cart_item_data['_warranty'] = $warranty_item;//下面的語句確保每個添加到購物車的操作都是唯一的訂單項$cart_item_data['unique_key'] = md5( microtime().rand() );WC()->session->set('days_manufacture', $warranty_item);}返回 $cart_item_data;}//在購物車和結帳上呈現(xiàn)元數(shù)據(jù)add_filter('woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2);函數(shù) rendering_meta_field_on_cart_and_checkout( $cart_data, $cart_item ) {$custom_items = array();//Woo 2.4.2 更新if( !empty( $cart_data ) ) {$custom_items = $cart_data;}if( isset( $cart_item['_warranty'] ) ) {$custom_items[] = array( "name" => __( "Warranty", "woocommerce" ), "value" => $cart_item['_warranty'] );}返回 $custom_items;}//將訂單中的信息添加為元數(shù)據(jù)add_action('woocommerce_add_order_item_meta','add_waranty_to_order_item_meta', 1, 3);函數(shù) add_waranty_to_order_item_meta( $item_id, $values, $cart_item_key ) {//檢索訂單 $item_id 的產(chǎn)品 ID$product_id = wc_get_order_item_meta( $item_id, '_product_id', true );//獲取此產(chǎn)品 ID 的保修值$warranty = get_post_meta( $product_id, '_warranty', true );//將元數(shù)據(jù)添加到訂單中wc_add_order_item_meta($item_id, '保修', $warranty, true);}
當然,這會出現(xiàn)在活動子主題(或主題)的 function.php 文件或任何插件文件中.
此代碼已經(jīng)過測試且有效.
<小時>參考文獻:
- WooCommerce:將自定義 Metabox 添加到管理訂單頁面
- 管理產(chǎn)品頁面自定義字段顯示在購物車和結帳中
I've created a custom field for warranty in my products pages, via function.php.
add_action( 'woocommerce_product_options_general_product_data', 'test_custom_fields' );
function test_custom_fields() {
// Print a custom text field
woocommerce_wp_text_input( array(
'id' => '_warranty',
'label' => 'i.e. 15 years',
'description' => '',
'desc_tip' => 'true',
'placeholder' => 'i.e. 15 years'
) );
}
add_action( 'woocommerce_process_product_meta', 'test_save_custom_fields' );
function test_save_custom_fields( $post_id ) {
if ( ! empty( $_POST['_warranty'] ) ) {
update_post_meta( $post_id, '_warranty', esc_attr( $_POST['_warranty'] ) );
}
}
I would like to "duplicate" this custom field with key and value, in an self-generated custom field on the admin order page depending on products in cart/order (without plugin).
So, with this custom field on order page, I will finally be able to display "warranty" in my pdf invoice with WooCommerce PDF Invoice plugin.
Another explanation :
- As admin, I fill _warranty value in "product1" page
- When "product1" is in an order, on the admin order view, I would like to see a custom field containing "_warranty + value" from product1 page.
- So, as admin, I could set
{{_warranty}}
in WooCommerce PDF Invoice plugin to display "Warranty : 15 years"
Many thanks for your help.
I have just tested the following case: show product meta in order items table in Order Details
But this does not give me a custom field, so I couldn't get my {{_warranty}}
value width it.
What I am doing wrong?
How can I achieve this?
Thanks.
First: "Duplicating this custom field with key and value, in an self-generated custom field on the admin order page" is not the good approach.
To achieve what you are expecting, you have missed just some little things. You need to:
- Store custom field in Cart (when product is added to cart)
- Render this on cart and checkout pages
- Add the information in the order as meta data (to make it part of the order)
With point 3 you will be able to get this on WooCommerce PDF Invoice plugin to display "Warranty : 15 years".
So the code you need is:
// create the custom field on product admin tab
add_action( 'woocommerce_product_options_general_product_data', 'create_warranty_custom_field' );
function create_warranty_custom_field() {
// Create a custom text field
woocommerce_wp_text_input( array(
'id' => '_warranty',
'type' => 'text',
'label' => __('Warranty', 'woocommerce' ),
'description' => '',
'desc_tip' => 'true',
'placeholder' => __('i.e. 15 years', 'woocommerce' ),
) );
}
// save the data value from this custom field on product admin tab
add_action( 'woocommerce_process_product_meta', 'save_warranty_custom_field' );
function save_warranty_custom_field( $post_id ) {
$wc_text_field = $_POST['_warranty'];
if ( !empty($wc_text_field) ) {
update_post_meta( $post_id, '_warranty', esc_attr( $wc_text_field ) );
}
}
// Store custom field in Cart
add_filter( 'woocommerce_add_cart_item_data', 'store_warranty_custom_field', 10, 2 );
function store_warranty_custom_field( $cart_item_data, $product_id ) {
$warranty_item = get_post_meta( $product_id , '_warranty', true );
if( !empty($warranty_item) ) {
$cart_item_data[ '_warranty' ] = $warranty_item;
// below statement make sure every add to cart action as unique line item
$cart_item_data['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'days_manufacture', $warranty_item );
}
return $cart_item_data;
}
// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_data, $cart_item ) {
$custom_items = array();
// Woo 2.4.2 updates
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['_warranty'] ) ) {
$custom_items[] = array( "name" => __( "Warranty", "woocommerce" ), "value" => $cart_item['_warranty'] );
}
return $custom_items;
}
// Add the information in the order as meta data
add_action('woocommerce_add_order_item_meta','add_waranty_to_order_item_meta', 1, 3 );
function add_waranty_to_order_item_meta( $item_id, $values, $cart_item_key ) {
// Retrieving the product id for the order $item_id
$product_id = wc_get_order_item_meta( $item_id, '_product_id', true );
// Getting the warranty value for this product Id
$warranty = get_post_meta( $product_id, '_warranty', true );
// Add the meta data to the order
wc_add_order_item_meta($item_id, 'Warranty', $warranty, true);
}
Naturally, this goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
References:
- WooCommerce : Add custom Metabox to admin order page
- Admin product pages custom field displayed in Cart and checkout
這篇關于在購物車、結帳和查看訂單中設置產(chǎn)品自定義字段和顯示值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!