Guest User

Untitled

a guest
Jun 25th, 2012
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. Stopping product saving process in observer
  2. <catalog_product_prepare_save>
  3. <observers>
  4. <brands_product_save_observer>
  5. <type>singleton</type>
  6. <class>brands/observer</class>
  7. <method>saveProductBrand</method>
  8. </brands_product_save_observer>
  9. </observers>
  10. </catalog_product_prepare_save>
  11.  
  12. public function saveProductBrand($observer) {
  13. $product = $observer->getProduct();
  14. $categoryIds = $product->getCategoryIds();
  15. if (isset($categoryIds)) {
  16. foreach ($categoryIds as $categoryId) {
  17. $isBrandCategory = Mage::getModel('brands/navigation')->isBrandCategory($categoryId);
  18. if ($isBrandCategory)
  19. $brandCategories[] = $categoryId;
  20. }
  21. if (isset($brandCategories)) {
  22. $brandId = Mage::getModel('brands/navigation')->getBrand($brandCategories[0]);
  23. if ($brandId) {
  24. $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 140);
  25. foreach ($attribute->getSource()->getAllOptions(true, true) as $option) {
  26. $attributeArray[$option['label']] = $option['value'];
  27. }
  28. $categoryName = Mage::getModel('catalog/category')->load($brandId)->getName();
  29. $product->setData('brand', $attributeArray[$categoryName]);
  30. }
  31. } else {
  32. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('Please add this product to a brand in the "Categories" tab.'));
  33.  
  34. HERE SOME CODE TO TELL MAGENTO TO STOP SAVING THE PRODUCT
  35.  
  36. return;
  37. }
  38. }
  39. }
  40.  
  41. Mage::throwException(Mage::helper('adminhtml')->__('You totally failed at that.'));
  42.  
  43. function saveAction()
  44. {
  45. ...
  46. $product = $this->_initProductSave();
  47.  
  48. try {
  49. $product->save();
  50. $productId = $product->getId();
  51. ...
  52. }
  53.  
  54. function save()
  55. {
  56. if( $error_detection )
  57. {
  58. return $this;
  59. }
  60. else
  61. {
  62. return parent::save();
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment