Añadir archivo descargable a producto simple WooCommerce

Con este código (Snippet) añadiremos una nueva funcionalidad que permite añadir archivo descargable a producto simple WooCommerce. Imaginaros que vendemos un producto que necesita un manual de instrucciones, pues podremos realizar el envío físico del producto y a su vez que le llegue el manual de instrucciones al cliente al realizar el pedido.

Añadir-archivo-descargable-a-producto-simple-WooCommerce

Código para añadir archivo descargable a un producto de envío en WooCommerce

// Crea campos para adjuntar descargables en la paágina de edición del producto
if ( in_array( 'woocommerce/woocommerce.php', get_option( 'active_plugins' ) ) ){

	add_action( 'woocommerce_product_data_tabs', 'woo_add_custom_admin_product_tab' );
	function woo_add_custom_admin_product_tab( $product_data_tabs ) {
		
		$product_data_tabs[ 'documentation-data-tab' ] = array(
			'label' => __( 'Documentación', 'woocommerce' ),
			'target' => 'documentation_product_data',
		);
		
		return $product_data_tabs;
	}
	
	add_action( 'woocommerce_product_data_panels', 'add_documentation_tab_fields' );
	function add_documentation_tab_fields(){
		global $post;
	
		wp_nonce_field( plugin_basename(__FILE__), 'wp_custom_attachment_nonce' );
		?>
	
		<div id="documentation_product_data" class="panel woocommerce_options_panel">
			<div class="form-field downloadable_files">
				<label><?php _e( 'Archivos descargables', 'woocommerce' ); ?>:</label>
				<table class="widefat">
					<thead>
						<tr>
							<th><?php _e( 'Name', 'woocommerce' ); ?> <span class="tips" data-tip="<?php esc_attr_e( 'This is the name of the download shown to the customer.', 'woocommerce' ); ?>">[?]</span></th>
							<th colspan="2"><?php _e( 'File URL', 'woocommerce' ); ?> <span class="tips" data-tip="<?php esc_attr_e( 'This is the URL or absolute path to the file which customers will get access to. URLs entered here should already be encoded.', 'woocommerce' ); ?>">[?]</span></th>
							<th> </th>
						</tr>
					</thead>
					<tbody>
						<?php
						$downloadable_files = get_post_meta( $post->ID, 'documentation_product_files', true );
						?>
						<tr>
							<td class="file_name"><input type="text" id="title_documentation_file" placeholder="Ej: Información adicional" name="wp_custom_attachment[title]" class="input_text" value="<?php if ( isset ( $downloadable_files['title'] ) ) echo esc_attr( $downloadable_files['title'] ); ?>" /></td>
							<td class="file_url"><input type="text" id="attachment_documentation_file" placeholder="<?php esc_attr_e( "http://", 'woocommerce' ); ?>" name="wp_custom_attachment[documentation]" class="input_text" value="<?php if ( isset ( $downloadable_files['documentation'] ) ) echo esc_attr( $downloadable_files['documentation'] ); ?>" /></td>
						    <td class="file_url_choose" width="1%"><input type="button" class="button upload_file_button" data-file="attachment_documentation_file" value="<?php _e( 'Choose file', 'woocommerce' )?>" /></td>
						</tr>
					</tbody>
				</table>
			</div>
		</div>
		<?php
	}
	
	// Guarda ficheros de documentación adjuntos a cada ficha de producto
	add_action( 'save_post', 'save_custom_meta_data', 99, 2 );
	function save_custom_meta_data( $post_id, $post ) {

		/* --- security verification --- */
		if ( 'product' !== $post->post_type ) {

			return $post_id;
		}

		if(!wp_verify_nonce($_POST['wp_custom_attachment_nonce'], plugin_basename(__FILE__))) {
			return $post_id;
	    	} // end if

	    	if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
	    		return $post_id;
	    	} // end if

	    	if('product' == $_POST['post_type']) {
	    		if(!current_user_can('edit_page', $post_id)) {
	    			return $post_id;
	      		} // end if
		} else {
			if(!current_user_can('edit_page', $post_id)) {
				return $post_id;
	        	} // end if
	    	} // end if
	    	/* - end security verification - */

	    	// Make sure the file array isn't empty
	    	if( isset( $_POST[ 'wp_custom_attachment' ] ) ) {
	
	    		$attachment_files = $_POST[ 'wp_custom_attachment' ];
	
	    		update_post_meta( $post_id, 'documentation_product_files', $attachment_files );
	    	}
	}
	
	// Añade botón de descarga de adjunto debajo de imágenes del producto
	add_action( 'woocommerce_after_single_product_summary', 'show_download_additional_info_button', 1 );
	function show_download_additional_info_button(){
		global $post, $product;
	
		?>
		<div class="attach_documentation_downloadable">
			<?php
			if( $documentation = get_post_meta( $post->ID, 'documentation_product_files', true )) {
				if ( !empty( $documentation[ 'documentation' ] ) ):
			?>
					<a class="button" href="<?php echo esc_attr( $documentation[ 'documentation' ] ); ?>" target="_blank" ><i class="fa fa-file-pdf-o fa-fw"></i><?php echo !empty( $documentation[ 'title' ] )? esc_attr( $documentation[ 'title' ] ) : 'Información adicional'; ?></a>
			<?php endif;
			}
			?>
		</div>
		<?php
	}
}
Ver más tutoriales de: WooCommerce

Tutoriales relaccionados

¿Necesitas un desarrollo a medida?

Login para aceder

LOGIN
REGISTRO