由于某些原因,你可能需要在视图文件中用自定义代码删除产品。但是你会惊讶的看到 “Cannot complete this operation from non-admin area.”这样的报错。下面有些小技巧让你实现它。

这里是奏效的代码部分:

$_authors_product = new Mage_Catalog_Model_Product();
	$_authors_product->load($_item_val_id);
 
	//echo "DELETED... ".$_POST['submit_item4sale_remove_by_entity_id'];
	$_item_val_id = $_POST['submit_item4sale_remove_by_entity_id']; 
	$_item_val_id = (int)str_replace('entity_id_', '', $_item_val_id);
 
	$_authors_product = new Mage_Catalog_Model_Product();
	$_authors_product->load($_item_val_id);
 
	$_current_customer_id = Mage::getSingleton('customer/session')->getCustomer()->getId();
 
	//Allow deletion only if product is from author
	if($_authors_product->submited_by_author == $_current_customer_id) 
	{
		//var_dump(Mage::registry('isSecureArea'));
 
		Mage::register('isSecureArea', true);
		//$_authors_product->delete();
		echo 'ALLOWED DELETED OH YEAAAAAA....';
		Mage::unregister('isSecureArea');	
	}

上面的代码是我最近项目中的一部分,其中最重要的是:

Mage::register('isSecureArea', true);
echo 'ALLOWED DELETED OH YEAAAAAA....';
Mage::unregister('isSecureArea');

基本的想法是在注册表中写入合适的值Mage::register(‘isSecureArea’, true),删除产品后移除它。

360magento提供专业的基于magento系统的电商网站开发服务,如有需求或相关咨询,请与我们联系