1

I have the following code provided to me from a plugin developer, but they are not very responsive and don't seem to test properly using latest 8.x PHP

I've been able to get things to work except when someone enters data into one of the custom fields or updates the data which is pulled back from the database, it doesn't save the change.

Background:

  • I have code which creates two custom fields on a product in Woocommerce called 'file_size' and 'image_dimensions'. Everything works in woocommerce, so viewing the product displays the custom fields and its values, same with updating those values it saves the data and the displays the change, even on refresh. So this side is good.

  • The code below is specific to a page from the plugin multivendorx .... the code works by adding a tab to a section of data and displaying the fields. It also retrieves the correct data from the database. So this is all good.

  • What the code does not do is when a user makes changes to the existing data or adds data into either of those two custom fields and then clicks update, the data is not saved to the database.

I don't have the developer skills to know if the 'save' portion of this code is up to date or even makes sense. Can anyone assist in understanding if the code needs to change?

/**
* Add Custom Tab in add product page.
* @Version 3.3.0
*/
function add_custom_product_data_tabs( $tabs ) {   
    $tabs['image_details'] = array(
        'label'    => __( 'Image Details', 'your-text-domain' ),       
        'target'   => 'custom_tab_product_data',
        'class'    => array(),
        'priority' => 100,   
    );   
    return $tabs;
}
add_filter( 'mvx_product_data_tabs', 'add_custom_product_data_tabs' );

// Now, to add content inside the custom tab, use the following code.
function add_custom_product_data_content( $pro_class_obj, $product, $post ) {  
    // Retrieve the values from the database
    $file_size = get_post_meta( $product->get_id(), 'file_size', true );
    $image_dimensions = get_post_meta( $product->get_id(), 'image_dimensions', true );  
    ?>   
    <div role="tabpanel" class="tab-pane fade" id="custom_tab_product_data"><!-- just make sure tabpanel id should replace with your added tab target -->       
        <div class="row-padding">           
            <div class="form-group">               
                <label class="control-label col-sm-3 col-md-3">File Size (i.e. 2MB):</label>               
                <div class="col-md-6 col-sm-9">                   
                    <input type="text" name="file_size" class="form-control" value="<?php echo esc_attr( $file_size ); ?>" />               
                </div>           
            </div>
            <div class="form-group">               
                <label class="control-label col-sm-3 col-md-3">Image Dimensions (i.e. 800x600):</label>               
                <div class="col-md-6 col-sm-9">                   
                    <input type="text" name="image_dimensions" class="form-control" value="<?php echo esc_attr( $image_dimensions ); ?>" />               
                </div>           
            </div>       
        </div>   
    </div>   
    <?php
}
add_action( 'mvx_product_tabs_content', 'add_custom_product_data_content', 10, 3 );

// Save the custom field values when the product is saved
function save_custom_product_data( $post_id ) {
    // Check if the custom fields values are set and save them
        if ( isset( $_POST['file_size'] ) ) {
        $file_size = sanitize_text_field( $_POST['file_size'] );
        update_post_meta( $post_id, 'file_size', $file_size );
    
        // Log the post_id and file_size value
        $logger = wc_get_logger();
        $context = array( 'source' => 'custom-fields' );
        $logger->info( 'Post ID: ' . $post_id . ' - File Size: ' . $file_size, $context );
    }

    if ( isset( $_POST['image_dimensions'] ) ) {
        $image_dimensions = sanitize_text_field( $_POST['image_dimensions'] );
        update_post_meta( $post_id, 'image_dimensions', $image_dimensions );

        // Log the post_id and image_dimensions value
        $logger = wc_get_logger();
        $context = array( 'source' => 'custom-fields' );
        $logger->info( 'Post ID: ' . $post_id . ' - Image Dimensions: ' . $image_dimensions, $context );
    }
}
add_action( 'woocommerce_process_product_meta', 'save_custom_product_data', 10, 1 );

tried ChatGPT for support and google, but to no avail.

4
  • 1
    As you say that "The code below is specific to a page from the plugin multivendor", your last function (that saves the data) should be hooked in a custom hook (from multivendor plugin related to that page), but not in woocommerce_process_product_meta which is related to WooCommerce admin product pages. So ask the plugin support, as nobody can guess that by magic. Commented Jul 10 at 6:58
  • Thanks - All it's doing is saving whats been entered into the custom field into the woocommerce database so wasn't sure if it required any plugin-specific code?
    – StevieK
    Commented Jul 11 at 7:44
  • Try to ask the plugin support, as nobody can guess that. Commented Jul 11 at 8:05
  • Thank you, very much appreciate your time. I have the solution which I will add here.
    – StevieK
    Commented Jul 13 at 6:43

1 Answer 1

0

The following additional code resolved the issue. The hook can be seen in the plugin code as well.

The hook required is mvx_process_product_object:

function save_custom_product_data( $product, $data ) {
    if ( isset( $_POST['file_size'] ) ) {
        update_post_meta( $product->get_id(), 'file_size', sanitize_text_field( $_POST['file_size'] ) );
    }

    if ( isset( $_POST['image_dimensions'] ) ) {
        update_post_meta( $product->get_id(), 'image_dimensions', sanitize_text_field( $_POST['image_dimensions'] ) );
    }

}
add_action( 'mvx_process_product_object', 'save_custom_product_data', 10, 2 );

Not the answer you're looking for? Browse other questions tagged or ask your own question.