これが、石鹸を使用して C# バージョンで実行するための私の方法です...どうやってこれにたどり着いたのか疑問に思われるかもしれません..投稿全体を読んでください。
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 で使用されていることがわかりました。
(アプリ/コード/コア/メイジ/カタログ/モデル/製品/属性/メディア/Api.php)
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']);
}
}
}