Programming, WordPress

Migrating from Shopify to WordPress

Shopify is a well known eCommerce platform, and web searches for data migrations either to or from Shopify are pretty easy to find. What I couldn’t find was an explanation on specifically migrating blog articles from Shopify into WordPress.

I recently had a client who ran a Shopify store that only sold one product, a magazine, while the rest of the store was dedicated to the blog content. The business wanted to expand the content strategy and build the subscriber database, so, while migrating platforms wasn’t my first choice, it made sense in the long run as we have more flexibility and control with WordPress.

Moving the product across to WooCommerce was trivial, we didn’t bother scripting that, we just created a new product in Woo, set up the payment options and moved on. Moving the blog content required more thought, and I wasn’t prepared to move them all manually. While they didn’t have a massive amount of blog content, it was enough to want to script the migration.

Shopify have a decent API and setting up API keys is fairly easy. The API documentation provides instructions for setting up an app within your Shopify store, so I won’t repeat that here. Most of the documentation on Shopify development I found was aimed at Ruby developers, which is fine, but as I was writing the script in PHP and running it on WordPress, I wanted something a little closer to home.

I found a PHP library for accessing the Shopify API which I installed with composer. From there it was pretty much following the API docs to fetch the right data.

Shopify stores blog content in a way that was slightly different to what I’m used to with WordPress. Initially I was trying to call the blog endpoint, which was basically returning nothing sensible. Then I noticed from the documentation that ‘Articles’ are stored within a blog, which makes sense as a blog is a collection of articles, so I needed to pass the Blog ID to the articles endpoint.

I think I was expecting what you would find with WordPress. Everything is a post, so calling all posts with no filters should return all posts.

To be fair, I missed it because I was skimming the documentation for code samples, not actually reading the instructions. My bad.

 $shopify = new PHPShopify\ShopifySDK($config);
 $blog_id = 123456789;
 $blog = $shopify->Blog($blog_id)->Article->get();

As all the articles I was looking for where stored in the same “blog”, calling the above method once returned everything I needed.

After that it was fairly simple to loop over the response and insert the articles into WordPress.

foreach($blog as $b) {
    $post_id = wp_insert_post(array(
        'post_title' => $b['title'],
        'post_content' =>$b['body_html'],
        'post_status' => 'publish',
        'post_date' => $b['published_at'],
        'post_excerpt' => $b['summary_html'],
        'post_author' => $author_id,
        'tags_input' => $b['tags'] ));
}

The only thing left was to import the images to the media library and attach them to the posts, which is as simple as downloading the image using the media_sideload_image function and attaching it to the post.

$image_url = $b['image']['src'];
$media = media_sideload_image($image_url, $post_id);
if (!empty($media) && !is_wp_error($media)) {
    $args = array(
        'post_type' => 'attachment',
        'posts_per_page' => -1,
        'post_status' => 'any',
        'post_parent' => $post_id
    );

    $attachments = get_posts($args);

    if (isset($attachments) && is_array($attachments)) {
        foreach ($attachments as $attachment) {
            $image = wp_get_attachment_image_src($attachment->ID, 'full');
            if (strpos($media, $image[0]) !== false) {
                set_post_thumbnail($post_id, $attachment->ID);
                break;
            }
        }
    }
}

All going well, you should now have your blog posts, tags and featured images migrated from your Shopify blog into WordPress.

You can download the complete plugin from https://github.com/dgroddick/shopify-migration

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.