Saturday, September 10, 2011

How to Change an Existing Products Attribute Set

I am working on a project which has different type of products having different product fields, initially i have created all the product under same default Attribute set, but after some time i realize that it will be tough to manage the products so i created different set of attribute sets, since i have already inserted lot of products and at such stage changing each products attribute set is something difficult process.

After doing some research i got a code to add the functionality to change a simple products attribute set directly on the Catalog > Manage Products page.

To achieve this go to file app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php
around line 253 add below line of code in function _prepareMassaction():

$sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
->setEntityTypeFilter(Mage::getModel('catalog/product')
->getResource()->getTypeId())->load()->toOptionHash(); 
 
array_unshift($statuses, array('label'=>", 'value'=>"));
$this->getMassactionBlock()->addItem('attribute_set', array(
    'label'=> Mage::helper('catalog')->__('Change attribute set'),
    'url' => $this->getUrl('*/*/massAttributeSet', array('_current'=>true)),
    'additional' => array(
        'visibility' => array(
            'name' => 'attribute_set',
            'type' => 'select',
            'class' => 'required-entry',
            'label' => Mage::helper('catalog')->__('Attribute Set'),
            'values' => $sets
        )
    )
));

And in this file: app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php (anywhere in the class) add a new function:



public function massAttributeSetAction(){ 
 
    $productIds = $this->getRequest()->getParam('product');
    $storeId = (int)$this->getRequest()->getParam('store', 0);
    if(!is_array($productIds)) {
        $this->_getSession()->addError($this->__('Please select product(s)'));
    } else {
        try {
            foreach ($productIds as $productId) {
                $product = Mage::getSingleton('catalog/product')
                ->unsetData()
                ->setStoreId($storeId)
                ->load($productId)
                ->setAttributeSetId($this->getRequest()->getParam('attribute_set'))
                ->setIsMassupdate(true)
                ->save();
            }
            Mage::dispatchEvent('catalog_product_massupdate_after', array('products'=>$productIds));
            $this->_getSession()->addSuccess($this->__('Total of %d record(s) were successfully updated', count($productIds)));
        } catch (Exception $e) {
            $this->_getSession()->addError($e->getMessage());
        }
    } 
 
    $this->_redirect('*/*/', array('store'=>(int)$this->getRequest()->getParam('store', 0)));
}

Now go to manage product in admin panel now you will be able to select the product and from action dropdown you can select the attribute set.

Wednesday, February 23, 2011

What is Magento EAV?

EAV stands for Entity, Attribute and Value.

Now what is this EAV means? We know that there are several table used in magento lets say we have a table Customer, this table would have several fields like first name, last name, email ID etc. and the values like Ashok, Sharma, ashok@somthing.com respectively. Now lets relate it with this EAV structure, here customer is an entity which has several attribute firstname, lastname and email ID and at last Ashok, sharma ect are values for the attributes.

Lets get into it deeply, in general scenario if we have an ecommerce application it will have several tables lets take an example of product table it will have all the information of a product and there will be another table for categories etc. So what we see is all the information of a product is in one table.

This is what magento doesn't follow. In magento product information is spread into several table. The core table for product is catalog_product_entity, if you see this table it doesn’t have any information of product which you have entered from admin panel except SKU. Now to get all details of product you have to join all attribute to the product entity table, to get product attribute go to the table eav_attribute. If you closely see this table there are lot of attributes so to distinguish product attribute see the field entity_type_id, since each entity has its unique entity_type_id, using this you can get load the attributes of product.

Now you have both entity and attribute its time to get values of the attributes, values are also separated across several table like all the string type values will be stored in catelog_product_varchar table, price will be stored in catelog_product_entity_decimal tables and so on.

So what we come to know from above is that EAV is much more scalable than normal database structure, this mean if we have to add any new field in product we don’t have to change the database table structure, what we do we create an attribute for that and insert values for it.

Hope this will be helpful.

Wednesday, January 12, 2011

Useful Codes in magento

Redirection in Magento Controllers.

Mage::app()->getResponse()->setRedirect(Mage::getUrl('customer/account/login')); 

How to run an custom query in magento?



$sql = "SELECT * FROM your_table"; 
$getData = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql); 
 
foreach($getData as $row) { 
    echo $row['name'] . "\n"; 
} 
 
// fetch write database connection that is used in Mage_Core module 
$write = Mage::getSingleton('core/resource')->getConnection('core_write'); 
// now $write is an instance of Zend_Db_Adapter_Abstract 
$write->query("insert into yourtablename values ('val1','val2')");
 
//or you can do it like this 
$write = Mage::getSingleton('core/resource')->getConnection('core_write'); 
$sql  = "insert into yourtablename values (?, ?)"; 
$write->query($sql, array('val1','val2'));
 


Get Customer Data



$customerData = Mage::helper('customer')->getCustomer()->getData());

Tuesday, January 4, 2011

Some Code Samples In Magento

Get CMS Page ID
$pageId = Mage::getBlockSingleton('cms/page')->getPage()->getIdentifier();

How to get product attributes?
<?php echo $_product->getResource()->getAttribute('attribute name')->getFrontend()->getValue($_product) ?>

How to show breadcrumbs in magento?
<?php echo $this->getLayout()->getBlock("breadcrumbs")->toHtml(); ?>

How to show Static Blocks?
<?php /* $this->block_code2call, static block code name - the one added under CMS > Static Blocks */ $_block = Mage::getModel('cms/block') ->setStoreId(Mage::app()->getStore()->getId()) ->load($this->getLayout()->createBlock('cms/block')->setBlockId($this->block_code2call)->getBlockId()); ?>

Get current category
Mage::getModel('catalog/layer')->getCurrentCategory()->getName()