显示在购物车和结帐中的管理产品页面自定义字段 [英] Admin product pages custom field displayed in Cart and checkout
问题描述
我在 WooCommerce Admin 中的产品页面的常规设置选项卡上创建了一个自定义字段,以插入几天的制造时间.我想在购物车和结帐页面上每个产品名称上方显示此自定义字段值.
这是我的代码:
//插入自定义管理字段函数 woo_add_custom_general_fields() {echo '';woocommerce_wp_text_input(数组('id' =>'天_制造','标签' =>__( '制造天数', 'woocommerce' ),'占位符' =>'','说明' =>__( '在此处插入', 'woocommerce' ),'类型' =>'数字','custom_attributes' =>大批('步骤' =>'任何','分钟' =>'1')) );回声'</div>';}add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields');//保存字段函数 woo_add_custom_general_fields_save( $post_id ){$woocommerce_number_field = $_POST['days_manufacture'];if( !empty( $woocommerce_number_field ) )update_post_meta( $post_id, 'days_manufacture', esc_attr( $woocommerce_number_field ) );}add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save');函数 save_days_field( $cart_item_data, $product_id ) {if( isset( $_REQUEST['days_manufacture'] ) ) {$cart_item_data[ 'days_manufacture' ] = get_post_meta( $post->ID, 'days_manufacture',true );/* 下面的语句确保每个添加到购物车的操作都是唯一的行项目 */$cart_item_data['unique_key'] = md5( microtime().rand() );}返回 $cart_item_data;}add_action('woocommerce_add_cart_item_data', 'save_days_field', 10, 2);函数 render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {$custom_items = array();/* Woo 2.4.2 更新 */if( !empty( $cart_data ) ) {$custom_items = $cart_data;}if( isset( $cart_item['days_manufacture'] ) ) {$custom_items[] = array( "name" => 'Days:', "value" => $cart_item['days_manufacture'] );}返回 $custom_items;}add_filter('woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2);但这不起作用,因为我无法在购物车和结帐页面上显示自定义字段值.
我怎样才能做到这一点?
谢谢
解决方案 我已经测试了您的代码并更正了一些部分,以使该产品自定义字段显示在购物车和结帐页面上.
这是更正后的代码:
//插入自定义管理字段add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields');函数 woo_add_custom_general_fields() {echo '';woocommerce_wp_text_input(数组('id' =>'天_制造','标签' =>__( '制造天数', 'woocommerce' ),'占位符' =>'','说明' =>__( '在此处插入', 'woocommerce' ),'类型' =>'数字','custom_attributes' =>大批('步骤' =>'任何','分钟' =>'1'),) );回声'</div>';}//保存字段add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save');函数 woo_add_custom_general_fields_save( $post_id ){$woocommerce_number_field = $_POST['days_manufacture'];if( !empty( $woocommerce_number_field ) )update_post_meta( $post_id, 'days_manufacture', esc_attr( $woocommerce_number_field ) );}//存储自定义字段add_filter('woocommerce_add_cart_item_data', 'save_days_field', 10, 2);函数 save_days_field( $cart_item_data, $product_id ) {$special_item = get_post_meta( $product_id , 'days_manufacture',true );如果(!空($special_item)){$cart_item_data['days_manufacture'] = $special_item;//下面的语句确保每个添加到购物车的操作都是唯一的订单项$cart_item_data['unique_key'] = md5( microtime().rand() );WC()->session->set('days_manufacture', $special_item);}返回 $cart_item_data;}//在购物车和结帐上呈现元数据add_filter('woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2);函数渲染_meta_field_on_cart_and_checkout( $cart_item_data, $cart_item ) {if( isset( $cart_item['days_manufacture'] ) ) {$cart_item_data[] = array( "name" => __( "Days", "woocommerce" ), "value" => $cart_item['days_manufacture'] );}返回 $cart_item_data;}当然,这会出现在活动子主题(或主题)的 function.php 文件或任何插件文件中.
此代码已经过测试且有效.
参考:WooCommerce:将自定义 Metabox 添加到管理订单页面一个>
I have created a Custom Field in WooCommerce Admin on the general settings tab of product pages, to insert a some days for manufacture. I would like to show this custom field value on cart and checkout pages above the name of each product.
Here is my code:
// Insert a Custom Admin Field
function woo_add_custom_general_fields() {
echo '<div class="options_group">';
woocommerce_wp_text_input( array(
'id' => 'days_manufacture',
'label' => __( 'Days for Manufacture', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Insert here', 'woocommerce' ),
'type' => 'number',
'custom_attributes' =>
array(
'step' => 'any',
'min' => '1'
)
) );
echo '</div>';
}
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save field
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_number_field = $_POST['days_manufacture'];
if( !empty( $woocommerce_number_field ) )
update_post_meta( $post_id, 'days_manufacture', esc_attr( $woocommerce_number_field ) );
}
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function save_days_field( $cart_item_data, $product_id ) {
if( isset( $_REQUEST['days_manufacture'] ) ) {
$cart_item_data[ 'days_manufacture' ] = get_post_meta( $post->ID, 'days_manufacture',true );
/* below statement make sure every add to cart action as unique line item */
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_days_field', 10, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
$custom_items = array();
/* Woo 2.4.2 updates */
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['days_manufacture'] ) ) {
$custom_items[] = array( "name" => 'Days:', "value" => $cart_item['days_manufacture'] );
}
return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
But this is not working as I can't display the custom field value on cart and checkout pages.
How can I achieve this?
Thanks
解决方案 I have tested your code and corrected some portions to make that product custom field appear on cart and checkout pages.
Here is that corrected code:
// Insert a Custom Admin Field
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
echo '<div class="options_group">';
woocommerce_wp_text_input( array(
'id' => 'days_manufacture',
'label' => __( 'Days for Manufacture', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Insert here', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '1'
),
) );
echo '</div>';
}
// Save the field
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_number_field = $_POST['days_manufacture'];
if( !empty( $woocommerce_number_field ) )
update_post_meta( $post_id, 'days_manufacture', esc_attr( $woocommerce_number_field ) );
}
// Store custom field
add_filter( 'woocommerce_add_cart_item_data', 'save_days_field', 10, 2 );
function save_days_field( $cart_item_data, $product_id ) {
$special_item = get_post_meta( $product_id , 'days_manufacture',true );
if(!empty($special_item)) {
$cart_item_data[ 'days_manufacture' ] = $special_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', $special_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_item_data, $cart_item ) {
if( isset( $cart_item['days_manufacture'] ) ) {
$cart_item_data[] = array( "name" => __( "Days", "woocommerce" ), "value" => $cart_item['days_manufacture'] );
}
return $cart_item_data;
}
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.
Reference: WooCommerce : Add custom Metabox to admin order page
这篇关于显示在购物车和结帐中的管理产品页面自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
相关文章
- 购物车和结帐中显示的管理产品页面自定义字段;
- 在WooCommerce购物车和结帐表中显示产品自定义字段值;
- 将产品自定义字段值保存在购物车中并将其显示在购物车和结帐中;
- 在产品中添加自定义字段以显示在购物车,结帐和订单中;
- 将产品自定义字段值保存在购物车中,并在购物车上显示并结帐;
- 在购物车、结帐和查看订单中设置产品自定义字段和显示值;
- 保存产品自定义字段并将其显示在购物车页面中;
- 设置产品自定义字段并在购物车,结帐和查看订单中显示值;
- 在Woocommerce购物车中显示自定义字段的值结帐项目;
- 将产品自定义字段单选按钮值保存在购物车中并将其显示在购物车页面上;
- 在 WooCommerce 单个产品和购物车页面中输出自定义字段值;
- 自定义Woocommerce购物车页面中2列上的结帐字段;
- 在购物车和结账时显示 WooCommerce 自定义产品字段元数据;
- 在购物车和结帐页面上显示 Woocommerce 产品类别;
- 在Woocommerce购物车,结帐和订单上显示并保存添加的自定义购物车项目数据;
- 在WooCommerce单一产品和购物车页面中输出自定义字段值;
- 在 WooCommerce 单一产品和购物车页面中输出自定义字段值;
- WooCommerce:避免结帐如果未填写所有购物车自定义字段;
- 根据Woocommerce中购物车项目的数量添加自定义结帐字段;
- 根据商品自定义字段的最高值在购物车和结帐页面上添加通知;
- 在购物车页面而非结帐页面中显示其他信息;
- 在结帐WooCommerce页面上获取购物车产品ID,以显示产品图片;
- 在结帐 WooCommerce 页面上获取购物车产品 ID,以显示产品图片;
- 在Woocommerce购物车和结帐中显示产品缩略图和属性;
- woocommerce在结帐和购物车页面中更改价格;
PHP最新文章
- 请求头字段Access-Control-Allow-Headers在预检响应中不允许Access-Control-Allow-Headers;
- 路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误;
- 什么是NCFB和NOFB模式?;
- 警告:mysqli_connect():(HY000 / 1045):访问被拒绝用户'用户名'@'localhost'(使用密码:是);
- 如何处理致命错误:cURL错误7:无法连接到xxxx端口443;
- 参数3传递给GuzzleHttp\Client :: request()必须是数组类型,给定字符串;
- phpMyAdmin的#2054无法登录到MySQL服务器;
- SSL错误SSL3_GET_SERVER_CERTIFICATE:证书验证失败;
- 在PHPExcel中设置字体颜色,字体和字体大小;
- 如何解决cURL错误(7):无法连接到主机?;