如何使用“快速编辑”按钮/功能在WooCommerce的管理区域中保存我的自定义“产品”字段值?
我已经在此处初始化了一个名为“ amazon_asin”的自定义产品字段:
function bh_amazon_custom_asin_meta() {
$args = array(
'label' => __( 'Amazon ASIN', 'woocommerce' ),
'placeholder' => __( 'Enter Amazon ASIN value here', 'woocommerce' ),
'id' => 'amazon_ASIN',
'desc_tip' => true,
'description' => __( 'This meta box is for internal only. Please add ASIN value here', 'woocommerce' ),
);
woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_sku', 'bh_amazon_custom_asin_meta');
function bh_amazon_custom_asin_save ( $post_id ) {
//grab the custom ASIN box
if ( isset( $_POST[ 'amazon_ASIN' ] ) ) {
$asin = isset( $_POST[ 'amazon_ASIN' ] ) ? sanitize_text_field( $_POST[ 'amazon_ASIN' ] ) : '';
}
// grab the product
$product = wc_get_product( $post_id );
// save the custom ASIN meta field
$product->update_meta_data( 'amazon_ASIN', $asin );
$product->save();
}
add_action ( 'woocommerce_process_product_meta', 'bh_amazon_custom_asin_save');
然后我在产品部分中填充了此字段。可以使用以下代码在此处快速编辑该值:
add_action('quick_edit_custom_box', 'brians_quick_edit_fields', 10, 2);
function brians_quick_edit_fields( $column_name, $post_type ) {
$asin = get_post_meta( get_the_id(), 'amazon_ASIN', true );
switch( $column_name ) :
case 'featured': {
echo '<fieldset class="inline-edit-col-right"><div class="inline-edit-col"><div class="inline-edit-group wp-clearfix">';
echo '<label class="alignleft">
<input type="checkbox" name="featured">
<span class="checkbox-title">Featured product</span>
</label>';
break;
}
case 'asin': {
echo '<label class="alignleft">
<span class="title">Amazon Asin</span>';
echo '<span class="input-text-wrap"><input type="text" name="amazon" value="' . $asin .'"></span>
</label>';
// for the LAST column only - closing the fieldset element
echo '</div></div></fieldset>';
break;
}
endswitch;
}
显示当前值的管理面板:
但是当我尝试使用快速编辑功能更新或保存新值时,帖子不会更新:
/*
* Quick Edit Save
*/
add_action( 'woocommerce_product_bulk_and_quick_edit', 'brians_quick_edit_save', 10, 2 );
function brians_quick_edit_save( $column_name, $post_type ){
// update the ASIN
if ( isset( $_POST['amazon_ASIN'] ) ) {
echo 'this is set';
update_post_meta( $post_id, 'amazon_ASIN', $_POST['amazon_ASIN'] );
}
}
由于某些原因,isset( $_POST['amazon_ASIN'] )
返回null
或false
。如何通过快速编辑成功更新和保存此值?