将多个图像上传到 Woocommerce 产品

问题描述 投票:0回答:2

我正在尝试将各种图像从 URL 上传到给定的

woocommerce
产品。我的代码面临的问题是,虽然我看到图像正在上传到服务器,但当我查看我的帖子时,我只看到上传到图库的最后一张图像。

这是我的代码:

function generateSecondImage($image_url, $post_id)
{
    $upload_dir = wp_upload_dir();
    $image_data = file_get_contents($image_url);
    $filename = basename($image_url);
    if (wp_mkdir_p($upload_dir['path'])) {
        $file = $upload_dir['path'] . '/' . $filename;
    } else {
        $file = $upload_dir['basedir'] . '/' . $filename;
    }

    file_put_contents($file, $image_data);

    $wp_filetype = wp_check_filetype($filename, null);
    $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => sanitize_file_name($filename),
        'post_content' => '',
        'post_status' => 'inherit',
    );
    $attach_id = wp_insert_attachment($attachment, $file, $post_id);
    require_once ABSPATH . 'wp-admin/includes/image.php';
    $attach_data = wp_generate_attachment_metadata($attach_id, $file);
    $res1 = wp_update_attachment_metadata($attach_id, $attach_data);
    $res3 = update_post_meta($post_id, '_product_image_gallery', $attach_id);

}

这是我调用该函数的方式

 for ($j=1; $j <$picCount ; $j++) { 
 generateSecondImage($pic[$j]->url, $post_id);
 }

我想也许,

res3
会覆盖图库并仅显示最后发布的图像,但如果是这样的话,我如何告诉
wordpress
将所有图像包含在for循环中?

wordpress image woocommerce
2个回答
2
投票

终于解决了!

这是我的最终代码。我的上传功能是这样的:

function generateSecondImage($image_url, $post_id)
{
    $upload_dir = wp_upload_dir();
    $image_data = file_get_contents($image_url);
    $filename = basename($image_url);
    if (wp_mkdir_p($upload_dir['path'])) {
        $file = $upload_dir['path'] . '/' . $filename;
    } else {
        $file = $upload_dir['basedir'] . '/' . $filename;
    }

    file_put_contents($file, $image_data);

    $wp_filetype = wp_check_filetype($filename, null);
    $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => sanitize_file_name($filename),
        'post_content' => '',
        'post_status' => 'inherit',
    );
    $attach_id = wp_insert_attachment($attachment, $file, $post_id);
    require_once ABSPATH . 'wp-admin/includes/image.php';
    $attach_data = wp_generate_attachment_metadata($attach_id, $file);
    $res1 = wp_update_attachment_metadata($attach_id, $attach_data);

    echo "Attach ID is".$attach_id;
    return $attach_id;

}

然后我必须创建一个以逗号分隔的 ID 列表并将其添加到

add_post_meta

所以我的循环变成了这样:

for ($j=1; $j <$picCount ; $j++) { 
$attarray[] = generateSecondImage($pic[$j]->url, $post_id);
}

$commaList = implode(', ', $attarray);
$res3 = add_post_meta($post_id, '_product_image_gallery', $commaList);

希望这可以帮助其他寻求解决方案的人。


0
投票

`$upload_dir = wp_upload_dir();
        $attchmentIds = [];
        foreach ($_FILES['productImages']['tmp_name'] as $key => $fileTmpName) {

            if ($fileTmpName) {
                $imageName = $_FILES['productImages']['name'][$key];
                $imageData = file_get_contents($fileTmpName);
                $upload_path = $upload_dir['path'] . '/' . $imageName;
                $imageType
                    = $_FILES['productImages']['type'][$key];
                if (file_put_contents($upload_path, $imageData) !== FALSE) {

                    $attachment         =   array(
                        'post_mime_type' => $imageType,
                        'post_title'     =>  $imageName,
                        'post_status'    => 'inherit',
                        'guid'           => $upload_dir['url'] . '/' . $imageName
                    );

                    $attachment_id = wp_insert_attachment($attachment, $upload_path);

                    $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_path);
                    wp_update_attachment_metadata($attachment_id, $attachment_data);

                    if ($key == $featured_image_key) {
                        $product->set_image_id($attachment_id);
                    } else {
                        // echo $attachment_id . "\n";
                        $attchmentIds[] = $attachment_id;
                    }
                    $product->set_gallery_image_ids($attchmentIds);
                }
            }
        }
        $product->save();
        $productId = $product->get_id();`

© www.soinside.com 2019 - 2024. All rights reserved.