Update / Save Post Meta – PHP – WordPress

I’m using a plugin on Woocommerce that adds a new metabox with editable quantity number fields onto the admin back-end.
My theme allows for front-end product posting and so I want to add bring some of these fields to the front-end.

I have managed to add the fields in my template file by using :

Read More
<?php 
$min   = get_post_meta( $post->ID, '_wpbo_minimum',  true );
$max   = get_post_meta( $post->ID, '_wpbo_maximum',  true );
?>

<label for="_wpbo_minimum">Minimum Quantity</label>
<input type="number" name="_wpbo_minimum" value="<?php echo $min; ?>" />

<label for="_wpbo_maximum">Maximum Quantity</label>
<input type="number" name="_wpbo_maximum" value="<?php echo $max; ?>" />

Both fields are showing on my front-end product edit form and getting their values from the backend if these were previously filled in.

But now I am struggling with updating and saving the fields with updated values
from front-end or saving new values from front-end if these values were not previously filled in.

I spent hours finding some tutorial on this, I think I should be concentrating on the following code :

update_post_meta($post->ID, '_wpbo_minimum', $_POST['_wpbo_minimum']);
update_post_meta($post->ID, '_wpbo_maximum', $_POST['_wpbo_maximum']);

But I can’t figure out if this is correct and where should this code be placed in order to save or update my fields.

I tried placing a function into my functions.php file that looks like this :

/* Update Minimum Qty field */ 
add_action('save_post', 'save_min_qty');

function save_min_qty($post_id)
{
        if(get_post_type($post_id) != "VA_LISTING_PTYPE")
        return;
    $min   = get_post_meta( $post->ID, '_wpbo_minimum',  true );
    if ( isset( $_POST['_wpbo_minimum'] )) {
                $min  = $_POST['_wpbo_minimum'];
            }

            if( isset( $_POST['_wpbo_minimum'] )) {
                if ( $min != 0 ) {
                    $min = wpbo_validate_number( $min );
                }
                update_post_meta( 
                    $post_id, 
                    '_wpbo_minimum', 
                    strip_tags( $min )
                );
}

But this just doesn’t seem to be the winner.

Would you be please able to point me to the right direction ?

Below is the code from the actual original plugin that creates metabox in the back-end, I am not sure if it’s any relevant to what I need to do in front-end and what part of this and where should I be using it :

<?php
if ( ! class_exists( 'IPQ_Quantity_Meta_Boxes' ) ) :
class IPQ_Quantity_Meta_Boxes {
    public function __construct() {
        add_action( 'save_post', array( $this, 'save_quantity_meta_data' ) );
    }


    /* Handle Saving Meta Box Data */   
    public function save_quantity_meta_data( $post_id ) {

        // Validate Post Type
        if ( ! isset( $_POST['post_type'] ) or $_POST['post_type'] !== 'product' ) {
            return;
        }

        // Validate User
        if ( !current_user_can( 'edit_post', $post_id ) ) {
            return;
        }

        // Verify Nonce
        if ( ! isset( $_POST["_wpbo_product_rule_nonce"] ) or ! wp_verify_nonce( $_POST["_wpbo_product_rule_nonce"], plugin_basename( __FILE__ ) ) ) {
            return;
        }

        // Update Rule Meta Values
        if ( isset( $_POST['_wpbo_minimum'] )) {
            $min  = $_POST['_wpbo_minimum'];
        }

        if( isset( $_POST['_wpbo_minimum'] )) {
            if ( $min != 0 ) {
                $min = wpbo_validate_number( $min );
            }
            update_post_meta( 
                $post_id, 
                '_wpbo_minimum', 
                strip_tags( $min )
            );
        }

        /* Make sure Max > Min */
        if( isset( $_POST['_wpbo_maximum'] )) {
            $max = $_POST['_wpbo_maximum'];
            if ( isset( $min ) and $max < $min and $max != 0 ) {
                $max = $min;
            }

            update_post_meta( 
                $post_id, 
                '_wpbo_maximum', 
                strip_tags( wpbo_validate_number( $max ) )
            );
        }       
    }
}
endif;

Related posts

1 comment

  1. Everything is correct except that you are checking for the wrong post_type – it should be “product” not “VA_LISTING_PTYPE”. Your function hooked to save_post is returning early because the post type is not correct.

    Incorrect

    if(get_post_type($post_id) != "VA_LISTING_PTYPE")
    return;
    

    Correct

    if (get_post_type($post_id) != "product"){
        // bail early if we aren't updating a WooCommerce Product
        return;
    }
    

    You can see the same check in the plugin you are extending here:

    https://github.com/wpbackoffice/woocommerce-incremental-product-quantities/blob/master/includes/class-ipq-product-meta-box.php#L222

Comments are closed.