我目前正在开发一个模块,使用后端的产品编辑。它的目的是检索产品所属的类别,并使用所选类别的列表填充属性(品牌属性)。
管理员必须至少选择一个类别.
我的模块按照预期工作,除非我不知道如何停止保存过程,如果管理员在编辑产品时没有选择任何类别。
这是工作流
->如果有选定的类别
-->其他
一般的问题可能是:如何将“停止保存”参数传递给观察者?
下面是我的config.xml文件的示例和处理我前面解释的工作流的方法。
非常感谢您的帮助,祝您玩得开心!
config.xml
<catalog_product_prepare_save>
<observers>
<brands_product_save_observer>
<type>singleton</type>
<class>brands/observer</class>
<method>saveProductBrand</method>
</brands_product_save_observer>
</observers>
</catalog_product_prepare_save>Observer.php
public function saveProductBrand($observer) {
$product = $observer->getProduct();
$categoryIds = $product->getCategoryIds();
if (isset($categoryIds)) {
foreach ($categoryIds as $categoryId) {
$isBrandCategory = Mage::getModel('brands/navigation')->isBrandCategory($categoryId);
if ($isBrandCategory)
$brandCategories[] = $categoryId;
}
if (isset($brandCategories)) {
$brandId = Mage::getModel('brands/navigation')->getBrand($brandCategories[0]);
if ($brandId) {
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 140);
foreach ($attribute->getSource()->getAllOptions(true, true) as $option) {
$attributeArray[$option['label']] = $option['value'];
}
$categoryName = Mage::getModel('catalog/category')->load($brandId)->getName();
$product->setData('brand', $attributeArray[$categoryName]);
}
} else {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('Please add this product to a brand in the "Categories" tab.'));
HERE SOME CODE TO TELL MAGENTO TO STOP SAVING THE PRODUCT
return;
}
}
}发布于 2011-03-24 22:03:13
关于Magento的哪一部分支持这一点,总是一派胡言,但抛出一个例外通常是告诉Magento出了问题的规定。堆栈上的层被设置为捕捉这些异常,并使用它们返回表单并显示错误消息。试试看
Mage::throwException(Mage::helper('adminhtml')->__('You totally failed at that.'));如果您看一下Adminhtml模块的Catalog/ProductController.php (1.5,但在以前的版本中采用类似的格式)
function saveAction()
{
...
$product = $this->_initProductSave();
try {
$product->save();
$productId = $product->getId();
...
}_initProductSave方法是触发catalog_product_prepare_save事件的地方。因为这不在saveAction的try/catch块之外,所以异常不会被捕获(如下面的注释所述)。
您需要将验证代码移动到产品模型的预保存事件(catalog_product_save_before)中。这样做可以让您抛出一个异常,并让管理员显示错误消息,并表示用于编辑的表单。
发布于 2011-03-25 01:46:23
几个想法。您实际上想要停止保存,还是“撤消”更改就足够了?如果您的观察者检测到所需的信息丢失,那么只需迭代已更改的数据并将其设置为原始数据并允许保存继续进行。您应该能够做$product->getOrigData()来比较哪些已经改变了?
或者,为什么不强制使用类别属性呢?您应该能够在您的配置xml中做到这一点(不太确定我的头是如何完成的)
最后,可以绑定到controller_action_predispatch_catalog_product_save事件,如果检测到错误状态,设置一个no-dispatch标志,将错误消息添加到会话和redirectReferrer。请查看我先前的答案here以获得详细信息。
=========EDIT=========
我找到了一种新方法。在您的观察者中,将对象上的_dataSaveAllowed值更改为false。Mage_Core_Model_Abstract::save()在继续保存之前检查该值。
HTH,
JD
发布于 2016-06-24 16:34:47
如果需要防止为核心模型(即Catalog/Product)执行保存方法,可以使用反射将"$_dataSaveAllowed“设置为false:
public function catalogProductSaveBefore($observer)
{
try {
$product = $observer->getProduct();
$reflectionClass = new ReflectionClass('Mage_Catalog_Model_Product');
$reflectionProperty = $reflectionClass->getProperty('_dataSaveAllowed');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($product, false);
} catch (Exception $e) {
Mage::log($e->getMessage());
}
return $this;
}https://stackoverflow.com/questions/5425416
复制相似问题