我认为这是一个非常容易的修复-但是每次我陷入困境时-我都遇到了WordPress严重错误,而且我不知道自己在做什么错-对Hooks来说是陌生的,所以请原谅。 / p>
我试图只打印我在后端保存的值,以传递到前端。
提前谢谢!
(此代码在functions.php中)
//Custom Colour Text Box
add_action( 'woocommerce_product_options_inventory_product_data', 'wc_custom_add_custom_fields' );
function wc_custom_add_custom_fields() {
// Print a custom text field
woocommerce_wp_text_input( array(
'id' => '_custom_text_field',
'label' => 'Product Colour',
'description' => 'This is where you put the colour of the product in.',
'desc_tip' => 'true',
'placeholder' => 'Custom Colour'
) );
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_save_custom_fields' );
function wc_custom_save_custom_fields( $post_id ) {
if ( ! empty( $_POST['_custom_text_field'] ) ) {
update_post_meta( $post_id, '_custom_text_field', esc_attr( $_POST['_custom_text_field'] ) );
}
}
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title', 2 );
function custom_field_display_below_title(){
global $product;
// Get the custom field value
$custom_field = get_post_meta( $product->get_id(), '_custom_product_text_field', true );
// Display
if( ! empty($custom_field) ){
echo '<p class="my-custom-field">'.$custom_field.'</p>';
}
}
/**** Display on the Product Page ***/
add_action( 'woocommerce_single_product_summary', 'custom_text', 9 );
function custom_text() {
print '<p class="my-custom-field"></p>';
}
答案 0 :(得分:1)
我没有收到错误,我在这里和那里做了微小的改动。
if ( ! empty( $_POST[..
仅在该字段不为空时有效,如果未填写任何内容,则不会保存。get_post_meta( $product->get_id(), '_custom_product_text_field',...
_custom_product_text_field
与_custom_text_field
woocommerce_single_product_summary
,您仅打印文本,缺少相应的代码//Custom Colour Text Box
add_action( 'woocommerce_product_options_inventory_product_data', 'wc_custom_add_custom_fields' );
function wc_custom_add_custom_fields() {
// Print a custom text field
woocommerce_wp_text_input( array(
'id' => '_custom_text_field',
'label' => 'Product Colour',
'description' => 'This is where you put the colour of the product in.',
'desc_tip' => 'true',
'placeholder' => 'Custom Colour'
) );
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_save_custom_fields', 10, 1 );
function wc_custom_save_custom_fields( $post_id ) {
$product = wc_get_product( $post_id );
$my_text_field = isset( $_POST['_custom_text_field'] ) ? $_POST['_custom_text_field'] : '';
$product->update_meta_data( '_custom_text_field', sanitize_text_field( $my_text_field ) );
$product->save();
}
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title' );
function custom_field_display_below_title() {
global $post;
// Get product
$product = wc_get_product( $post->ID );
// Check for the custom field value
$my_text_field = $product->get_meta( '_custom_text_field' );
// Display
if( $my_text_field ) {
echo '<p class="my-custom-field">' . $my_text_field . '</p>';
}
}
/**** Display on the Product Page ***/
add_action( 'woocommerce_single_product_summary', 'custom_text' );
function custom_text() {
global $post;
// Get product
$product = wc_get_product( $post->ID );
// Check for the custom field value
$my_text_field = $product->get_meta( '_custom_text_field' );
// Display
if( $my_text_field ) {
echo '<p class="my-custom-field">' . $my_text_field . '</p>';
}
}