我在产品的图像库中添加了图像:

// $filePath: image full path
$mediaArray = array(
    "thumbnail"   => $filePath,
    "small_image" => $filePath,
    "image"       => $filePath,
);

$product->addImageToMediaGallery($filePath, $mediaArray, true, false);

$product->save();

在此代码之后,图像将添加到画廊中,但未选择。如何选择此图像作为“基本图像”(和/或小图像,缩略图)?

有帮助吗?

解决方案

$mediaArray 参数必须是一个数组,具有您要设置的任何图像类型的值。因此,而不是:

$mediaArray = array(
    "thumbnail"   => $filePath,
    "small_image" => $filePath,
    "image"       => $filePath,
);

用这个:

$mediaArray = array(
    "thumbnail",
    "small_image",
    "image",
);

或图像属性的任何其他组合,如果您不想将图像设置为属性的主图像,则您甚至具有一个空数组。

许可以下: CC-BY-SA归因
scroll top