WooCommerce产品导入程序:将媒体上传目录更改为/ {SKU} /

时间:2019-01-21 15:52:20

标签: woocommerce hook-woocommerce

在WooCommerce中,我们希望像这样组织产品的媒体:

/wp-content/products/{SKU}/{allImagesofTheSpecificProdukt}

现在,在“产品/页面编辑”部分中的标准上传器中,我们已经对此代码进行了编码,它的工作原理就像一个超级按钮:

自己的插件:

/* Change Upload-Dir for Products /wp-content/products/{SKU} */
add_filter( 'wp_handle_upload_prefilter', 'test_product_pre_upload' );
function test_product_pre_upload( $file ) {
    add_filter( 'upload_dir', 'test_product_upload_dir' );
    return $file;
}
function test_product_upload_dir( $param ) {
    $id = $_REQUEST['post_id'];
    $parent = get_post( $id )->post_parent;

    if (get_post_type($id) == "product" || get_post_type($parent) == "product") {
        $sku = get_post_meta( $id, '_sku', true );
        if ($sku == '' || $sku == NULL) {
            $sku = get_post_meta( $parent, '_sku', true );
        }
        if ($sku != '') {
            $param['subdir']  = '/products/' . $param['subdir'];
            $param['path'] = $param['basedir'] . $param['subdir'] . preg_replace('/\s+/', '', $sku);
            $param['url']  = $param['baseurl'] . $param['subdir'] . preg_replace('/\s+/', '', $sku);
        } else {
            $param['subdir']  = '/products/' . $param['subdir'];
            $param['path'] = $param['basedir'] . $param['subdir'];
            $param['url']  = $param['baseurl'] . $param['subdir'];
        }
    }
    return $param;
}

但是现在我们也希望使用WooCommerce Importer(内置版本-无插件)时也能如此工作。这怎么可能?

0 个答案:

没有答案