wordpress - Update a custom-field by another custom-field -
iam working lots of custom_fields in wordpress.
currently iam working woocommerce, dont know if question wp or woo related.
i have 1 custom-field setup select box. can choose between several items such as:
- new
- in_stock
- sold_out
when select "sold_out" , save post/product, not want save field, want set "_stock_status" "outofstock".
the field "_stock_status" default woocommerce field. drop-down box. can select values "instock" or "outofstock".
the thing iam working woocommerce save function, called, woocommerce_process_product_meta
.
i thought run 2 update_post_meta
function. doesnt work.
i tried following, testing, check if custom-field not empty. if not empty update selected value, , update "_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 function can save _my_custom_field, _stock_status dont change.
i tried variations of this, , other if-functions. seems cant update field 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 iam doing wrong here, maybe can point me 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 tried lower or no priority.
this how create 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 custom stock-status here.', 'aat-net-theme' ); ?></span> </p> <?php } add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
maybe helpfull else.
i tried many different things, @ point deactivated meta-box save function , fields still save without problem.
it seems problem caused using 2 default woocommerce actions create , save fields / meta-box.
add_action( 'woocommerce_process_product_meta'...
and
add_action( 'woocommerce_product_options_general_product_data'...
since couldnt find solution created normal wordpress meta-box this:
function lwsaat_custom_meta() { add_meta_box( 'lwsaat_meta', // html 'id' attribute of edit screen section __( 'heading', 'lwsaat-plugin' ), // title of edit screen section 'lwsaat_meta_callback', // function prints out html edit screen section 'product', // type of writing screen on show edit screen section 'normal', // part of page edit screen section should shown ('normal', 'advanced', or 'side') 'high' // priority within context boxes should show ('high', 'core', 'default' or 'low') ); } add_action( 'add_meta_boxes', 'lwsaat_custom_meta' );
i created callback function so:
function lwsaat_meta_callback( $post ) { wp_nonce_field( basename( __file__ ), 'lwsaat_nonce' ); $lwsaat_stored_meta = get_post_meta( $post->id ); ?> <p class="form-field custom_stock"> <label for="custom_stock"><?php echo __( 'custom stock', 'lwsaat-plugin' ); ?></label> <?php $custom_stock = $lwsaat_stored_meta['_aat_stock_select'][0]; ?> <select id="custom_stock" name="_aat_stock_select"> <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="not_available" <?php selected( $custom_stock, 'not_available' ); ?>>not available</option> <option value="eu_no_import" <?php selected( $custom_stock, 'eu_no_import' ); ?>>import eu not allowed</option> </select> </p> <?php }
and save function this:
function lwsaat_meta_save( $post_id ) { // checks save status $is_autosave = wp_is_post_autosave( $post_id ); $is_revision = wp_is_post_revision( $post_id ); $is_valid_nonce = ( isset( $_post[ 'lwsaat_nonce' ] ) && wp_verify_nonce( $_post[ 'lwsaat_nonce' ], basename( __file__ ) ) ) ? 'true' : 'false'; // exits script depending on save status if ( $is_autosave || $is_revision || !$is_valid_nonce ) { return; } // save custom stock select $custom_stock = $_post[ '_aat_stock_select' ]; if( $custom_stock == 'new' ) { update_post_meta( $post_id, '_aat_stock_select', $_post[ '_aat_stock_select' ] ); update_post_meta( $post_id, '_stock_status', 'instock' ); } elseif ( $custom_stock == 'not_available' ) { update_post_meta( $post_id, '_aat_stock_select', $_post[ '_aat_stock_select' ] ); update_post_meta( $post_id, '_stock_status', 'outofstock' ); } else { update_post_meta( $post_id, '_aat_stock_select', $_post[ '_aat_stock_select' ] ); } } add_action( 'save_post', 'lwsaat_meta_save' );
with code can resave product stock along custom field. not possible me native woocoommerce functions.
Comments
Post a Comment