Skip to content

Instantly share code, notes, and snippets.

@trey8611
Created February 21, 2022 19:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trey8611/0dc801c5c97ccb460b9e7da6fe5c9a3e to your computer and use it in GitHub Desktop.
Save trey8611/0dc801c5c97ccb460b9e7da6fe5c9a3e to your computer and use it in GitHub Desktop.
[Import WooCommerce Rest API Product Feed] #wpallimport #woocommerce #restapi

This function can be saved in your child theme's functions.php file, or in a code snippets plugin ( https://wordpress.org/plugins/code-snippets/ ), then used in WP All Import to download a product feed via the WooCommerce REST API. Things that need to be/can be changed are:

  • In the function call, change "100" to the amount of product you want per-page request.
  • In the code, change the $max_pages value from 3 to however many pages you want to fetch.
  • In the code, change the $key and $sec (secret value) values to the correct ones from your WooCo API key details.

Usage (in the Download a file › From URL field in WP All Import):

[my_fetch_products("https://example.com/wp-json/wc/v3/products?per_page=100","json")]

Code:

function my_fetch_products( $url, $type ) {
    if ( empty( $url ) ) return;
    $key      = 'ck_xxxxx'; // wooco rest api key
    $sec      = 'cs_xxxxx'; // wooco rest api secret
    $uploads  = wp_upload_dir();
    $type     = 'json';
    $filename = $uploads['basedir'] . '/' . basename( strtok( $url, '?' ) ) . '.' . $type;

    if ( file_exists( $filename ) ) {
            @unlink( $filename );
    }
    
    $ch = curl_init();
    
    $filtered      = 'start';
    $page          = 1;
    $final_results = array();
    $max_pages     = 3; // change this
    
    while ( ! empty( $filtered ) && $page <= $max_pages ) {
        $url = add_query_arg( array( 'page' => $page ), $url );
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "GET" );
        curl_setopt( $ch, CURLOPT_USERPWD, $key . ':' . $sec );
        $result = curl_exec($ch);
        if ( curl_errno( $ch ) ) {
            exit( 'Error:' . curl_error( $ch ) );
        }
        
        $filtered = json_decode( $result, 1 );
        $filtered = array_filter( $filtered );
        $final_results = array_merge( $final_results, $filtered );
        
        $page++;
    }
    
    file_put_contents( $filename, json_encode( $final_results ) );
    
    curl_close( $ch );
    
    return str_replace( $uploads['basedir'], $uploads['baseurl'], $filename );
}
@Janus5G
Copy link

Janus5G commented Sep 9, 2022

Nice to import a category You can use [my_fetch_products("https://example.com/wp-json/wc/v3/products?category=ID","json")]

Just a problem that one have to have a license for WP-All import can You make a plugin that just collect the data and write out a CSV or XML file with the products maybe :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment