简体   繁体   中英

Magento - How to set media gallery images programmatically?

I try to set a new magento product image eg che_3.png with:

$visibility = array (
   'thumbnail',
   'small_image',
   'image'
);
$product->addImageToMediaGallery( $file, $visibility, true, false);

The product image is moved from the

temporary directory: /media/tmp/catalog/product/upload/sessionid/original/che_3.png

to the

destination directory: /media/catalog/product/c/h/che_3.png

Now, if i want to call the image filepath - that is saved by magento - is wrong. The media_gallery object looks like this:

[media_gallery] => Array
        (
            [images] => Array
                (
                    [0] => Array
                        (
                            [value_id] => 89
                            [file] => /c/h/che_3_2_1.png //che_3.svg is already in here
                            [label] => 
                            [position] => 1
                            [disabled] => 0
                            [label_default] => 
                            [position_default] => 1
                            [disabled_default] => 0
                        )

                    [1] => Array // why this second item?
                        (
                            [value_id] => 88
                            [file] => /c/h/che_3_2.png
                            [label] => 
                            [position] => 1
                            [disabled] => 0
                            [label_default] => 
                            [position_default] => 1
                            [disabled_default] => 0
                        )

                )

            [values] => Array
                (
                )

        )

As you can see the filename is extended by magento and also two items are added to the media_gallery.

My question is how can I reset the product respectively the internal counter so magento is using the original file as it is?

Thanks in advance.

解决的问题 :我用$product->save()保存了两次$product->save() ,似乎$product->addImageToMediaGallery()也执行了两次。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM