更新管理元框字段中的产品发布元数据 [英] Updating product post meta data in admin meta box field

查看:58
本文介绍了更新管理元框字段中的产品发布元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 update_post_meta() 函数更新 WooCommerce 产品元数据,但它不起作用.

这是我的代码:

 函数 woo_add_deal_general_fields_save( $post_id ){$post_id = (int)$post_id;//尝试转换为整数$woocommerce_textarea = $_POST['_deal_textarea'];if( !empty( $woocommerce_textarea ) )如果 ( get_post_meta($post_id, '_deal_textarea', FALSE) ) {$test= update_post_meta($post_id, '_deal_textarea', $woocommerce_textarea );} 别的 {add_post_meta($post_id, '_deal_textarea', $woocommerce_textarea );}var_dump($test);exit;}

如果我使用固定的产品 ID 进行尝试,它会起作用:

$test= update_post_meta(70, '_deal_textarea', $woocommerce_textarea );

为什么它不适用于 $post_id, (int)$post_id, &要么 get_the_ID();?

这是我的代码部分,如函数调用:

//显示字段add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields');//保存字段add_action('woocommerce_process_product_meta', 'woo_add_deal_general_fields_save');函数 woo_add_custom_general_fields() {全球 $woocommerce, $post;$feature_product=get_post_meta(get_the_ID(), '_featured', true );if($feature_product=='yes'){echo '

';//自定义字段将在此处创建...//文本区域woocommerce_wp_textarea_input(大批('id' =>'_deal_textarea','标签' =>__( '交易说明', 'woocommerce' ),'占位符' =>'','说明' =>__( '在此处输入交易产品文本值.(将显示在主页上)', 'woocommerce' )));回声'</div>';}}

谢谢

解决方案

这是您重新访问的经过测试且功能齐全的代码,基于这个答案:

//在常规标签产品页面中插入自定义管理字段add_action('woocommerce_product_options_general_product_data', 'add_deal_custom_general_product_field');函数 add_deal_custom_general_product_field() {全球 $post;$feature_product = get_post_meta( $post->ID, '_featured', true );如果( $feature_product == '是' ){echo '

';woocommerce_wp_textarea_input(数组('id' =>'_deal_textarea','标签' =>__( '交易说明', 'woocommerce' ),'占位符' =>'','说明' =>__( '在此处输入交易产品文本值.(将显示在主页上)', 'woocommerce' )) );回声'</div>';}}//提交时在常规标签产品页面中保存自定义管理字段add_action('woocommerce_process_product_meta', 'save_deal_custom_general_product_field');函数 save_deal_custom_general_product_field( $post_id ){$wc_field = $_POST['_deal_textarea'];$feature_product = get_post_meta( $post_id, '_featured', true );if(!empty($wc_field) && $feature_product == 'yes')update_post_meta( $post_id, '_deal_textarea', esc_attr( $wc_field ) );}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

此代码已经过测试并有效

I am trying to update WooCommerce product meta data using update_post_meta() function, but it does''t work.

Here is my code:

 function woo_add_deal_general_fields_save( $post_id ){
     $post_id = (int)$post_id; // tried to convert into integer 
    $woocommerce_textarea = $_POST['_deal_textarea'];

    if( !empty( $woocommerce_textarea ) ) 
    if ( get_post_meta($post_id, '_deal_textarea', FALSE ) ) { 
     $test=   update_post_meta($post_id, '_deal_textarea', $woocommerce_textarea );
    } else {     
        add_post_meta($post_id, '_deal_textarea',  $woocommerce_textarea  );
    }

         var_dump($test);exit;

}

If I try it with a fixed product ID, it works:

$test=   update_post_meta(70, '_deal_textarea', $woocommerce_textarea );

Why its not working with $post_id, (int)$post_id, & either get_the_ID();?

Here is the part of my code like function calls:

// 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_deal_general_fields_save' );

function woo_add_custom_general_fields() {

  global $woocommerce, $post;

$feature_product=get_post_meta(get_the_ID(), '_featured', true );
 if($feature_product=='yes'){

  echo '<div class="options_group">';

  // Custom fields will be created here...
  // Textarea
woocommerce_wp_textarea_input( 
    array( 
        'id'          => '_deal_textarea', 
        'label'       => __( 'Deal Caption', 'woocommerce' ), 
        'placeholder' => '', 
        'description' => __( 'Enter the Deal Product Text value here. (will be shown on home page)', 'woocommerce' ) 
    )
);
  echo '</div>';
 }

}

Thanks

解决方案

Here is your revisited tested and fully functional code, based on this answer:

// Inserting a Custom Admin Field in general tab products pages
add_action( 'woocommerce_product_options_general_product_data', 'add_deal_custom_general_product_field' );
function add_deal_custom_general_product_field() {
    global $post;

    $feature_product = get_post_meta( $post->ID, '_featured', true );

    if( $feature_product == 'yes' ){

        echo '<div class="options_group">';

        woocommerce_wp_textarea_input( array(
            'id'                => '_deal_textarea',
            'label'             => __( 'Deal Caption', 'woocommerce' ),
            'placeholder'       => '',
            'description'       => __( 'Enter the Deal Product Text value here. (will be shown on home page)', 'woocommerce' )
        ) );

        echo '</div>';

    }
}

// Saving the Custom Admin Field in general tab products pages when submitted
add_action( 'woocommerce_process_product_meta', 'save_deal_custom_general_product_field' );
function save_deal_custom_general_product_field( $post_id ){

$wc_field = $_POST['_deal_textarea'];

$feature_product = get_post_meta( $post_id, '_featured', true );

if( !empty($wc_field) && $feature_product == 'yes')
    update_post_meta( $post_id, '_deal_textarea', esc_attr( $wc_field ) );
}

The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works

这篇关于更新管理元框字段中的产品发布元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆