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

Laravel通过DB Facade插入WooCommerce商品分类异常排查

问题:Laravel批量导入WooCommerce商品分类无法关联产品及后台不可见

我正在用Laravel开发WooCommerce批量数据导入流程,需要实现商品分类的父子关系,同时要让分类能关联产品,且在WordPress后台可见可操作。试过Laravel Corcel但满足不了需求,现在代码能创建分类,但分类无法关联到产品,也没法在WordPress后台查看和操作。

相关代码

private function addSections($sections) {
    Log::info("Sections:", $sections);

    $cat_ids = [];

    $category_hierarchy = [
        'Categories',
        'Product Sub-Group',
        'Product Group',
        'Product Sub-Group 1',
        'Product Type'
    ];

    $connection = DB::connection('wordpress');

    foreach ($category_hierarchy as $category) {
        if (isset($sections[$category]) && !$this->skipCategory($sections[$category])) {

            // 检查分类是否已存在
            $term = $connection->table('terms')
                ->where('name', $sections[$category])
                ->first();

            if (!$term) {
                $term_id = $connection->table('terms')->insertGetId([
                    'name' => $sections[$category],
                    'slug' => Str::slug($sections[$category])
                ]);

                // 插入到term_taxonomy表
                $connection->table('term_taxonomy')->insert([
                    'term_id' => $term_id,
                    'taxonomy' => 'product_cat',
                    'parent' => $parent_id,
                ]);
            } else {
                $term_id = $term->term_id;
            }

            $parent_id = $term_id;
            $cat_ids[] = $term_id;
        }
    }

    Log::info("获取到的分类ID:", $cat_ids);
    return $cat_ids;

}

private function skipCategory($categoryName) {
    return $categoryName == 'Unused'
        || stripos($categoryName, "Height") !== false
        || stripos($categoryName, "H:") !== false
        || $categoryName == 'Yes';
}

private function attachTermsToProduct($product_id, $term_ids)
{
    $connection = 'wordpress'; 

    $term_ids = array_filter($term_ids);

    foreach ($term_ids as $term_id) {
        $exists = DB::connection($connection)
            ->table('term_relationships')
            ->where('object_id', $product_id)
            ->where('term_taxonomy_id', $term_id)
            ->count();

        if (!$exists) {
            DB::connection($connection)->table('term_relationships')->insert([
                'object_id' => $product_id,
                'term_taxonomy_id' => $term_id,
                'term_order' => 0,  // 使用默认排序
            ]);
        }
    }

    foreach ($term_ids as $term_id) {
        $count = DB::connection($connection)
            ->table('term_relationships')
            ->where('term_taxonomy_id', $term_id)
            ->count();

        DB::connection($connection)->table('term_taxonomy')
            ->where('term_taxonomy_id', $term_id)
            ->update(['count' => $count]);
    }
}

日志输出

[2023-08-05 08:36:30] local.INFO: Sections: {"Product Group":"Jewellery","Product Sub-Group 1":"Pendants","Product Type":"Gold"} 
[2023-08-05 08:36:30] local.INFO: Obtained the category IDs: ["486","490","488"] 
[2023-08-05 08:36:30] local.INFO: Section IDs:   
[2023-08-05 08:36:30] local.INFO: Array
(
    [0] => 486
    [1] => 490
    [2] => 488
)

内容的提问来源于stack exchange,提问作者Steven Hardy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 04:35:54