Magento Add Multiselect Attribute to Product with Category Source

Magento Programatically Add Multiselect Attribute for Product with Category Source

Create module configuration file : app\etc\modules\Nik_Categorylink.xml

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

 

Module config file: app\code\local\Nik\Categorylink\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Nik_Categorylink>
            <version>0.1.0</version>
        </Nik_Categorylink>
    </modules>
    <global>
        <helpers>
            <categorylink>
                <class>Nik_Categorylink_Helper</class>
            </categorylink>
        </helpers>
        <models>
            <categorylink>
                <class>Nik_Categorylink_Model</class>
                <resourceModel>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</resourceModel>
            </categorylink>
        </models>
        <resources>
            <categorylink_setup>
                <setup>
                    <module>Nik_Categorylink</module>
                    <class>Mage_Catalog_Model_Resource_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </categorylink_setup>
        </resources>
    </global>
</config>

 

Module SQL setup file : app\code\local\Nik\Categorylink\sql\categorylink_setup\mysql4-install-0.1.0.php

<?php
$installer = $this;
$installer->startSetup();

$installer->addAttribute(
    "catalog_product", "categorylinks",
    array(
        "type" => "text",
        "backend" => "eav/entity_attribute_backend_array",
        'group' => 'General',
        "label" => "Shop More Category Link",
        "input" => "multiselect",
        "source" => "categorylink/System_Config_Source_Category",
        'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
        'is_visible' => 1,
        'required' => 0,
        'searchable' => 0,
        'filterable' => 0,
        'unique' => 0,
        'comparable' => 0,
        'visible_on_front' => 0,
        'user_defined' => 1,
    )
);
$installer->endSetup();
?>

 

4. Create Module Model file:
app\code\local\Nik\Categorylink\Model\System\Config\Source\Category.php


<?php

class Nik_Categorylink_Model_System_Config_Source_Category extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    public function getAllOptions($withEmpty = false)
    {
        $collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->addIsActiveFilter();

        $options = array();

        foreach($collection as $category){
            if($category->getName() != 'Root Catalog'){
                $options[] = array(
                    'label' => $category->getName(),
                    'value' => $category->getId()
                );
            }
        }
        return $options;
    }

    public function toOptionArray()
    {
        return $this->getAllOptions();
    }

    public function getOptionText($value)
    {
        $options = $this->getAllOptions(false);
        foreach ($options as $item) {
            if ($item['value'] == $value) {
                return $item['label'];
            }
        }
        return false;
    }
}
?>
Create Module Helper file: app\code\local\Nik\Categorylink\Helper\Data.php

<?php

class Nik_Categorylink_Helper_Data extends Mage_Core_Helper_Abstract
{
//add function as your requirement.
}

Now, Module is ready for Use. Enjoy 🙂

Advertisements

Magento : Generating(create) automatically invoice after the orders are shipped

Create module config file : app\etc\modules\Nik_Autoinvoice.xml

<config>
    <modules>
        <Nik_Autoinvoice>
            <active>true</active>
            <codePool>local</codePool>
        </Nik_Autoinvoice>
    </modules>
</config>

I'd go the creating an event observer for sales_order_shipment_save_after:

File path: app/code/local/Nik/Autoinvoice/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Nik_Autoinvoice>
            <version>0.1.0</version>
        </Nik_Autoinvoice>
    </modules>
    <global>
        <events>
            <sales_order_shipment_save_after>
                <observers>
                    <nik_autoinvoice>
                        <type>singleton</type>
                        <class>Nik_Autoinvoice_Model_Observer</class>
                        <method>autoInvoice</method>
                    </nik_autoinvoice>
                </observers>
            </sales_order_shipment_save_after>
        </events>
    </global>
</config>

Now, we create one observer file : app\code\local\Nik\Autoinvoice\Model\Observer.php


<?php
class Nik_Autoinvoice_Model_Observer
{
 public function autoInvoice($observer) {
 $shipment = $observer->getEvent()->getShipment();
 $order = $shipment->getOrder();
 if($order->canInvoice())
 {
 $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
 $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
 $invoice->register();
 $transactionSave = Mage::getModel('core/resource_transaction')
 ->addObject($invoice)
 ->addObject($invoice->getOrder());
 $transactionSave->save();
 }
 }
}