将所有产品分配到一个类别中。

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

WooCommerce: assign a category to all products

问题

我已经将数万种产品导入到Woocommerce,但它们没有分配到任何类别。
如何将一个类别(例如默认类别)分配给我的目录中的所有产品?
谢谢

当然,使用后台进行操作会太长,我正在寻找SQL查询或者可能是插件的解决方案。

英文:

I have imported tens of thousands of products into Woocommerce but they are not assigned to any category.
How can I assign a category (for example the default category) to all the products in my catalog?
Thanks

The option to do it with the back office would be much too long of course, I'm looking for the SQL query instead, or a plugin maybe?

答案1

得分: 1

UPDATE wp_term_relationships AS tr
JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_terms AS t ON tt.term_id = t.term_id
JOIN wp_posts AS p ON tr.object_id = p.ID
SET tt.term_taxonomy_id = {your_category_id}
WHERE tt.taxonomy = 'product_cat'
AND p.post_type = 'product';

将 "wp_" 替换为您的前缀(如果不同)。
将 {your_category_id} 替换为您的目标类别的ID。

另一种方法是使用一个函数

function assign_all_products_to_category() {
$category_id = 123; // 替换为您目标产品类别的实际ID

// 获取所有产品ID
$args = array(
    'post_type' => 'product',
    'posts_per_page' => -1,
);
$products = get_posts( $args );

if ( $products ) {
    foreach ( $products as $product ) {
        $product_id = $product->ID;
        wp_set_object_terms( $product_id, $category_id, 'product_cat', true );
    }
    echo '所有产品已成功分配到类别。';
} else {
    echo '未找到任何产品。';
}

}

根据需要执行该函数。

例如,将其作为URL 运行

function check_custom_param() {
if ( isset( $_GET['run_function'] ) && $_GET['run_function'] === 'assign_products' ) {
assign_all_products_to_category();
exit; // 在运行函数后终止脚本执行
}
}
add_action( 'template_redirect', 'check_custom_param' );

您的URL 应如下所示:http://example.com/?run_function=assign_products

英文:

Something like that should do the trick. Backup before doing it !

UPDATE wp_term_relationships AS tr
JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_terms AS t ON tt.term_id = t.term_id
JOIN wp_posts AS p ON tr.object_id = p.ID
SET tt.term_taxonomy_id = {your_category_id}
WHERE tt.taxonomy = 'product_cat'
  AND p.post_type = 'product';

Replace wp_ with your prefix if its different.
Replace {your_category_id} with the ID of the category you aim.

Another way is using a function

function assign_all_products_to_category() {
    $category_id = 123; // Replace with the actual ID of your target product category

    // Get all product IDs
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,
    );
    $products = get_posts( $args );

    if ( $products ) {
        foreach ( $products as $product ) {
            $product_id = $product->ID;
            wp_set_object_terms( $product_id, $category_id, 'product_cat', true );
        }
        echo 'All products have been assigned to the category successfully.';
    } else {
        echo 'No products found.';
    }
}

Execute the function as you see fit.

For example run it as url

function check_custom_param() {
    if ( isset( $_GET['run_function'] ) && $_GET['run_function'] === 'assign_products' ) {
        assign_all_products_to_category();
        exit; // Terminate script execution after running the function
    }
}
add_action( 'template_redirect', 'check_custom_param' );

Your url should look like: http://example.com/?run_function=assign_products

huangapple
  • 本文由 发表于 2023年6月19日 05:32:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502600.html
匿名

发表评论

匿名网友

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

确定