Export Woocommerce Categories CSV

This tutorial explains how to export woocommerce categories CSV.

Export Woocommerce Categories CSV

Also refer the tutorial that explains how to export woocommerce products.

Export Woocommerce Categories CSV

The following script export woocommerce categories.

Initialise script

ini_set('memory_limit', '2024M');
require_once 'wp-load.php';
require_once ABSPATH.'/wp-admin/includes/taxonomy.php';


Export main category details

function getCategories() {
    $custom_terms = get_terms('product_cat');
    $allData = array();
    foreach( $custom_terms as $custom_term ) {
        $categoryData = array( $custom_term->term_id, $custom_term->parent, $custom_term->name, $custom_term->slug, $custom_term->description  );
        $seoData = yoastSeoDetails( $custom_term->term_id );
        $featuredImage = getCategoryImage( $custom_term->term_id  );
        $allData[$custom_term->term_id] = array_merge( $categoryData, $seoData, $featuredImage );

Export category image URL

Please refer the tutorial that explains how to download the image from a remote server using the URL.

function getCategoryImage( $catId ) {
    $thumbnail_id = get_woocommerce_term_meta( $catId, 'thumbnail_id', true ); 
    $catImageUrl = wp_get_attachment_url( $thumbnail_id );    
    return array($catImageUrl);

Export Category SEO Details

function yoastSeoDetails( $catId ) {
    $seoTitle = get_post_meta( $catId, '_yoast_wpseo_title', true );
    $metaDesc = get_post_meta( $catId, '_yoast_wpseo_metadesc', true );
    $metaKeywords = get_post_meta( $catId, '_yoast_wpseo_metakeywords', true );
    $seoData = array( $seoTitle, $metaDesc, $metaKeywords );
    return $seoData;

Create categories CSV

function createCSV($data) {
    $fileName = 'exportcategories.csv';
    header('Content-Type: application/excel');
    header('Content-Disposition: attachment; filename="' . $fileName . '"');
    $headers = ['Id', 'Parent Id', 'Name', 'Slug', 'Long Desc', 'SEO Title','SEO Meta Desc','SEO Meta keywords','Image Url'];    
    $file = fopen('php://output', 'w');
    fputcsv($file, $headers);
    foreach ($data as $row) {
        fputcsv($file, $row);

Share this Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *