Search code examples
phpwordpresswpallimport

WP All Import / Wordpress - Only import product if full category path exist


We need to import a lot of products on our website, so it is very important when we import from a csv/xml file it does not import categories which is not already on our website.

We use a plugin called WP All Import, where you can add a filter if a product should be created or not but whatever we do, we just can't make it work.

Code: This is the code we need to modify to return true when category path exist and to return false when category path doesn't. Right now it just returns false all the time even though I know the category path exist.

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; 
}

Just to make it clear and short: We want to only import the whole product/products to our website if the full category path from the specific product already exist on our site. Else we want it to not import the product and skip it.

On github they have some examples, where they show how to use the filter of which products should be imported which is where we get the filter from here

Hope it was clear enough and someone can help us out here :)


Solution

  • This can probaly be done in a better way, but I finally managed to do it this way:

    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);
    

    In the earlier try, I did not give the function anything to compare with, so I made two new functions. The first one is where I map my categories to the correct category path on my website. The other function is where I tell what should be imported to the website, where I tell it to only import if merchant_product_category_path from the csv/xml is in the $find array.