phpwordpresswoocommerceproductcustom-fields

Added WooCommerce admin product custom fields throwing warning


I have some code in child theme functions.php file, adding a couple of custom fields to WooCommerce Admin Product options. It has worked for a few years with no problem, but now I'm getting PHP warnings as follows:

PHP Warning: Undefined variable $post in /home/xxx/public_html/wp-content/themes/flatsome-cobra/functions.php(65) : eval()'d code on line 4 PHP Warning: Attempt to read property "ID" on null in /home/xxx/wp-content/themes/flatsome-cobra/functions.php(65) : eval()'d code on line 4

Here is the code:

<?php
// Add custom Theme Functions here

// Display Fields
add_action('woocommerce_product_options_inventory_product_data', 'woocommerce_product_custom_fields');
 
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
 
 
function woocommerce_product_custom_fields()
{
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    // Custom Part Number Text Field
    woocommerce_wp_text_input(
        array(
            'id' => '_part_number',
            'placeholder' => 'Part Number (Short SKU)',
            'label' => __('Part Number', 'woocommerce'),
            'desc_tip' => 'true'
        )
    );
    // Custom Part Ref Text Field
    woocommerce_wp_text_input(
        array(
            'id' => '_part_ref',
            'placeholder' => 'Part Ref (Schematic Ref)',
            'label' => __('Part Ref', 'woocommerce'),
            'desc_tip' => 'true'
        )
    );
    echo '</div>';
 
}

function woocommerce_product_custom_fields_save($post_id)
{
    // Custom Part Number Text Field
    $woocommerce_part_number = $_POST['_part_number'];
    if (!empty($woocommerce_part_number))
        update_post_meta($post_id, '_part_number', esc_attr($woocommerce_part_number));
    // Custom Part Ref Text Field
    $woocommerce_part_ref = $_POST['_part_ref'];
    if (!empty($woocommerce_part_ref))
        update_post_meta($post_id, '_part_ref', esc_attr($woocommerce_part_ref));
}

Please, do anyone know what changes are needed to get rid of the warnings?

I've suppressed warnings on the web page, but I guess this will need correcting for future PHP versions, I'm using PHP 8.1.


Solution

  • Your code is a bit outdated since WooCommerce 3, try the following instead:

    // Display Fields
    add_action('woocommerce_product_options_inventory_product_data', 'display_admin_product_custom_input_fields');
    function display_admin_product_custom_input_fields() {
        echo '<div class="product_custom_field">';
    
        woocommerce_wp_text_input( array(
            'id'            => '_part_number',
            'placeholder'   => __('Part Number (Short SKU)', 'woocommerce'),
            'label'         => __('Part Number', 'woocommerce'),
            'desc_tip'      => 'true'
        ) );
    
        woocommerce_wp_text_input( array( 
            'id'            => '_part_ref',
            'placeholder'   => __('Part Ref (Schematic Ref)', 'woocommerce'),
            'label'         => __('Part Ref', 'woocommerce'),
            'desc_tip'      => 'true'
        ) );
    
        echo '</div>';
    }
    
    // Save Field values
    add_action('woocommerce_admin_process_product_object', 'save_admin_product_custom_field_values', 10, 1);
    function save_admin_product_custom_field_values($product) {
        if ( isset($_POST['_part_number'])){
            $product->update_meta_data('_part_number', sanitize_text_field($_POST['_part_number']));
        }
        if ( isset($_POST['_part_ref'])){
            $product->update_meta_data('_part_ref', sanitize_text_field($_POST['_part_ref']));
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    It should solve the PHP warning.