Tuesday 26 September 2017

Add Custom Field to Customer Address

$installer = Mage::getSingleton('eav/entity_setup', 'eav_setup');

$installer->startSetup();

$installer->addAttribute('customer_address', 'addresstyped', array(
    'type' => 'varchar',
    'input' => 'text',
    'label' => 'Address Type',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'visible_on_front' => 1
));
Mage::getSingleton('eav/config')
    ->getAttribute('customer_address', 'addresstyped')
    ->setData('used_in_forms', array('customer_register_address','customer_address_edit','adminhtml_customer_address'))
    ->save();
$installer->endSetup();


Module created :-
1. Path :- /app/etc/modules/Dapl_Excellenceaddress.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Dapl_Excellenceaddress>
            <active>true</active>
            <codePool>local</codePool>
        </Dapl_Excellenceaddress>
    </modules>
</config>

2. Path:- /app/code/local/Dapl/Excellenceaddress/etc/config.xml

 <global>
                <fieldsets>
            <sales_convert_quote_address>
                <addresstyped>
                    <to_order_address>*</to_order_address>
                    <to_customer_address>*</to_customer_address>
                </addresstyped>
            </sales_convert_quote_address>
            <customer_address>
                <addresstyped>
                    <to_quote_address>*</to_quote_address>
                </addresstyped>
            </customer_address>
        </fieldsets>
 </global>

Next step:-
ALTER TABLE  `sales_flat_quote_address` ADD  `addresstyped` VARCHAR( 255 ) NOT NULL AFTER  `fax` ;

Text
Add {{depend addresstyped}}ID# {{var addresstyped}}{{/depend}} where ever you want it. {{depend}} basically checks, if govt_id is not empty.
Text One line
Add {{depend addresstyped}}ID# {{var addresstyped}}{{/depend}} where ever you want it. This format shows up in the checkout page shipping,billing address dropdowns.
HTML
Add {{depend addresstyped}}<br/>ID# {{var addresstyped}}{{/depend}}. This format is used in many places like Order View, Address Display etc.
PDF
Add {{depend addresstyped}}<br/>ID# {{var addresstyped}}{{/depend}}|. This format is used in PDF invoices, shipments etc.
Javascript Template
Add <br/>ID#{addresstyped}. This is used in admin add/edit address area.
After saving these in the configuration, the new address format should be visible. 

Wednesday 13 September 2017

Magento Custom Category Attribute File

1.Your category attribute installer:
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_category', 'custom_flv', array(
    'group'                    => 'General',
    'label'                    => 'Some File',
    'input'                    => 'image',
    'type'                     => 'varchar',
    'backend'                  => 'some_module/category_attribute_backend_file',
    'global'                   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'                  => true,
    'required'                 => false,
    'user_defined'             => true,
    'order'                    => 20
));
$installer->endSetup();


2.\app\code\local\Some\Module\Model\Category\Attribute\Backend\File.php

class Some_Module_Model_Category_Attribute_Backend_File extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
    public function afterSave($object)
    {
        $value = $object->getData($this->getAttribute()->getName());

        if (is_array($value) && !empty($value['delete'])) {
            $object->setData($this->getAttribute()->getName(), '');
            $this->getAttribute()->getEntity()
                ->saveAttribute($object, $this->getAttribute()->getName());
            return;
        }

        $path = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category' . DS;
        try {
            $uploader = new Mage_Core_Model_File_Uploader($this->getAttribute()->getName());

            $uploader->setAllowedExtensions(array('flv','pdf','doc','txt','sql'));
            $uploader->setAllowRenameFiles(true);
            $result = $uploader->save($path);
            //allowed extensions here
            $object->setData($this->getAttribute()->getName(), $result['file']);
            $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());
        } catch (Exception $e) {
            if ($e->getCode() != Mage_Core_Model_File_Uploader::TMP_NAME_EMPTY) {
                Mage::logException($e);
            }
            return;
        }
    }
}

3./app/code/local/Some/Module/etc/config.xml
<global>
    <resources>
        <some_module_setup>
            <setup>
                <module>Some_Module</module>
                <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
            </setup>
        </some_module_setup>
    </resources>
    <!--another nodes -->
 </global>