2

我们需要在我们的网站上导入很多产品,因此当我们从csv/xml文件中导入时,它不会导入我们网站上没有的类别,这一点非常重要。

我们使用一个名为 WP All Import 的插件,您可以在其中添加过滤器是否应该创建产品,但无论我们做什么,我们都无法使其工作。

代码:这是我们需要修改的代码,当类别路径存在时返回true,当类别路径不存在时返回false。现在,即使我知道类别路径存在,它也一直返回 false。

add_filter('wp_all_import_is_post_to_create', 'wpai_pmxi_single_category', 10, 3);

function wpai_pmxi_single_category( $continue_import, $data, $import_id ) { 
        // here we can check is term exists 
        $term = empty($term_into['parent']) ? term_exists( $term_into['name'], $tx_name, 0 ) : term_exists( $term_into['name'], $tx_name, $term_into['parent'] ); 
        // if term doesn't exists we can return false, so WP All Import will not create it 
        if ( empty($term) and !is_wp_error($term) ) { 
                return false;
        } else {
            return true;
        }
        return $term_into; 
}

简而言之:如果我们网站上已经存在来自特定产品的完整类别路径,我们只想将整个产品/产品导入我们的网站。否则,我们希望它不导入产品并跳过它。

在 github 上,他们有一些示例,其中展示了如何使用应该导入哪些产品的过滤器,这是我们从这里获取过滤器的地方

希望它足够清楚,有人可以在这里帮助我们:)

4

1 回答 1

1

这可能可以以更好的方式完成,但我最终设法做到了:

function map( $erstat ) {
    global $find;
    $find = array("Dame>Mode>Blazere & veste>Elegante blazere", "Dame>Sport & Outdoor>Overtøj>Skibukser", "Default Category > Accessories > Cufflinks & tie bars");
    $replace = array( "Mænd > Accessories > Solbriller", "Mænd > Tøj > Trøjer > Hættetrøjer", "Mænd > Tøj > Trøjer > Hættetrøjer");
    return $replace[array_search($erstat, $find)];
}

function my_is_post_to_create( $continue_import, $data, $import_id ) {
    map( $erstat );
    global $find;
    $awin = $data['merchant_product_category_path'];
    if (in_array( $awin, $find )) return true; 
}
add_filter('wp_all_import_is_post_to_create', 'my_is_post_to_create', 10, 3);

在前面的尝试中,我没有给函数任何可比较的东西,所以我做了两个新函数。第一个是我将我的类别映射到我网站上正确的类别路径的地方。另一个功能是我告诉网站应该导入什么,我告诉它只在数组中导入merchant_product_category_path时才导入csv/xml$find

于 2019-12-02T23:56:49.427 回答