在Magento中删除产品的问题

时间:2013-01-11 23:25:36

标签: php magento magento-1.6

我正在尝试从目录中删除产品

每当我尝试使用管理界面(群发或单一操作)删除产品时,它会抛出 500内部服务器错误。 每当我尝试使用脚本(以编程方式)删除产品时,只需挂起删除方法调用

没有显示错误输出,即使打开了日志记录,也不会抛出异常并且日志也是静默的。

以下是我在脚本中使用的示例代码:

//...
$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 {
        $product->delete(); // this is the line where it hangs
        print $product->getSku() . " deleted" . PHP_EOL;
    } catch (Exception $e) {
        print $e->getMessage();
    }
}

有没有人有这样的经历,可能是什么原因?

Magento Enterprise 1.11与我所知道的Magento Community 1.6相对应。

3 个答案:

答案 0 :(得分:5)

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

编辑:快速谷歌搜索导致另一种解决方案。

http://www.fortwaynewebdevelopment.com/magento-delete-products-programmatically/

这个看起来应该位于magento root中的文件中。它会删除所有产品,因此请注意,但不同的是它。它不是使用集合,而是通过首先解析产品ID,然后为每个产品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();
?> 

答案 1 :(得分:0)

我们找到了解决此问题的方法,可以帮助任何有此问题的人完成任务。

导出您要删除的所有产品,然后使用“删除实体”操作导入它们。

从管理员界面或自定义脚本中删除产品的问题仍然存在,我将继续对此进行调查并相应地更新答案。

答案 2 :(得分:0)

要按产品SKU删除产品,请使用以下代码:

<?php
//...
$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 {

        $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $product->getSku());

        $product->delete();
        echo $product->getSku() . " deleted" . PHP_EOL;

    } catch (Exception $e) {
        echo $e->getMessage();
    }
}