以编程方式为 WooCommerce 中的产品添加更多可下载文件
Adding programatically more downloadable files for products in WooCommerce
我正在尝试在 woocommerce 产品中再上传一个可下载文件。我的产品中已有一个可下载文件,想再添加一个。
为此,我使用以下代码:
if($_FILES){
$attachment_id = media_handle_upload( 'abe_update_epub', $post_id );
if ( is_wp_error( $attachment_id ) ) {
$errors = $attachment_id->get_error_messages();
foreach( $errors as $error ){
echo $error;
}
echo 'There was an error uploading the image';
} else {
// to get exiting file/Old file
$abe_file = get_post_meta($abe_post_id, '_downloadable_files', true);
foreach($abe_file as $abe){
$name = $abe['name'];
$url = $abe['file'];
}
// This is my new file which i want to upload also
$file_name = 'Epub Files';
$file_url1 = wp_get_attachment_url($attachment_id);
$files[md5( $file_url )] = array(
'name' => $file_name,
'file' => $file_url
);
update_post_meta( $post_id, '_downloadable_files', $files );
echo 'The image was uploaded successfully!';
}
}
此功能以正确的方式上传文件,但它用新文件替换旧文件。
我该如何解决这个问题?
我在这个脚本中做错了什么?
谢谢
update_post_meta 整体重写 meta。
您应该在新数组中加入旧数据 ($abe_file) 和新数据 ($file) 并使用 update_post_meta.
写入它
— Definitive Update 3
There was a lot of mistakes in your code:
In your code there is 2 mistakes in get_post_meta()
function:
- Replaced undefined $abe_post_id
by defined $post_id
.
- Removed third argument "true"
as it's an array (NOT a string).
The outputted array of $abe_file
is a tri-dimensional array with a structure similar to this example:
array(
0 => array(
"67f3fe902b6c55ac07b92ac804d1a9c8" => array(
"name" => "filename1"
"file" => "http://www.domain.tld/wp-content/uploads/woocommerce_uploads/2016/09/file1.pdf"
),
"95ce074e798b2e9d6d0d4cbce02f0497" => array(
"name" => "filename2"
"file" => "http://www.domain.tld/wp-content/uploads/woocommerce_uploads/2016/09/file2.pdf"
)
)
);
您不需要像以前那样在 foreach 循环中迭代此数组,因为您只想将新文件插入其中。您必须确保 $post_id
是您产品的 ID(因为 $abe_post_id
未定义)...
这是有效的更新代码:
if($_FILES){
$attachment_id = media_handle_upload( 'abe_update_epub', $post_id );
if ( is_wp_error( $attachment_id ) ) {
$errors = $attachment_id->get_error_messages();
foreach( $errors as $error ){
echo $error;
}
echo 'There was an error uploading the image';
} else {
// Get exiting array of downloadable files. IMPORTANT:
// 1) => Removed "true" condition as it's an array (ot a string)
// 2) => As "$abe_post_id" is not defined, I have replace it with "$post_id"
$abe_file = get_post_meta($post_id, '_downloadable_files'); // removed "true"
// NEW FILE: Setting the name, getting the url and and Md5 hash number
$file_name = 'Epub Files';
$file_url = wp_get_attachment_url($attachment_id);
$md5_num = md5( $file_url );
// Inserting new file in the exiting array of downloadable files
$abe_file[0][$md5_num] = array(
'name' => $file_name,
'file' => $file_url
);
// Updating database with the new array
update_post_meta( $post_id, '_downloadable_files', $abe_file[0] );
// Displaying a success notice
echo 'The image was uploaded successfully!';
}
}
我正在尝试在 woocommerce 产品中再上传一个可下载文件。我的产品中已有一个可下载文件,想再添加一个。
为此,我使用以下代码:
if($_FILES){
$attachment_id = media_handle_upload( 'abe_update_epub', $post_id );
if ( is_wp_error( $attachment_id ) ) {
$errors = $attachment_id->get_error_messages();
foreach( $errors as $error ){
echo $error;
}
echo 'There was an error uploading the image';
} else {
// to get exiting file/Old file
$abe_file = get_post_meta($abe_post_id, '_downloadable_files', true);
foreach($abe_file as $abe){
$name = $abe['name'];
$url = $abe['file'];
}
// This is my new file which i want to upload also
$file_name = 'Epub Files';
$file_url1 = wp_get_attachment_url($attachment_id);
$files[md5( $file_url )] = array(
'name' => $file_name,
'file' => $file_url
);
update_post_meta( $post_id, '_downloadable_files', $files );
echo 'The image was uploaded successfully!';
}
}
此功能以正确的方式上传文件,但它用新文件替换旧文件。
我该如何解决这个问题?
我在这个脚本中做错了什么?
谢谢
update_post_meta 整体重写 meta。
您应该在新数组中加入旧数据 ($abe_file) 和新数据 ($file) 并使用 update_post_meta.
写入它— Definitive Update 3
There was a lot of mistakes in your code:
In your code there is 2 mistakes in
get_post_meta()
function:
- Replaced undefined$abe_post_id
by defined$post_id
. - Removed third argument"true"
as it's an array (NOT a string).The outputted array of
$abe_file
is a tri-dimensional array with a structure similar to this example:array( 0 => array( "67f3fe902b6c55ac07b92ac804d1a9c8" => array( "name" => "filename1" "file" => "http://www.domain.tld/wp-content/uploads/woocommerce_uploads/2016/09/file1.pdf" ), "95ce074e798b2e9d6d0d4cbce02f0497" => array( "name" => "filename2" "file" => "http://www.domain.tld/wp-content/uploads/woocommerce_uploads/2016/09/file2.pdf" ) ) );
您不需要像以前那样在 foreach 循环中迭代此数组,因为您只想将新文件插入其中。您必须确保 $post_id
是您产品的 ID(因为 $abe_post_id
未定义)...
这是有效的更新代码:
if($_FILES){
$attachment_id = media_handle_upload( 'abe_update_epub', $post_id );
if ( is_wp_error( $attachment_id ) ) {
$errors = $attachment_id->get_error_messages();
foreach( $errors as $error ){
echo $error;
}
echo 'There was an error uploading the image';
} else {
// Get exiting array of downloadable files. IMPORTANT:
// 1) => Removed "true" condition as it's an array (ot a string)
// 2) => As "$abe_post_id" is not defined, I have replace it with "$post_id"
$abe_file = get_post_meta($post_id, '_downloadable_files'); // removed "true"
// NEW FILE: Setting the name, getting the url and and Md5 hash number
$file_name = 'Epub Files';
$file_url = wp_get_attachment_url($attachment_id);
$md5_num = md5( $file_url );
// Inserting new file in the exiting array of downloadable files
$abe_file[0][$md5_num] = array(
'name' => $file_name,
'file' => $file_url
);
// Updating database with the new array
update_post_meta( $post_id, '_downloadable_files', $abe_file[0] );
// Displaying a success notice
echo 'The image was uploaded successfully!';
}
}