Php 从导入时的产品标签自动分配产品类别

Php 从导入时的产品标签自动分配产品类别,php,wordpress,woocommerce,taxonomy-terms,wpallimport,Php,Wordpress,Woocommerce,Taxonomy Terms,Wpallimport,我尝试在使用WP All Imports插件导入产品时,从产品标签自动设置Woocommerce产品类别 例如:我正在导入特定的面包屑段塞作为产品标签:Sports/Shoes/Running Shoes>现在我想将该段塞连接到预定义的类别层次结构: 标签:运动/鞋/跑步鞋=>产品类别:鞋/跑步鞋 也许有一个插件可以在预先定义的产品类别中放置标签,以显示类别中选定的产品 我发现以下代码非常接近我实现目标所需的代码: function auto_add_category ($product_id

我尝试在使用WP All Imports插件导入产品时,从产品标签自动设置Woocommerce产品类别

例如:我正在导入特定的面包屑段塞作为产品标签:Sports/Shoes/Running Shoes>现在我想将该段塞连接到预定义的类别层次结构:

标签:运动/鞋/跑步鞋=>产品类别:鞋/跑步鞋

也许有一个插件可以在预先定义的产品类别中放置标签,以显示类别中选定的产品

我发现以下代码非常接近我实现目标所需的代码:

function auto_add_category ($product_id = 0) {

    if (!$product_id) return;

    // because we use save_post action, let's check post type here
    $post_type = get_post_type($post_id);
    if ( "product" != $post_type ) return;

    $tag_categories = array (
        'ring' => 'Jewellery'
        'necklace' => 'Jewellery',
        'dress' => 'Clothing',
    );

    // get_terms returns ALL terms, so we have to add object_ids param to get terms to a specific product
    $product_tags = get_terms( array( 'taxonomy' => 'product_tag', 'object_ids' => $product_id ) );
    foreach ($product_tags as $term) {
        if ($tag_categories[$term->slug] ) {
            $cat = get_term_by( 'name', $tag_categories[$term->slug], 'product_cat' );
            $cat_id = $cat->term_id;
            if ($cat_id) {
                $result =  wp_set_post_terms( $product_id, $cat_id, 'product_cat', true );
            }
        }
    }
}
add_action('save_post','auto_add_category');

首先,你需要自己理解逻辑。 对于我来说,还不清楚如何过滤标签集合中的哪一个应该像前面描述的那样组织

了解逻辑后,可以使用导入设置中的“函数编辑器”字段。 在屏幕底部,有所有导入的映射

您可以将函数放在那里,并在标记字段中使用它。例如:

您具有以下功能:

<?php
function import_rearrange_tags( $tag, $count = 1 ) {
  // Some code here
  return $tag;
}
?>

我使用第二个参数来显示整个想法。

例如:我正在导入特定的面包屑段塞作为产品标签:Sports/Shoes/Running Shoes>现在我想将该段塞连接到预定义的类别层次结构:Tag:Sports/Shoes/Running Shoes=>Product cat:Shoes/Running可能有一个插件可以在predifined中放置标签产品类别是否显示类别中选定的产品?我不理解你的问题。通常面包屑的意思正是-它们应该显示你的层级当前位置。所以你只需要调整好它们,它们就可以正常工作了。标签需要结合不同类别的产品,所以它们不应该出现在面包屑中,但它们有自己的页面。
[import_rearrange_tags({tags[1]}, 3)]