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.

Answers


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.


Need Your Help

Facebook android SDK 3 get email from user

android facebook

Here is my code for facebook me request.i want to get user email and other basic info.this code is work without any issue in emulator.but in real device email gives null value. id,fristname... come...

Magento: Adding +/- quantity increment buttons in list.phtml

php jquery magento button increment

I've added some quantity increment buttons to my product list view page, I've added the code to list.phtml - The +/- buttons and quantity field show up but I just need to link it all in with the 'a...