Php magento删除其他图像

Php magento删除其他图像,php,magento,Php,Magento,这里是我的C版本使用soap的方法。。。你可能想知道我是怎么做到的。。阅读整篇文章 <?php require 'app/Mage.php'; Mage::app(); $products = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('id')

这里是我的C版本使用soap的方法。。。你可能想知道我是怎么做到的。。阅读整篇文章

<?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();
}
在这一点上,我知道删除图像所必须做的一切,只是现在必须编写一个独立的函数

     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;
         }

我想知道它们是否是一种通过编程禁用附加图像的方法?是的,请参见底部的部分,上面写着“foreach($result as$galleryResult)”?这意味着它将遍历附加到文档的每个图片并将其删除。我希望您只需复制并粘贴它,所以我对它进行了测试,但是如果您查看我的整个解释,它概述了如何通过反向工程发现magento内置soap调用的工作方式的基本原理。另外,将用户名设置为“user822179”以外的其他名称。
     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;
         }
// 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']);
                        }
                }
        }