Browsing articles in "Magento"
May 10, 2013
kalpesh

Magento add attribute to order

Adding custom attribute to order in Magento is same as we do for customer and category. The difference is we will use different setup class AND we will not need attribute set, group and attribute input type now. We will create a quick module which will do exactly what we want and nothing more than that. So let’s start our new module.

1.) Create a file at app/etc/modules/ and name it whatever you want. I will name it Namespace_Module.xml
Paste this code in that file:

< ?xml version="1.0"?>
<config>
    <modules>
        <namespace_module>
            <active>true</active>
            <codepool>local</codepool>
        </namespace_module>
    </modules>
</config>

2.) Create necessary directories to reach to app/code/local/Namespace/Module/etc/, so that we can create our config.xml there. Paste below code in this config file:

< ?xml version="1.0"?>
<config>
    <modules>
        <namespace_module>
            <version>0.0.1</version>
        </namespace_module>
    </modules>
 
    <global>
        <resources>
            <modulename_setup>
                <setup>
                    <module>Namespace_Module</module>
                    <class>Mage_Sales_Model_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </modulename_setup>
            <modulename_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </modulename_write>
            <modulename_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </modulename_read>
        </resources>
    </global>
</config>

Continue reading »

May 10, 2013
kalpesh

Magento add attribute to category

Adding category attribute in Magento is same as we add for product and customer. Here in this post I will create a custom module which will add your new custom category attribute in Magento. So let’s start with the module:

1.) As usual, create an XML file in app/etc/modules/ directory, I will name it Namespace_Module.xml
Paste below code in it changing the Namespace and Module as you want.

< ?xml version="1.0"?>
<config>
    <modules>
        <namespace_module>
            <active>true</active>
            <codepool>local</codepool>
        </namespace_module>
    </modules>
</config>

2.) Second step would be to create a file config.xml in app/code/local/Namespace/Module/etc/ directory. Create all the directories required get there. Paste the below code in it making sure you have changed Namespace and Module with your naming.

< ?xml version="1.0"?>
<config>
    <modules>
        <namespace_module>
            <version>0.0.1</version>
        </namespace_module>
    </modules>
 
    <global>
        <resources>
            <module>
                <setup>
                    <module>Namespace_Module</module>
                    <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </module>
            <module_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </module_write>
            <module_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </module_read>
        </resources>
    </global>
</config>

I have only included setup class and connections as those are the only things we will need to execute our script which will insert the custom attribute to category in our Magento project.
Continue reading »

May 10, 2013
kalpesh

Magento add attribute to customer

In Magento to add an attribute to customer is not an option in the admin panel like it does have for Product attribute. So you have to end up writing the script that will add your custom attribute in customer’s EAV tables.

Below code will insert your custom customer attribute in Magento system. You can even specify whil creating the attribute whether that attribute should appear in the forms (like register/signup) or not.

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

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$entityTypeId     = $setup->getEntityTypeId('customer');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$setup->addAttribute('customer', 'your_attribute_code_here', array(
    'input'         => 'text', //or select or whatever you like
    'type'          => 'int', //or varchar or anything you want it
    'label'         => 'Attribute description goes here',
    'visible'       => 1,
    'required'      => 0, //mandatory? then 1
    'user_defined' => 1,
));

$setup->addAttributeToGroup(
 $entityTypeId,
 $attributeSetId,
 $attributeGroupId,
 'your_attribute_code_here',
 '100'
);

$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'your_attribute_code_here');
$oAttribute->setData('used_in_forms', array('adminhtml_customer')); 
$oAttribute->save();

$setup->endSetup();

Here we have used our custom attribute’s:
input type as text, but you can have it anything you like to appear in the form. It can be textarea, date, select or anything you want.
– data type as int, but you can have it anything from text, datetime, varchar or decimal. Remember customer is EAV in Magento, so it needs this information to store all the future values of this attribute in customer_entity_int (customer_entity_*) table.

May 10, 2013
kalpesh

Magento get products by attribute set id or name

In Magento get all the products by specific attribute set ID or Name with below code snippet. If you want to get products by attribute set Name, then first get the attribute set ID from name as shown in the first 4 lines of code. If you already have attribute set ID, ignore first 4 lines of code as it is only used if you have attribute set Name and not it’s ID.

The resultant array will give you all the products with all the attributes and values. You can narrow the attributes that you only need by specifying them when you call $products->addAttributeToSelect(*) or when you get all data from product $prod->getData().

//If planning to get all products by attribute set NAME
$attrSetName = 'your_attribute_set_name_here';
$attributeSetId = Mage::getModel('eav/entity_attribute_set')
    ->load($attrSetName, 'attribute_set_name')
    ->getAttributeSetId();

//If planning to get all products by attribute set ID
$attributeSetId = 'your_attribute_set_id_here';

$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('attribute_set_id',$attributeSetId);

$products->addAttributeToSelect('*');

$products->load();
foreach($products as $prod) {
        $productsArray[] = $prod->getData(); //get all data or specify any attribute
}

Mage::log($productsArray());
Apr 28, 2013
kalpesh

Magento get all invoices and shipments of an order

Getting all the invoices of an order:

$order = Mage::getModel('sales/order')->load($orderID);
if ($order->hasInvoices()) {
    $invIncrementIDs = array();
    foreach ($order->getInvoiceCollection() as $inv) {
        $invIncrementIDs[] = $inv->getIncrementId();
    //other invoice details...
    } Mage::log($invIncrementIDs);
}

Getting all the shipments of an order:

$order = Mage::getModel('sales/order')->load($orderID);
foreach($order->getShipmentsCollection() as $shipment)
{
    Mage::log($shipment->getData()); //get each shipment data here...
}
Pages:«1...12131415161718...29»

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions