Skip to content

Instantly share code, notes, and snippets.

@juanlistab
Last active December 12, 2023 03:28
Show Gist options
  • Save juanlistab/f0e08e9dc0a71b3d76d20b7c35364673 to your computer and use it in GitHub Desktop.
Save juanlistab/f0e08e9dc0a71b3d76d20b7c35364673 to your computer and use it in GitHub Desktop.
Create variations as child XML elements in WP All Export - With Custom Loop for Attributes

Create variations as child XML elements in WP All Export - With Custom Loop for Attributes

Instructions:

// Loop for variations

function my_output_variants( $parent_id ) {
	$xml = '';
	$parent = wc_get_product( $parent_id );	
	if ( ! $parent ) return '**LT**variables**GT****LT**/variables**GT**';
	
	if ( $parent->is_type( 'simple' ) ) {
		$xml .= '**LT**variables**GT**';
        $xml .= "**LT**sku**GT**" .  $parent->get_sku() . "**LT**/sku**GT**";
		$xml .= "**LT**regular_price**GT**" .  $parent->get_regular_price() . "**LT**/regular_price**GT**";
		$xml .= "**LT**sale_price**GT**" .  $parent->get_sale_price() . "**LT**/sale_price**GT**";
		$xml .= "**LT**size**GT**" . $parent->get_attribute( 'size' ) . "**LT**/size**GT**";
        $xml .= "**LT**material**GT**" . $parent->get_attribute( 'material' ) . "**LT**/material**GT**";
		$xml .= "**LT**brand**GT**" . $parent->get_attribute( 'brand' ) . "**LT**/brand**GT**";
		$xml .= "**LT**stock**GT**" .  $parent->get_stock_quantity() . "**LT**/stock**GT**";
		$xml .= '**LT**/variables**GT**';
		return $xml;
	}
	
	$variants = $parent->get_children();
	if ( empty( $variants ) ) return '**LT**variables**GT****LT**/variables**GT**';	
	$xml .= '**LT**variables**GT**';
	
	foreach ( $variants as $variant_id ) {
		$variation = wc_get_product( $variant_id );
		if ( ! $variation ) continue;
		
		$xml .= '**LT**variable**GT**';
        $xml .= "**LT**sku**GT**" .  $variation->get_sku() . "**LT**/sku**GT**";
        $xml .= "**LT**stock**GT**" .  $variation->get_stock_quantity() . "**LT**/stock**GT**";
		$xml .= "**LT**regular_price**GT**" .  $variation->get_regular_price() . "**LT**/regular_price**GT**";
		$xml .= "**LT**sale_price**GT**" .  $variation->get_sale_price() . "**LT**/sale_price**GT**";
		$xml .= "**LT**size**GT**" . $variation->get_attribute( 'size' ) . "**LT**/size**GT**";
        $xml .= "**LT**material**GT**" . $parent->get_attribute( 'material' ) . "**LT**/material**GT**";
		$xml .= "**LT**brand**GT**" . $parent->get_attribute( 'brand' ) . "**LT**/brand**GT**";
        $image_id = $variation->get_image_id();
        $image_url = wp_get_attachment_url( $image_id );
        if ($image_url) {
            $xml .= "**LT**image**GT**" . $image_url . "**LT**/image**GT**";
        }

		$xml .= "**LT**/variable**GT**";




	}
	$xml .= '**LT**/variables**GT**';
	
	return $xml;
}

// Loop for attributes 

function my_output_attributes( $parent_id ) {
    $parent = wc_get_product( $parent_id );
    if ( ! $parent ) return '**LT**ProductSize**GT****LT**/ProductSize**GT**';

    $size_attr = $parent->get_attribute( 'size' );
    $size_attr = str_replace(',', '|', $size_attr);

    $xml = '**LT**ProductSize**GT**';
    $xml .= $size_attr;
    $xml .= '**LT**/ProductSize**GT**';
    
    return $xml;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment