Skip to content

Instantly share code, notes, and snippets.

@trey8611
Created November 26, 2021 16:27
Show Gist options
  • Save trey8611/8e5a520f0c544c84d4ea22910da68670 to your computer and use it in GitHub Desktop.
Save trey8611/8e5a520f0c544c84d4ea22910da68670 to your computer and use it in GitHub Desktop.
[Export Parent SKU if Variation SKU is blank] #wpallimport

This function should be used on a new instance (click "Add Field") of the export element containing the product ID.

function my_get_sku( $id ) {
	$prod = wc_get_product( $id );
	if ( ! $prod ) return;
	
	if ( ! empty( $prod->get_sku() ) ) {
		return $prod->get_sku();
	}
	
	if ( ! empty( $prod->get_parent_id() ) ) {
		$parent = wc_get_product( $prod->get_parent_id() );
		if ( ! $parent ) return;
		return $parent->get_sku();
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment