首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >images删除其他图像

images删除其他图像
EN

Stack Overflow用户
提问于 2013-01-16 02:19:02
回答 1查看 1.2K关注 0票数 0
代码语言:javascript
复制
<?php
require 'app/Mage.php';
Mage::app();
$products = Mage::getModel('catalog/product')->getCollection()
                                        ->addAttributeToSelect('id')
                                        ->addAttributeToSelect('image')
                                        ->addAttributeToSelect('small_image')
                                        ->addAttributeToSelect('thumbnail_image');
foreach ($products as $product) {
  if (!$product->hasImage()) continue;
  if (!$product->hasSmallImage()) $product->setSmallImage($product->getImage());
  if (!$product->hasThumbnail()) $product->setThumbnail($product->getImage());
  $product->save();
}

我试图找出如何用这段代码删除额外的图像,但我在网上看不到任何指南:(我使用这段代码来设置小的,缩略图到基本图像,现在我被困在试图删除重复的额外图像。

EN

回答 1

Stack Overflow用户

发布于 2013-01-16 04:23:19

下面是我使用soap实现的C#版本的方法...你可能想知道我是怎么做到这一点的..请阅读整篇文章。

代码语言:javascript
复制
private void RemoveImagesFromProduct()
        {
            try
            {    
                List<catalogProductImageEntity> imageList = this.mageObject.catalogProductAttributeMediaList(this.VendorProductId, null, "sku").ToList();

                // execute the soap call to remove each one
                foreach (catalogProductImageEntity catProdImg in imageList)
                {
                    this.mageObject.catalogProductAttributeMediaRemove(this.VendorProductId, catProdImg.file, "sku");
                }
            }
            catch (Exception exception)
            {
                this.SetProgressErrUpdate("Remove Image Handler Error");
                this.SetProgressErrUpdate(exception.Message);
            }
        }

在确定soap调用指向的位置之后,我发现在Core Magento中使用了这些方法:

(app/code/core/Mage/Catalog/Model/Product/Attribute/Media/Api.php)

代码语言:javascript
复制
     public function items($productId, $store = null, $identifierType = null)
     {
         $product = $this->_initProduct($productId, $store, $identifierType);

         $gallery = $this->_getGalleryAttribute($product);

         $galleryData = $product->getData(self::ATTRIBUTE_CODE);

         if (!isset($galleryData['images']) || !is_array($galleryData['images'])) {
             return array();
         }

         $result = array();

         foreach ($galleryData['images'] as &$image) {
             $result[] = $this->_imageToArray($image, $product);
         }

         return $result;
     }

     public function remove($productId, $file, $identifierType = null)
         {
             $product = $this->_initProduct($productId, null, $identifierType);

             $gallery = $this->_getGalleryAttribute($product);

             if (!$gallery->getBackend()->getImage($product, $file)) {
                 $this->_fault('not_exists');
             }

             $gallery->getBackend()->removeImage($product, $file);

             try {
                 $product->save();
             } catch (Mage_Core_Exception $e) {
                 $this->_fault('not_removed', $e->getMessage());
             }

             return true;
         }

在这一点上,我知道所有必须做的事情来删除图像,只是现在必须编写一个独立的函数。

代码语言:javascript
复制
// Set the product ID here, or load a collection and foreach it and use $product->getId()
        $prodId = 4967;
        // Reload here. Collections in magento are just unpredictable sometimes!
        $loadedProduct = Mage::getModel('catalog/product')->load($prodId);

        $attributes = $loadedProduct->getTypeInstance(true)->getSetAttributes($loadedProduct);

        if (isset($attributes['media_gallery'])) {
                // From this point forward, the gallery is represented by $attributes
                $gallery = $attributes['media_gallery'];
                $galleryData = $loadedProduct->getData('media_gallery');

                if (!isset($galleryData['images']) || !is_array($galleryData['images'])) {
                        die('no images for this product');
                }

                $result = array();
                foreach ($galleryData['images'] as &$image) {
                        $data = array(
                            'file'      => $image['file'],
                            'label'     => $image['label'],
                            'position'  => $image['position'],
                            'exclude'   => $image['disabled'],
                            'url'       => Mage::getSingleton('catalog/product_media_config')->getMediaUrl($image['file']),
                            'types'     => array()
                        );


                        foreach ($loadedProduct->getMediaAttributes() as $attribute) {
                            if ($loadedProduct->getData($attribute->getAttributeCode()) == $image['file']) {
                                $data['types'][] = $attribute->getAttributeCode();
                                }
                        }

                        $result[] = $data;
                }

                // At this point all the media info will be displayed for the product
                // From this point forward you just need to call the same parts of the remove soap api call
                foreach ($result as $galleryResult) {
                        // if the image exists, delete it.
                        if($gallery->getBackend()->getImage($loadedProduct, $galleryResult['file'])) {
                                $gallery->getBackend()->removeImage($loadedProduct, $galleryResult['file']);
                        }
                }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14344218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档