Magento 製品は、php の「admin」から削除する必要があります。安全な場所から削除していることを宣言する必要があります。外部スクリプトでは、通常 Mage::app('admin'); を使用します。ただし、以下のように「isSecureArea」を宣言するだけです。
また、Mage_Core_Exception をキャッチして、問題の原因を確認してください。
try {
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('sku')->addStoreFilter($store_id);
$collection->load();
// collection is not empty I checked
foreach ($collection as $product) {
try {
// Register a secure area to simulate 'admin'
Mage::register('isSecureArea', true);
$product->delete(); // this is the line where it hangs
Mage::unregister('isSecureArea');
print $product->getSku() . " deleted" . PHP_EOL;
} catch (Exception $e) {
print $e;
}
}
} catch (Mage_Core_Exception $e) {
echo( $e->getMessage() );
}
編集: Google で簡単に検索すると、別の解決策が見つかります。
http://www.fortwaynewebdevelopment.com/magento-delete-products-programmatically/
これは、magento ルートにあるファイルにあるはずです。すべての商品が削除されるので注意が必要ですが、やり方が異なります。コレクションを使用する代わりに、最初に製品 ID を解析してから各製品オブジェクトを読み込みます。最初にこれを提案しようと思いましたが、コレクション モデルは常に「magento 方式」であり、最初の試みに適しています。
コレクションの一部であるオブジェクトが、読み込まれたオブジェクト モデルとは少し異なる場合があることに気付きました。Magentoはそのように少し厄介です。うまくいけば、これが役立つかもしれません。
<?php
function deleteAllProducts()
{
require_once 'app/Mage.php';
Mage :: app("default") -> setCurrentStore( Mage_Core_Model_App :: ADMIN_STORE_ID );
$products = Mage :: getResourceModel('catalog/product_collection')->setStoreId(1)->getAllIds();
if(is_array($products))
{
foreach ($products as $key => $pId)
{
try
{
$product = Mage::getModel('catalog/product')->load($pId)->delete();
echo "successfully deleted product with ID: ". $pId ."<br />";
}
catch (Exception $e)
{
echo "Could not delete product with ID: ". $pId ."<br />";
}
}
}
}
deleteAllProducts();
?>