Iam working with lots of custom_fields in WordPress.
Currently Iam working with WooCommerce, I dont exactly know if my question is WP or just Woo related.
I have one custom-field setup as a select box. I can choose between several items such as:
- new
- in_stock
- sold_out
When I select "sold_out" and save the post/product, I not only want to save this field, but I also want to set the "_stock_status" to "outofstock".
The field "_stock_status" is an default WooCommerce field. It is also a drop-down box. You can select the values "instock" or "outofstock".
The thing is Iam working with a WooCommerce save function, its called, woocommerce_process_product_meta
.
I thought that I could just run two update_post_meta
function. But that doesnt work.
I tried the following, for testing, check if my custom-field is not empty. If it is NOT empty update it with the selected value, and also update the "_stock_status".
$woocommerce_select = $_POST['_my_custom_field'];
if( !empty( $woocommerce_select ) ) {
update_post_meta( $post_id, '_my_custom_field', esc_attr( $woocommerce_select ) );
update_post_meta( $post_id, '_stock_status', 'outofstock' );
}
With this function I can save _my_custom_field, but the _stock_status dont change.
I also tried variations of this, and other if-functions. But it seems that I just cant update that field like this way.
$woocommerce_select = $_POST['_my_custom_field'];
if( $woocommerce_select == 'sold_out' ) {
update_post_meta( $post_id, '_my_custom_field', esc_attr( $woocommerce_select ) );
update_post_meta( $post_id, '_stock_status', 'outofstock' );
} else {
update_post_meta( $post_id, '_my_custom_field', esc_attr( $woocommerce_select ) );
}
Dont know what Iam doing wrong here, maybe somebody can point me to it.
Thanks, Mo
Update: Function/Hook added:
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_select = $_POST['_my_custom_field'];
if( !empty( $woocommerce_select ) ) {
update_post_meta( $post_id, '_my_custom_field', esc_attr( $woocommerce_select ) );
update_post_meta( $post_id, '_stock_status', 'outofstock' );
}
}
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save', 999 );
I also tried it with a lower or no priority.
This is how I create the custom field:
function woo_add_custom_general_fields() {
global $woocommerce, $post; ?>
<div class="options_group">
<p class="form-field custom_stock">
<label for="custom_stock"><?php echo __( 'Custom Stock', 'aat-net-theme' ); ?></label>
<span class="wrap">
<?php $custom_stock = get_post_meta( $post->ID, '_my_custom_field', true ); ?>
<select id="custom_stock" name="_my_custom_field">
<option value="" <?php selected( $custom_stock, '' ); ?>> - Select Stock - </option>
<option value="new" <?php selected( $custom_stock, 'new' ); ?>>New</option>
<option value="in_stock" <?php selected( $custom_stock, 'in_stock' ); ?>>In Stock</option>
<option value="on_request" <?php selected( $custom_stock, 'on_request' ); ?>>On Request</option>
<option value="in_transit" <?php selected( $custom_stock, 'in_transit' ); ?>>In Transit</option>
<option value="not_available" <?php selected( $custom_stock, 'not_available' ); ?>>Not Available</option>
</select>
</span>
<span class="description"><?php _e( 'Select the custom stock-status here.', 'aat-net-theme' ); ?></span>
</p>
<?php }
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
maybe this will be helpfull for somebody else.
I tried many different things, at some point I deactivated my meta-box save function and the fields would still save without a problem.
It seems that my problem was caused by using the 2 default WooCommerce actions to create and save my fields / meta-box.
and
Since I couldnt find a solution I now created a normal WordPress meta-box like this:
I than created the callback function like so:
And the save function like this:
With this code I can resave the product stock along with my custom field. It was not possible for me with the native WooCoommerce functions.