Php 在购物车和订单项目名称下显示woocommerce产品自定义字段
我有一个代码,在产品编辑页面的“常规”选项卡中显示自定义字段 经理填写完这些字段后,数据将显示在存档/类别页面和单个产品页面上 此外,这些字段位于购物车和结帐页面中 这是我的密码:Php 在购物车和订单项目名称下显示woocommerce产品自定义字段,php,wordpress,woocommerce,cart,orders,Php,Wordpress,Woocommerce,Cart,Orders,我有一个代码,在产品编辑页面的“常规”选项卡中显示自定义字段 经理填写完这些字段后,数据将显示在存档/类别页面和单个产品页面上 此外,这些字段位于购物车和结帐页面中 这是我的密码: // Display Fields using WooCommerce Action Hook add_action( 'woocommerce_product_options_general_product_data', 'woocom_general_product_data_custom_field' );
// Display Fields using WooCommerce Action Hook
add_action( 'woocommerce_product_options_general_product_data', 'woocom_general_product_data_custom_field' );
function woocom_general_product_data_custom_field() {
// Create a custom text field
// Custom Weight Field
woocommerce_wp_text_input(
array(
'id' => '_custom_weight',
'label' => __( 'Weight dishes', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( '', 'woocommerce' )
)
);
// Calories Field
woocommerce_wp_text_input(
array(
'id' => '_сalories',
'label' => __( 'Calories', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'false',
'description' => __( '', 'woocommerce' )
)
);
// Ingredients Field
woocommerce_wp_textarea_input(
array(
'id' => '_ingredients',
'label' => __( 'Ingredients', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( '', 'woocommerce' )
)
);
}
// Hook to save the data value from the custom fields
add_action( 'woocommerce_process_product_meta', 'woocom_save_general_proddata_custom_field' );
// Hook callback function to save custom fields information
function woocom_save_general_proddata_custom_field( $post_id ) {
// Save Custom Weight Field
$custom_weight = $_POST['_custom_weight'];
if( ! empty( $custom_weight ) ) {
update_post_meta( $post_id, '_custom_weight', esc_attr( $custom_weight ) );
} else {
delete_post_meta( $post_id, '_custom_weight' );
}
// Save Calories Field
$сalories = $_POST['_сalories'];
if( ! empty( $сalories ) ) {
update_post_meta( $post_id, '_сalories', esc_attr( $сalories ) );
} else {
delete_post_meta( $post_id, '_сalories' );
}
// Save Ingredients Field
$ingredients = $_POST['_ingredients'];
if( ! empty( $ingredients ) ) {
update_post_meta( $post_id, '_ingredients', esc_html( $ingredients ) );
} else {
delete_post_meta( $post_id, '_ingredients' );
}
}
// Displaying the custom field value (on single product pages under short description)
add_action('woocommerce_before_add_to_cart_form', 'display_custom_meta_field_value', 25 );
add_action('woocommerce_after_shop_loop_item', 'display_custom_meta_field_value', 25 );
function display_custom_meta_field_value() {
global $product;
$custom_weight = get_post_meta( $product->get_id(),'_custom_weight', true );
if( ! empty( $custom_weight ) )
echo '<p id="value-on-single-product">' . 'Weight: ' . $custom_weight . 'g' . '</p>';
$сalories = get_post_meta( $product->get_id(),'_сalories', true );
if( ! empty( $сalories ) )
echo '<p id="value-on-single-product">' . 'Calories: ' . $сalories . ' kcal.' . '</p>';
$ingredients = get_post_meta( $product->get_id(),'_ingredients', true );
if( ! empty( $ingredients ) )
echo '<p id="value-on-single-product">' . 'Ingredients: ' . $ingredients . '</p>';
}
// Render the custom product field in cart and checkout
add_filter( 'woocommerce_get_item_data', 'woocom_custom_fields_cart', 10, 2 );
function woocom_custom_fields_cart( $cart_data, $cart_item )
{
$custom_items = array();
if( !empty( $cart_data ) )
$custom_items = $cart_data;
// Get the product ID
$product_id = $cart_item['product_id'];
if( $custom_field_value = get_post_meta( $product_id, '_custom_weight', true ) )
$custom_items[] = array(
'name' => __( 'Weight', 'woocommerce' ),
'value' => $custom_field_value,
'display' => $custom_field_value . 'g',
);
return $custom_items;
}
//使用操作挂钩显示字段
添加操作(“woocommerce\u product\u options\u general\u product\u data”、“woocom\u general\u product\u data\u custom\u field”);
函数woocom\通用\产品\数据\自定义\字段(){
//创建自定义文本字段
//自定义权重字段
woocommerce_wp_text_input(
数组(
'id'=>''自定义重量',
“标签”=>uuuuuuuuuuuuuuu('Weight displays','woocommerce'),
'占位符'=>'',
'desc_tip'=>'true',
'description'=>\('woocommerce')
)
);
//热量场
woocommerce_wp_text_input(
数组(
“id”=>“\uСalories”,
'label'=>\('carries','woocommerce'),
'占位符'=>'',
'desc_tip'=>'false',
'description'=>\('woocommerce')
)
);
//成分领域
woocommerce_wp_text区域_输入(
数组(
'id'=>'u配料',
“标签”=>uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
'占位符'=>'',
'desc_tip'=>'true',
'description'=>\('woocommerce')
)
);
}
//钩子以保存自定义字段中的数据值
添加操作(“woocommerce\u process\u product\u meta”、“woocom\u save\u general\u proddata\u custom\u field”);
//用于保存自定义字段信息的钩子回调函数
函数woocom\u save\u general\u proddata\u custom\u字段($post\u id){
//保存自定义重量字段
$custom_weight=$_POST[''自定义_weight'];
如果(!空($custom_weight)){
更新发布元数据($post\u id,''u custom\u weight',esc\u attr($custom\u weight));
}否则{
删除“post”meta($post\u id,“\u custom\u weight”);
}
//节省热量场
$c alories=$_POST[''c alories'];
如果(!空($c alories)){
更新发布元数据($post\u id,''.'c alories',esc\u attr($c alories));
}否则{
删除后置元($post_id,'.'cuc alories');
}
//保存成分字段
$配料=$_POST[“配料”];
如果(!空($Components)){
更新发布元数据($post\u id,'u配料',esc\u html($配料));
}否则{
删除post-meta($post-id,'u-components');
}
}
//显示自定义字段值(在“简短说明”下的单个产品页面上)
添加操作(“添加到购物车表单之前的woocommerce”、“显示自定义元字段值”,25);
添加操作('WOOMerce\u在\u shop\u loop\u项目之后,'display\u custom\u meta\u field\u value',25);
函数显示\自定义\元\字段\值(){
全球$产品;
$custom\u weight=get\u post\u meta($product->get\u id(),“u custom\u weight”,true);
如果(!空($custom_weight))
echo'。“重量:”。$custom_Weight.“g”。
;
$c alories=get_post_meta($product->get_id(),“'c alories',true);
如果(!空($c alories))
echo“.”卡路里:'.$cf alories.'kcal.
';
$components=get\u post\u meta($product->get\u id(),“配料”,true);
如果(!空($Components))
echo“.”成分:“.$contracents.”
”;
}
//在购物车和结帐中呈现自定义产品字段
添加过滤器('woocommerce\u get\u item\u data','woocom\u custom\u fields\u cart',10,2);
函数woocom\自定义\字段\购物车($cart\数据,$cart\项目)
{
$custom_items=array();
如果(!空($cart_数据))
$custom\u items=$cart\u数据;
//获取产品ID
$product_id=$cart_项目['product_id'];
如果($custom_field_value=get_post_meta($product_id,''u custom_weight',true))
$custom_items[]=数组(
“名称”=>uuuuuuuuuuuuu('Weight','woocommerce'),
“值”=>$custom\u字段\u值,
'display'=>$custom\u field\u值'g',
);
返回$custom_项目;
}
但不幸的是,我无法在感谢页面、电子邮件和订单编辑页面上的产品名称之后添加这些自定义字段
我也怀疑上面的代码是否正确,尽管一切正常
我将为你的帮助而高兴 我重新查看了您的代码并做了一些更改,添加了必要的代码,以在购物车、结帐、收到的订单、我的帐户订单视图和电子邮件通知上显示您的自定义字段数据(适用于Woocommerce 3+):
//后端:显示其他产品字段
添加操作('woocommerce\u product\u options\u general\u product\u data'、'add\u fields\u to\u options\u general\u product\u data');
函数添加\字段\到\选项\常规\产品\数据(){
//自定义权重字段
woocommerce_wp_text_输入(数组(
'id'=>''自定义重量',
“标签”=>uuuuuuuuuuuuuuu('Weight displays','woocommerce'),
));
//热量场
woocommerce_wp_text_输入(数组(
“id”=>“\uСalories”,
'label'=>\('carries','woocommerce'),
));
//成分领域
woocommerce\u wp\u文本区域\u输入(数组(
'id'=>'u配料',
“标签”=>uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
));
}
//后端:保存自定义字段中的数据值
添加操作('woocommerce\u admin\u process\u product\u object','save\u admin\u product\u custom\u fields\u values');
函数保存\管理\产品\自定义\字段\值($product){
//保存自定义重量字段
如果(设置($邮政[''自定义重量]){
$product->update_meta_数据('u custom_weight',sanitize_text_字段($_POST['u custom_weight']));
}
//节省热量场
如果(isset($_POST[''''c alories')){
$product->update_meta_数据('u2c alories',清理_text_字段($_POST['u2c alories'));
}
//保存成分字段
如果(isset($_POST[''u配料])){
$product->update_meta_数据(''u配料',净化_文本区域_字段($\u位置
// Backend: Display additional product fields
add_action( 'woocommerce_product_options_general_product_data', 'add_fields_to_options_general_product_data' );
function add_fields_to_options_general_product_data() {
// Custom Weight Field
woocommerce_wp_text_input( array(
'id' => '_custom_weight',
'label' => __( 'Weight dishes', 'woocommerce' ),
));
// Calories Field
woocommerce_wp_text_input( array(
'id' => '_сalories',
'label' => __( 'Calories', 'woocommerce' ),
));
// Ingredients Field
woocommerce_wp_textarea_input( array(
'id' => '_ingredients',
'label' => __( 'Ingredients', 'woocommerce' ),
));
}
// Backend: Save the data value from the custom fields
add_action( 'woocommerce_admin_process_product_object', 'save_admin_product_custom_fields_values' );
function save_admin_product_custom_fields_values( $product ) {
// Save Custom Weight Field
if( isset( $_POST['_custom_weight'] ) ) {
$product->update_meta_data( '_custom_weight', sanitize_text_field( $_POST['_custom_weight'] ) );
}
// Save Calories Field
if( isset( $_POST['_сalories'] ) ) {
$product->update_meta_data( '_сalories', sanitize_text_field( $_POST['_сalories'] ) );
}
// Save Ingredients Field
if( isset( $_POST['_ingredients'] ) ) {
$product->update_meta_data( '_ingredients', sanitize_textarea_field( $_POST['_ingredients'] ) );
}
}
// Display product custom fields values on single product pages under short description and on archive pages
add_action('woocommerce_before_add_to_cart_form', 'display_custom_meta_field_value', 25 );
add_action('woocommerce_after_shop_loop_item', 'display_custom_meta_field_value', 25 );
function display_custom_meta_field_value() {
global $product;
if( $custom_weight = $product->get_meta('_custom_weight') )
echo '<p id="value-on-single-product">' . __("Weight:", "woocommerce") . ' ' . $custom_weight . 'g' . '</p>';
if( $сalories = $product->get_meta('_сalories') )
echo '<p id="value-on-single-product">' . __("Calories:", "woocommerce") . ' ' . $сalories . ' kcal.' . '</p>';
if( $ingredients = $product->get_meta('_ingredients') )
echo '<p id="value-on-single-product">' . __("Ingredients:", "woocommerce") . ' ' . $ingredients . '</p>';
}
// Add custom fields values under cart item name in cart
add_filter( 'woocommerce_cart_item_name', 'custom_cart_item_name', 10, 3 );
function custom_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
if( ! is_cart() )
return $item_name;
if( $value1 = $cart_item['data']->get_meta('_custom_weight') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Weight", "woocommerce") . ':</strong> ' . $value1 . 'g</span>';
}
if( $value2 = $cart_item['data']->get_meta('_сalories') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Calories", "woocommerce") . ':</strong> ' . $value2 . 'kcal</span>';
}
if( $value3 = $cart_item['data']->get_meta('_ingredients') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Ingredients", "woocommerce") . ':</strong> <br>' . $value3 . '</span>';
}
return $item_name;
}
// Display custom fields values under item name in checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_checkout_cart_item_name', 10, 3 );
function custom_checkout_cart_item_name( $item_qty, $cart_item, $cart_item_key ) {
if( $value1 = $cart_item['data']->get_meta('_custom_weight') ) {
$item_qty .= '<br><span class="custom-field"><strong>' . __("Weight", "woocommerce") . ':</strong> ' . $value1 . 'g</span>';
}
if( $value2 = $cart_item['data']->get_meta('_сalories') ) {
$item_qty .= '<br><span class="custom-field"><strong>' . __("Calories", "woocommerce") . ':</strong> ' . $value2 . 'kcal</span>';
}
if( $value3 = $cart_item['data']->get_meta('_ingredients') ) {
$item_qty .= '<br><span class="custom-field"><strong>' . __("Ingredients", "woocommerce") . ':</strong> <br>' . $value3 . '</span>';
}
return $item_qty;
}
// Display custom fields values on orders and email notifications
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
$product = $item->get_product();
if( $value1 = $product->get_meta('_custom_weight') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Weight", "woocommerce") . ':</strong> ' . $value1 . 'g</span>';
}
if( $value2 = $product->get_meta('_сalories') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Calories", "woocommerce") . ':</strong> ' . $value2 . 'kcal</span>';
}
if( $value3 = $product->get_meta('_ingredients') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Ingredients", "woocommerce") . ':</strong> <br>' . $value3 . '</span>';
}
return $item_name;
}