Woocommerce / WP All Import:如果产品名称包含类别名称,则自动将产品添加到类别

Woocommerce / WP All Import: automatically add product to category if product name contains the category name

如果产品名称包含类别(例如产品名称是:Awesome brand baseball bat,产品应自动添加到类别棒球棒),我想自动将其添加到类别中。是否有可以自动执行此操作甚至更好的插件:是否可以向 WP All Import 添加规则来执行此操作?

设置类别喜欢

$product->set_category_ids([ 300, 400 ] );

应该不是问题,但我如何才能将文章名称与我的所有类别进行比较,以便自动将产品添加到它们中?

  1. 通过get_product_categories( $fields );

    加载所有产品类别
  2. 使用get the product name查找产品名称

  3. 遍历类别并compare each of them to the product name. Depending on your situation and what values are in product category title or product names, you may need to use a regex为此

完成后,您的代码应如下所示:

$product_category_list = $product->get_categories();
$product_name = $product->get_name();
$categories_to_put_product_in = array();

foreach($product_category_list as $current_category) {
    if (strpos($product_name, $current_category->term_id) !== false) {
        $categories_to_put_product_in[] = $current_category;
    }
}
$product->set_category_ids($categories_to_put_product_in);