Laravel WooCommerce – 使用 DB Facade 插入到分类中

huangapple go评论104阅读模式
英文:

Laravel WooCommerce - Using the DB Facade to insert into categories

问题

我遇到了使用Laravel创建WooCommerce产品类别(product_cats)的问题。我正在创建一个大型数据源的处理过程,并使用Laravel进行处理。我可以使用Laravel Corcel,但它不具备我需要的所有功能。

我需要建立父子关系,以便能够附加类别并在WordPress管理界面中查看它们。

下面是相关的代码,但它创建了类别,但它们未附加到产品上,也无法在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])) {

                    // Check if the term already exists
                    $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])
                        ]);

                        // Insert into the term_taxonomy table
                        $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("Obtained the category IDs:", $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,  // This assumes you want the default order.
                    ]);
                }
            }

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

日志输出了正确的ID,但我无法在WooCommerce中看到任何内容。

[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
)
英文:

I am having issues with creating WooCommerce product_cats using laravel. I am creating the process of a large feed and using laravel for it. I could use laravel corcel, but it doesn't have all the facilities that I need.

I need to have parent child relationships, to be able to attach the categories and see them in the WordPress admin.

The code is creating the categories, but they are not attaching to the products, and not viewable/manipulatable within the wordpress admin area.

 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])) {
// Check if the term already exists
$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])
]);
// Insert into the term_taxonomy table
$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("Obtained the category IDs:", $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,  // This assumes you want the default order.
]);
}
}
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]);
}
}

The logs are outputting the correct IDs, I just can not see anything in WooCommerce.

[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
)

答案1

得分: 1

抱歉你要处理数据库连接的方式让我感到遗憾。我真的会尝试将其抽象化。无论如何,在你列出的函数中,$parent_id 在开始的块中没有被设置为任何值...

// 插入到 term_taxonomy 表格中
$connection->table('term_taxonomy')->insert([
    'term_id' => $term_id,
    'taxonomy' => 'product_cat',
    'parent' => $parent_id,
]);
英文:

man I feel sorry for you having to handle the db connection like that. I would really attempt to abstract that. however anyways. given that you only listed functions $parent_id is not being set to anything in the beginning blocks...

// Insert into the term_taxonomy table
$connection->table('term_taxonomy')->insert([
'term_id' => $term_id,
'taxonomy' => 'product_cat',
'parent' => $parent_id,
]);

huangapple
  • 本文由 发表于 2023年8月5日 16:48:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定