How to modify WooThemes Product Add-Ons for multi-file upload

So as I’ve been switching over to Word Press and woo-commerce for the site there are a few modifications that I needed to do.  One thing that I needed was to be able to have customers upload their files and metadata and link them to orders for studio services (mastering, etc).  So I purchased the WooThemes Product Add-Ons (http://www.woothemes.com/products/product-add-ons/) extensions which seemed like it had what I needed.

Weeeeeeell.  It does do a great job of integrating and linking with orders, but I’m less than impressed with their file upload capabilities.  There’s a number of things that can be made better here, but the biggest issue is that it has no capability to do multifile uploads.  So after poking around in the code a little bit, I found that I can make two simple mods in order to enable this functionality.

First we need to add a check in the file-upload handler that checks for multiple entries in $_FILES and handles them appropriately.

1: Modified get_cart_item_data(…) in  /classes/fields/class-product-add-on-file-upload.php:

public function get_cart_item_data() {

  $cart_item_data           = array();

  foreach ( $this->addon[‘options’] as $option ) {

    $field_name = $this->get_field_name() . ‘-‘ . sanitize_title( $option[‘label’] );

    if ( ! empty( $_FILES[ $field_name ] ) && ! empty( $_FILES[ $field_name ][‘name’] ) ) {

$newcode = is_array($_FILES[$field_name][‘name’]);

      if ($newcode){
        //Added by JJL 1/3/15 to test multi-file upload tweak, this is in conjunction with
        //adding the ‘name=”$field_name[]” multiple’ HTML5 tags in the templates/addons/file_upload.php
        if (!empty($_FILES[$field_name][‘name’][0])){
          for( $i = 0; $i < count( $_FILES[$field_name][‘name’]); $i++ ) {

             $filedata = array(               ‘name’     => $_FILES[$field_name][‘name’][$i],
              ‘type’     =>$_FILES[$field_name][‘type’][$i],
              ‘tmp_name’ => $_FILES[$field_name][‘tmp_name’][$i],
              ‘error’    => $_FILES[$field_name][‘error’][$i],
              ‘size’     => $_FILES[$field_name][‘size’][$i]
            );
            $upload = $this->handle_upload( $filedata );
            if ( empty( $upload[‘error’] ) && ! empty( $upload[‘file’] ) ) {
              $value  = woocommerce_clean( $upload[‘url’] );
              $cart_item_data[] = array(
                ‘name’         => $this->get_option_label( $option ),
                ‘value’        => $value,
                ‘display’    => basename( $value ),
                ‘price’     => $this->get_option_price( $option )
              );
            } else {
              return new WP_Error( ‘addon-error’, $upload[‘error’] );
            }
          }
        }
      }else{
            $upload = $this-&gt;handle_upload( $_FILES[ $field_name ] );
            if ( empty( $upload[‘error’] ) &amp;&amp; ! empty( $upload[‘file’] ) ) {
                $value  = woocommerce_clean( $upload[‘url’] );
                $cart_item_data[] = array(
                    ‘name’         =&gt; $this-&gt;get_option_label( $option ),
                    ‘value’        =&gt; $value,
                    ‘display’    =&gt; basename( $value ),
                    ‘price’     =&gt; $this-&gt;get_option_price( $option )
                );
          } else {
              return new WP_Error( ‘addon-error’, $upload[‘error’] );
          }
}
} elseif ( isset( $this-&gt;value[ sanitize_title( $option[‘label’] ) ] ) ) {
      $cart_item_data[] = array(
        ‘name’         =&gt; $this-&gt;get_option_label( $option ),
        ‘value’        =&gt; $this-&gt;value[ sanitize_title( $option[‘label’] ) ],
        ‘display’    =&gt; basename( $this-&gt;value[ sanitize_title( $option[‘label’] ) ] ),
        ‘price’     =&gt; $this-&gt;get_option_price( $option )
      );
    }
  }
  return $cart_item_data;
}

Secondly we need to modify the HTML5 input fields for file uploads to contain array “[]” syntax and the “multiple” keyword.

2: Modified  /templates/addons/file-upload.php:

<?php foreach ( $addon[‘options’] as $key => $option ) :

$price = ($option[‘price’]>0) ? ‘ (‘ . woocommerce_price( get_product_addon_price_for_display( $option[‘price’] ) ) . ‘)’ : ”;

if ( empty( $option[‘label’] ) ) : ?>

<p class=”form-row form-row-wide addon-wrap-<?php echo sanitize_title( $addon[‘field-name’] ); ?>”>
<input type=”file” class=”input-text addon” data-price=”<?php echo get_product_addon_price_for_display( $option[‘price’] ); ?>” name=”addon-<?php echo sanitize_title( $addon[‘field-name’] ); ?>-<?php echo sanitize_title( $option[‘label’] ); ?>[] multiple /> <small><?php echo sprintf( __( ‘(max file size %s)’, ‘woocommerce-product-addons’ ), $max_size ) ?></small>
</p>

<?php else : ?>

<p class=”form-row form-row-wide addon-wrap-<?php echo sanitize_title( $addon[‘field-name’] ); ?>”>
<label><?php echo wptexturize( $option[‘label’] ) . ‘ ‘ . $price; ?> <input type=”file” class=”input-text addon” data-price=”<?php echo get_product_addon_price_for_display( $option[‘price’] ); ?>” name=”addon-<?php echo sanitize_title( $addon[‘field-name’] ); ?>-<?php echo sanitize_title( $option[‘label’] ); ?>[] multiple  /> <small><?php echo sprintf( __( ‘(max file size %s)’, ‘woocommerce-product-addons’ ), $max_size ) ?></small></label>
</p>

<?php endif; ?>

<?php endforeach; ?>