wordpress 添加WooCommerce管理产品自定义字段抛出警告

gk7wooem  于 2023-11-17  发布在  WordPress
关注(0)|答案(1)|浏览(163)

我在子主题functions.php文件中有一些代码,在WooCommerce管理产品选项中添加了几个自定义字段。它已经工作了几年,没有问题,但现在我得到了如下PHP警告:
PHP警告:/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
代码如下:

<?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));
}

字符串
请问,有谁知道需要做什么改变才能摆脱警告?
我已经在网页上隐藏了警告,但我想这将需要在未来的PHP版本中进行纠正,我使用的是PHP 8.1。

yshpjwxd

yshpjwxd1#

您的代码自WooCommerce 3以来有点过时,请尝试以下操作:

// 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']));
    }
}

字符串
代码放在你的子主题的functions.php文件中(或插件中)。测试和工作。
它应该解决PHP警告。

相关问题