WP All Import / Wordpress - 只有存在完整类别路径时才导入产品
WP All Import / Wordpress - Only import product if full category path exist
我们需要在我们的网站上导入很多产品,因此当我们从 csv/xml
文件导入时,它不会导入我们网站上尚不存在的类别,这一点非常重要。
我们使用一个名为 WP All Import 的插件,您可以在其中添加一个过滤器,判断是否应该创建产品,但无论我们做什么,我们都无法让它发挥作用。
代码:这是我们需要修改的代码,当类别路径存在时为return true,当类别路径不存在时为return false .现在它一直都是 return 错误,即使我知道类别路径存在。
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;
}
简而言之: 如果特定产品的完整类别路径已经存在,我们只想将整个 product/products 导入我们的网站我们的网站。否则我们希望它不导入产品并跳过它。
在 github 他们有一些示例,其中展示了如何使用应该导入哪些产品的过滤器,这是我们从 here
获得过滤器的地方
希望它足够清楚,有人可以在这里帮助我们 :)
这可能可以用更好的方式完成,但我最终还是这样做了:
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);
在之前的尝试中,我没有给函数做任何比较,所以我做了两个新函数。第一个是我将我的类别映射到我网站上正确的类别路径的地方。另一个功能是告诉我应该将什么导入网站,我告诉它仅当 csv/xml
中的 merchant_product_category_path
在 $find
数组中时才导入。
我们需要在我们的网站上导入很多产品,因此当我们从 csv/xml
文件导入时,它不会导入我们网站上尚不存在的类别,这一点非常重要。
我们使用一个名为 WP All Import 的插件,您可以在其中添加一个过滤器,判断是否应该创建产品,但无论我们做什么,我们都无法让它发挥作用。
代码:这是我们需要修改的代码,当类别路径存在时为return true,当类别路径不存在时为return false .现在它一直都是 return 错误,即使我知道类别路径存在。
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;
}
简而言之: 如果特定产品的完整类别路径已经存在,我们只想将整个 product/products 导入我们的网站我们的网站。否则我们希望它不导入产品并跳过它。
在 github 他们有一些示例,其中展示了如何使用应该导入哪些产品的过滤器,这是我们从 here
获得过滤器的地方希望它足够清楚,有人可以在这里帮助我们 :)
这可能可以用更好的方式完成,但我最终还是这样做了:
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);
在之前的尝试中,我没有给函数做任何比较,所以我做了两个新函数。第一个是我将我的类别映射到我网站上正确的类别路径的地方。另一个功能是告诉我应该将什么导入网站,我告诉它仅当 csv/xml
中的 merchant_product_category_path
在 $find
数组中时才导入。