php自定义添加字段,php-WooCommerce:将自定义字段添加到产品变体中

以下代码的问题是它不会显示在实时网站上.非常感谢所有帮助,我今天大部分时间都在尝试找出我做错了什么.

在Woocommerce中的“简单产品”和“可变产品”页面上添加6个自定义文本字段和1个复选框.这些不是购物者要提供(即填写)的字段,而是我想在产品页面上显示的自定义信息(而不是在标签中).

// Display Fields

add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields

add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields() {

global $woocommerce, $post;

echo '

';

// Custom fields will be created here...

// Text Field

woocommerce_wp_text_input(

array(

'id' => '_ISBN_field',

'label' => __( 'ISBN', 'woocommerce' ),

'placeholder' => '',

'desc_tip' => 'true',

'description' => __( 'ISBN.', 'woocommerce' )

)

);

function woo_add_custom_general_fields_save( $post_id ){

// Customer text ISBN Field

$woocommerce_text_field = $_POST['_ISBN_field'];

if( !empty( $woocommerce_text_field ) )

update_post_meta( $post_id, '_ISBN_field', esc_attr( $woocommerce_text_field ) );

else

update_post_meta( $post_id, '_ISBN_field', '' );

}

// Display Custom Field Value

echo get_post_meta( $post->ID, '_ISBN_field', true );

}

/* WooCommerce */

/* ----------------------------------------------------------------------------------- */

/* Start WooThemes Functions - Please refrain from editing this section */

/* ----------------------------------------------------------------------------------- */

解决方法:

我从不需要打扰woocommerce_product_after_variable_attributes_js,您只需要添加输入然后进行保存即可.

自Remi的文章发表以来,另一件事发生了变化,那就是WooCommerce变体元数据不再打印在< Table>中.元素…,现在是< div>元件.这对于您构建新内容的方式很重要.

这是向变体添加元字段的方法:

// regular variable products

add_action( 'woocommerce_product_after_variable_attributes', 'add_to_variations_metabox', 10, 3 );

add_action( 'woocommerce_save_product_variation', 'save_product_variation', 20, 2 );

/*

* Add new inputs to each variation

*

* @param string $loop

* @param array $variation_data

* @return print HTML

*/

function add_to_variations_metabox( $loop, $variation_data, $variation ){

$custom = get_post_meta( $variation->ID, '_custom', true ); ?>

}

/*

* Save extra meta info for variable products

*

* @param int $variation_id

* @param int $i

* return void

*/

function save_product_variation( $variation_id, $i ){

// save custom data

if ( isset( $_POST['variation_custom_data'][$i] ) ) {

// sanitize data in way that makes sense for your data type

$custom_data = ( trim( $_POST['variation_custom_data'][$i] ) === '' ) ? '' : sanitize_title( $_POST['variation_custom_data'][$i] );

update_post_meta( $variation_id, '_custom', $custom_data );

}

}

标签:php,wordpress,woocommerce

来源: https://codeday.me/bug/20191011/1896147.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值