Getting an Attribute’s Default Value if a Product Doesn’t Have a Value Saved

<?php
public function getValue($product, $attributeCode){
	if(($val = $product->getData($attributeCode)) === null){
		return $product->getResource()->getAttribute($attributeCode)->getDefaultValue();
	}

	return $val;
}
  • If the attribute has never been saved on a product, it’ll return the default for the attribute.
  • If the attribute has been saved for a product, it’ll return that value.
  • If the attribute was saved as an empty string, it’ll get deleted from the product (instead of being saved as an empty string. If the value is required to be blank, it could be saved as a single space, and then a trim check could be added when outputting to the block.