Export Woocommerce related crosssell upsell products

This tutorial explains how to Export Woocommerce related cross-sell upsell products. Here I am exporting the related, cross-sell and upsell products to the CSV.

Please also refer the tutorial explains how to export woocommerce categories and products.

Related Products

function relatedProducts( $product ) {
    
    $relatedData = '';
    $relatedProducts = $product->get_related( 4 );
    if ( sizeof( $relatedProducts ) === 0 ) return array();
    
    foreach($relatedProducts as $relatedProduct) {
        
        $relatedData .= get_post_meta( $relatedProduct, '_sku', true ).',';
        
    }
    return array(substr( $relatedData, 0, -1 ));
}

Cross-sell Products

function crosssellProducts( $product) {
    
    $crosssellData = '';
    $crosssellProducts =  WC()->cart->get_cross_sells($product);
    
    if ( sizeof( $crosssellProducts ) === 0 ) return array();
    
    foreach($crosssellProducts as $crosssellProduct) {
        
        $crosssellData .= get_post_meta( $crosssellProduct, '_sku', true ).',';
        
    }
    return array(substr( $crosssellData, 0, -1 ));
}

Upsell Products

function upsellProducts( $product ) {
    
    $upsellData = '';
    $upsellProducts = $product->get_upsells( 4 );
    if ( sizeof( $upsellProducts ) === 0 ) return array();
    
    foreach($upsellProducts as $upsellProduct) {
        
        $upsellData .= get_post_meta( $upsellProduct, '_sku', true ).',';
        
    }
    return array(substr( $upsellData, 0, -1 ));
}

Share this Tutorial

Leave a Reply

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

*