You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义函数创建WooCommerce产品后价格无法在产品表显示的问题排查

WooCommerce产品价格不显示问题修复

问题原因

你的代码中手动将_price元字段设置为空字符串:

update_post_meta($post_id, '_price', '');

WooCommerce产品列表依赖_price字段展示价格,而后台编辑更新时,系统会自动同步_regular_price的值到_price,所以只有更新后价格才会正常显示。

修复方案

删除上述设置_price为空的代码行,或者将_price的值设置为与_regular_price一致的数值。

修改后的完整代码

function create_simple_product($array = array(), $upgrade = false)
{
    $user_id = get_current_user();

    if ($upgrade) {
        $array = array_map('convertNameAndSKU', $array);
    }
    $post_ids = [];
    foreach ($array as $key => $v) {

        $title = $v['name'];
        if ($upgrade) {
            $title = '+ Additional ' . $title;
        }

        if (!post_exists($title)) {

            $post_id = wp_insert_post(array(
                'post_author' => $user_id,
                'post_title' => $title,
                'post_content' => '',
                'post_status' => 'publish',
                'post_type' => "product",
                'post_category' => array(74)
            ));

            $price = $v['price'];
            if ($upgrade) {
                if ($price > 1) {
                    $price = $price / 2;
                }
                $price = ($price * 75) / 100;
            }
            $formatted_price = number_format($price, 3);

            wp_set_object_terms($post_id, 'simple', 'product_type');
            update_post_meta($post_id, '_visibility', 'visible');
            update_post_meta($post_id, '_stock_status', 'instock');
            update_post_meta($post_id, 'total_sales', '0');
            update_post_meta($post_id, '_downloadable', 'no');
            update_post_meta($post_id, '_virtual', 'yes');
            update_post_meta($post_id, '_regular_price', $formatted_price);
            update_post_meta($post_id, '_price', $formatted_price); // 同步价格到_price字段
            update_post_meta($post_id, '_sale_price', '');
            update_post_meta($post_id, '_purchase_note', '');
            update_post_meta($post_id, '_featured', 'no');
            update_post_meta($post_id, '_weight', '');
            update_post_meta($post_id, '_length', '');
            update_post_meta($post_id, '_width', '');
            update_post_meta($post_id, '_height', '');
            if ($upgrade) {
                update_post_meta($post_id, '_sku', $v['sku'] . '-' . 'upgrade');
            } else {
                update_post_meta($post_id, '_sku', $v['sku']);
            }
            update_post_meta($post_id, '_product_attributes', array());
            update_post_meta($post_id, '_sale_price_dates_from', '');
            update_post_meta($post_id, '_sale_price_dates_to', '');
            update_post_meta($post_id, '_sold_individually', '');
            update_post_meta($post_id, '_manage_stock', 'no');
            update_post_meta($post_id, '_backorders', 'no');
            update_post_meta($post_id, '_stock', '');

            array_push($post_ids, array(
                'count' => (int) explode('-', $v['name'])[1],
                'pricing' => number_format($v['price']),
                'link' => '/?add-to-cart=' . $post_id,
                'popular' => false,
                'discountpercentage' => 25,
            ));
        }
    }

    echo '<pre>';
    print_r($post_ids);
    echo '</pre>';
}

内容的提问来源于stack exchange,提问作者Robin Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 01:53:20