将自定义字段添加到产品库存选项卡,并在 WooCommerce 中的单个产品和商店存档页面上显示价值
Add custom field to product inventory tab and display value on single product and shop archive pages in WooCommerce
我认为这是一个非常简单的修复 - 但每次我放入钩子时 - 我都会收到 WordPress 严重错误,我不知道我做错了什么 - Hooks 新手,所以请原谅我.
我试图只打印我在后端保存的值,以传递到前端。有什么建议吗?
此代码在 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>';
}
我没有收到错误,我在这里和那里做了一些小改动。
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>';
}
}
我认为这是一个非常简单的修复 - 但每次我放入钩子时 - 我都会收到 WordPress 严重错误,我不知道我做错了什么 - Hooks 新手,所以请原谅我.
我试图只打印我在后端保存的值,以传递到前端。有什么建议吗?
此代码在 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>';
}
我没有收到错误,我在这里和那里做了一些小改动。
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>';
}
}