Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arunbasillal/644ba6ed13296440071a9af3895e0563 to your computer and use it in GitHub Desktop.
Save arunbasillal/644ba6ed13296440071a9af3895e0563 to your computer and use it in GitHub Desktop.
IAP: %wc_posttitle% Custom image attribute to use title of main product instead of WooCommerce variation title.
/**
* Return title of the post where the image is uploaded to.
* For %wc_posttitle%
*
* For WooCommerce variations, this will return the name of the original product
* instead of the variation title.
*
* @param $image_id The ID of the image that is being updated.
* @param $parent_post_id The post to which the image is attached (uploaded) to. 0 if the image is not attached to any post.
* @param $args (array) An array containing additional arguments.
*
* @return String Title of the post where the image is uploaded to.
*/
function iaffpro_get_custom_attribute_tag_wc_posttitle( $image_id, $parent_post_id, $args = array() ) {
if ( (int) $parent_post_id === 0 ) {
return '';
}
// Get the ID of the main product instead of the variation ID.
if ( function_exists( 'wc_get_product' ) ) {
$variation = wc_get_product( $parent_post_id );
if ( $variation && $variation->is_type( 'variation' ) ) {
$parent_post_id = wp_get_post_parent_id( $parent_post_id );
}
}
$post = get_post( $parent_post_id );
return $post->post_title;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment