Export Woocommerce Products CSV

This tutorial explains how to Export Woocommerce Products CSV. Here I am showing how to export products in CSV.

export woocommerce products CSV

Cick here for Woocommerce category export..Also refer related , crosssell and upsell products export.

Export Woocommerce Products CSV

Please copy and paste the following code.

Initialise Database Connection

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

getAllProducts();

Export Products

function getAllProducts() {
    
    $loop = new WP_Query(array('post_type' => array('product', 'product_variation'), 
                        'posts_per_page' => -1,
                        'product_cat' => 'heat-proof-trivet-mats'
        ));
    $allData = array();
    while ($loop->have_posts()) : $loop->the_post();
        
        $id = get_the_ID();
        $product = wc_get_product( $id ); 
         if (get_post_type() == 'product_variation') {

            // ****************** end error checking *****************
        } else {
          
            $productData = prepareProductData( $product );
            $seoData = yoastSeoDetails( $id );
            $tagsData = getProductTags( $id );
            $mainImage = getFeaturedImage( $id );
            $categories = getCategories( $id );
            $galleryImages = getProductGalleryImages( $product);
            $additionalInfo = additionalInfo( $id, $product );
            $shippingInfo = shippingClass($id);
            $related = relatedProducts( $product );
            $crosssell = crosssellProducts($product);
            $upsell = upsellProducts( $product );
          //  $taxClass = taxClass( $id );
            //$taxStatus = taxStatus( $id );

            $allData[$id] = array_merge( $productData, $seoData, $tagsData, $mainImage, $galleryImages, $categories, $additionalInfo,$shippingInfo, $related, $crosssell, $upsell);

        }

    endwhile;
 
    createCSV($allData);
}

Export Product Details and additional Info

function prepareProductData( $product ) {
    
    $productData = array( $product->id ,
                          $product->post->post_title,
                          $product->post->post_excerpt,
                          $product->post->post_status,
                          $product->post->post_content                           
                        );
    return $productData;
    
}
function additionalInfo( $productId, $product ) {
 
    $sku = get_post_meta( $productId, '_sku', true );
    $minprice = get_post_meta( $productId, '_wc_measurement_price_calculator_min_price', true );
    $weight = get_post_meta( $productId, '_weight', true );
    $length = get_post_meta( $productId, '_length', true );
    $width = get_post_meta( $productId, '_width', true );
    $height = get_post_meta( $productId, '_height', true );
   
    $regPrice = $product->get_regular_price();
    $salePrice = $product->get_sale_price();
    $price = $product->get_price();
    $additionalInfo = array( $sku, $price, $regPrice, $salePrice,$minprice,$weight,$length,$width,$height);
    return $additionalInfo;
    
}

Export Product Gallery Images

Please click here to download the images from the URL.

function getProductGalleryImages( $product) {
    
    $addImageUrl = '';
    $attachment_ids = $product->get_gallery_attachment_ids();
    
    try {
        
        foreach( $attachment_ids as $attachment_id ) 
        {
          $addImageUrl .= wp_get_attachment_url( $attachment_id ).',';
        }
        return array(substr( $addImageUrl, 0, -1 ));

    } catch (Exception $ex) {
        echo 'Product Gallery not existing : '.$ex .PHP_EOL;
    }
    
}

Export Featured Image URL

function getFeaturedImage( $productId ) {
    
    $featuredImage = wp_get_attachment_image_src( get_post_thumbnail_id( $productId), 'full'  );
    return array($featuredImage[0]);
    
}

Export Product Tags

function getProductTags ( $productId ) {
    
    $tagsdata = '';
    $tags = get_the_terms( $productId, 'product_tag'  );

    foreach( $tags as $tag ) {
        $tagsdata .=  $tag->name.',';
    }
    return array(substr( $tagsdata, 0, -1 ));
   
}

Export Product SEO Details

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

Create CSV

function createCSV($data) {
    
    $fileName = 'exportheatproof.csv';
    header('Content-Type: application/excel');
    header('Content-Disposition: attachment; filename="' . $fileName . '"');
    
    $headers = ['Id', 'Name', 'Short Desc', 'Status', 'Long Desc', 'SEO Title','SEO Meta Desc','SEO Meta keywords','Tags','Main Image','Galleries','Categories','CategoryId','Parent Category','Sku','Reg price','Sale Price','Price','Minprice','Weight','Length','Width','Height','SI','Related','Cross','Upsell'];    
    $file = fopen('php://output', 'w');
    
    fputcsv($file, $headers);
    
    foreach ($data as $row) {
        
        fputcsv($file, $row);
        
    }  
    fclose($file);
    
}

Share this Tutorial

Leave a Reply

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

*