【问题标题】:Updating product post meta data in admin meta box field在管理元框字段中更新产品发布元数据
【发布时间】:2017-06-03 07:31:28
【问题描述】:

我正在尝试使用 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>';
 }

}

谢谢

【问题讨论】:

  • 请告诉我们你是如何调用这个函数的。或者更确切地说,var_dump( $post_id );exit; 的结果是什么

标签: php wordpress woocommerce product meta-boxes


【解决方案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 文件中或任何插件文件中。

此代码已经过测试并且可以工作

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-07
      • 2017-12-31
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      相关资源
      最近更新 更多