Different upload directory for woocommerce products

I have been trying to get a different upload directonly for woocommerce product post types to work but it is applying to every upload. Here is my code:

function custom_upload_dir($path)
{   
    // Determines if uploading from inside a post/page/cpt
    // If not, default Upload folder is used
    $use_default_dir = (
            isset($_REQUEST['post_id'] ) 
            && $_REQUEST['post_id'] == 0 && $_REQUEST['post_type'] == 'product'  
            ) 
            ? true : false; 

    if( !empty( $path['error'] ) || $use_default_dir )
        return $path; //error or uploading not from a post/page/cpt 

     // Save uploads in FILETYPE based folders. When using this method, 
     // you may want to change the check for $use_default_dir
     $extension = substr( strrchr( $_POST['name'], '.' ), 1 );

     switch( $extension )
     {
        case 'jpg':
        case 'png':
        case 'gif':
            $customdir = '/woo/images';
            break;

        case 'mp4':
        case 'm4v':
            $customdir = '/woo/videos';
            break;

        case 'txt':
        case 'doc':
        case 'pdf':
            $customdir = '/woo/documents';
            break;

        default:
            $customdir = '/woo/others';
            break;
     }

    //remove default subdir (year/month)
    $path['path']    = str_replace($path['subdir'], '', $path['path']); 
    $path['url']     = str_replace($path['subdir'], '', $path['url']);  

    $path['subdir']  = $customdir;
    $path['path']   .= $customdir; 
    $path['url']    .= $customdir;  

    return $path;
}

This is writing my files to the woo/ directory perfect but is not limited to the product post type only. Any help would be appreciated.

Related posts

1 comment

  1. You are going to want to overide the woo default uploads directory in the woocommerce plugin file class-wc-admin-post-types.php If you want to override it, you can create a filter to this ‘upload_dir’ with higher priorities.

    add_action('pre_get_posts', '_my_pre_get_posts', 10, 1);
    function _my_pre_get_posts( $wp ) {
    global $typenow, $blog_id;
    
      if ( 'product' == $typenow && $blog_id != 1) {
    switch_to_blog(1);
      }
    }
    

    It may or may not work due to the post_type depending on the current post_type being queried, if you want this to work with orders as well you probably will check for ‘shop_order’.

    I tried this once but only worked a few times. It might be overiding to early before the woo methods but I think you can also remove a method temporarily and then let the script you have work separately.

    I found your question looking for something similar so thought you may have more luck with this than I. Also WP has a built in $mime action/filter. I use it like this:

    add_filter('upload_mimes','wpaclip_restrict_mimes_types');
    function wpaclip_restrict_mimes_types($mimes) {
      if (!current_user_can('author')) {
        return;
      }
    
      $mimes = array(
        'acc'       => 'audio/aac',
        'mp4 | m4a' => 'audio/mp4',
        'mp1 | mp2 | mp3 | mpg | mpeg' => 'audio/mpeg',
        'ogg | oga' => 'audio/ogg',
        'wav'       => 'audio/wav',
        'webm'      => 'audio/webm'
        );
    
      return $mimes;
     }
    

    This might help isolate your file types.

Comments are closed.