0

How does this work? I cannot seem to find anything on it. I'm looking for a way to validate a field before before_save. If condition are good, then continue, otherwise throw error. This would happen in the product admin.

For example...keep in mind this is an example.

$product = $observer->getProduct();

$weight = $product->getWeight();

If(!$weight())
{
Validation Error(Or if we could just make the attribute required on this event or Zend validation ) 
}

I've tried on the before_save, but just not working.

Mage::throwException(Mage::helper('catalog')->__('Weight is blank));

I've added my code. The challenge now is that when I 'Save' it fires. However, if I don't do anything It won't fire a second time. On a third time it fires again as an endless loop. This prevents the logic from working as intended.

$product = $observer->getProduct();

$erpsku = $product->getErpSku();
$weight = $product->getWeight();
$DimensionalShippingHeight = $product->getDimensionalShippingHeight();
$DimensionalShippingWidth = $product->getDimensionalShippingLength();
$DimensionalShippingLength = $product->getDimensionalShippingWidth();

$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute('erp_sku');
$erpskuoptionvalue = $attr->getSource()->getOptionId('Sales');


if(($erpsku == $erpskuoptionvalue) && !$weight && (!$DimensionalShippingHeight || !$DimensionalShippingWidth || !$DimensionalShippingLength))
{

   Mage::throwException('Please fill in Weight & All Shipping Dimensions');

}
elseif(($erpsku == $erpskuoptionvalue) && !$weight)
{

   Mage::throwException('Please fill in Weight');

}
elseif(($erpsku == $erpskuoptionvalue) && (!$DimensionalShippingHeight || !$DimensionalShippingWidth || !$DimensionalShippingLength))
{

   Mage::throwException('Please fill All Shipping Dimensions');

}
  • Which magento version are you using? Have you debug Is this your observer is trigger or not? – Prashant Valanda May 6 '16 at 5:44
  • Using 1.9.1 Currently. – Anthony Cicchelli May 6 '16 at 14:07
  • I've added to my code and got the Exception working...kind see above. – Anthony Cicchelli May 6 '16 at 16:06
0

My suggestion would be to used a backend model instead of having to deal with that observer.

Basically you first need an install/upgrade script to update the field you want to validate:

<?php

$installer = $this;
$installer->startSetup();

$attribute = "attributecode";

$entityTypeId = $installer->getEntityTypeId('catalog_product');
$installer->updateAttribute($entityTypeId, $attribute, 'backend_model', 'modulename/eav_entity_attribute_backend_attributecode');

$installer->endSetup();

Where attributecode is the code of the attribute you want to validate and modulename is the name declared under the <models> tag of your module.

Then you need to create the following file: Model/Eav/Entity/Attribute/Backend/Attributecode.php :

<?php

class Vendor_Module_Model_Eav_Entity_Attribute_Backend_Attributecode extends Mage_Eav_Model_Entity_Attribute_Backend_Default
{
    public function beforeSave($object)
    {
        parent::beforeSave($object);
        $attrCode = $this->getAttribute()->getAttributeCode();
        $value = $object->getData($attrCode);
        // Do your validation
        // Use Mage::throwException("Error"); to throw the errors
    }
}
  • I'll give this a try, however, the Observer is what I need to try to get working right now. – Anthony Cicchelli May 6 '16 at 14:12
  • Everytime I throwException it works the first time, but the second time it lets me save. Then the third time it will throwException work, but then it'll let me save. Any idea? – Anthony Cicchelli May 11 '16 at 17:38
  • Had to write it like this. I had the improper format incase anyone rules into this... Mage::throwException(Mage::helper('adminhtml')->__('Please fill in Weight & All Shipping Dimensions')); Updated above. – Anthony Cicchelli May 13 '16 at 17:49
0

Working Script for throwing an except on Product Page.

$product = $observer->getProduct();

$erpsku = $product->getErpSku();
$weight = $product->getWeight();
$dimensionalshippingheight = $product->getDimensionalShippingHeight();
$dimensionalshippingwidth = $product->getDimensionalShippingLength();
$dimensionalshippinglength = $product->getDimensionalShippingWidth();

$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute('erp_sku');
$erpskuoptionvalue = $attr->getSource()->getOptionId('Sales');

if(($erpsku === $erpskuoptionvalue) && !$weight && (!$dimensionalshippingheight || !$dimensionalshippingwidth || !$dimensionalshippinglength))
{

Mage::throwException(Mage::helper('adminhtml')->__('Please fill in Weight & All Shipping Dimensions'));

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.