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> |
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 »
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.
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()); |
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... | |
} |
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)