wc_display_item_downloads()WC 3.0.0

Display item download links.

Hooks from the function

Return

String|null.

Usage

wc_display_item_downloads( $item, $args );
$item(WC_Order_Item) (required)
Order Item.
$args(array)
Arguments.
Default: array()

Changelog

Since 3.0.0 Introduced.

wc_display_item_downloads() code WC 8.7.0

function wc_display_item_downloads( $item, $args = array() ) {
	$strings = array();
	$html    = '';
	$args    = wp_parse_args(
		$args,
		array(
			'before'    => '<ul class ="wc-item-downloads"><li>',
			'after'     => '</li></ul>',
			'separator' => '</li><li>',
			'echo'      => true,
			'show_url'  => false,
		)
	);

	$downloads = is_object( $item ) && $item->is_type( 'line_item' ) ? $item->get_item_downloads() : array();

	if ( ! empty( $downloads ) ) {
		$i = 0;
		foreach ( $downloads as $file ) {
			$i ++;

			if ( $args['show_url'] ) {
				$strings[] = '<strong class="wc-item-download-label">' . esc_html( $file['name'] ) . ':</strong> ' . esc_html( $file['download_url'] );
			} else {
				/* translators: %d: downloads count */
				$prefix    = count( $downloads ) > 1 ? sprintf( __( 'Download %d', 'woocommerce' ), $i ) : __( 'Download', 'woocommerce' );
				$strings[] = '<strong class="wc-item-download-label">' . $prefix . ':</strong> <a href="' . esc_url( $file['download_url'] ) . '" target="_blank">' . esc_html( $file['name'] ) . '</a>';
			}
		}
	}

	if ( $strings ) {
		$html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
	}

	$html = apply_filters( 'woocommerce_display_item_downloads', $html, $item, $args );

	if ( $args['echo'] ) {
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo $html;
	} else {
		return $html;
	}
}