在管理元框字段中更新产品发布元数据

时间:2016-10-04 10:43:21

标签: php wordpress woocommerce product meta-boxes

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

这是我的代码:

 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;

}

如果我使用固定的产品ID进行尝试,可以使用

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

为何不使用$post_id, (int)$post_id, & either get_the_ID();

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

// 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>';
 }

}

由于

2 个答案:

答案 0 :(得分:1)

以下是您重新访问的经过测试且功能齐全的代码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 ) );
}

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

此代码经过测试并正常运行

答案 1 :(得分:0)

我不知道错误在哪里,但我简化了一些代码。请试一试:

function woo_add_deal_general_fields_save( $post_id ){

    $woocommerce_textarea = $_POST['_deal_textarea'];

    if( !empty( $woocommerce_textarea ) ) {          
         $test = update_post_meta( $post_id, '_deal_textarea', $woocommerce_textarea );
         var_dump( $test ); exit;           
    }            

}
相关问题