我们需要在我们的网站上导入很多产品,因此当我们从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 上,他们有一些示例,其中展示了如何使用应该导入哪些产品的过滤器,这是我们从这里获取过滤器的地方
希望它足够清楚,有人可以在这里帮助我们:)