Skip to content

Instantly share code, notes, and snippets.

@juanlistab
Created December 12, 2023 02:40
Show Gist options
  • Save juanlistab/43be7418839e25bd9d4d94e4e55b6f49 to your computer and use it in GitHub Desktop.
Save juanlistab/43be7418839e25bd9d4d94e4e55b6f49 to your computer and use it in GitHub Desktop.
Create variations as child XML elements in WP All Export

Create variations as child XML elements in WP All Export

Instructions:

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment