Thursday, October 25, 2012

How to add custom product type in Magento


>> Basically You can't create a product type from Admin. You need to modify config.xml and sql in your module.

config.xml

<global>
    <catalog>
        <product>
            <type>
                <classified translate="label" module="vendor">
                    <label>Classified</label>
                    <model>vendor/product_type_classified</model>
                    <composite>0</composite>
                    <index_priority>50</index_priority>
                    <is_qty>1</is_qty>
                </classified>
            </type>
        </product>
    </catalog>
</global>

Here classified is my custom product type and vendor is my custom module.

Now you need to update price type attributes for new type : To run your sql file of your module for next time you need to delete the row created for the module "vendor" in core_resource table. We will discuss later for update Sql.
<?php

$installer = $this;
/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */

$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$fieldList = array(
    'price',
    'special_price',
    'special_from_date',
    'special_to_date',
    'minimal_price',
    'cost',
    'tier_price',
    'weight',
    'tax_class_id'
);

// make these attributes applicable to downloadable products
foreach ($fieldList as $field) {
    $applyTo = split(',', $setup->getAttribute('catalog_product', $field, 'apply_to'));
    if (!in_array('classified', $applyTo)) {
        $applyTo[] = 'classified';
        $setup->updateAttribute('catalog_product', $field, 'apply_to', join(',', $applyTo));
    }
}

$installer->endSetup();

?>

Wednesday, October 17, 2012

How to get attribute set name in Magento


>> $prodAttributeSet = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName();


>> To get the attributeset name in cart page see the following..
app/design/frontend/base/default/template/checkout/cart/item/default.phtml (or in your theme folder.)

Here you can see the cart item object $_item = $this->getItem(); at line 28. Then you have to load the product with product id.
<?php
    $_product = Mage::getModel('catalog/product')->load($_item->getProductId());
    $prodAttributeSet = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName();
?>

Friday, October 12, 2012

Get Product collection in Magento


 There are many way to get product collection.
 >> First preferable way to get Collection of Magento products :

   $visibility = array(
       Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
       Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG,
       Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH
    );
   $productCollection = Mage::getModel('catalog/product')->getCollection()
                                        ->addAttributeToSelect('id')
                                        ->addAttributeToSelect('name')
                                        ->addAttributeToFilter('visibility' , $visibility);

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($productCollection);
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($productCollection);

$productCollection->getSelect()->limit(12);


Wednesday, October 10, 2012

Remove next elements in jQuery


>>  This peace of code will remove Next input fields :

          jQuery("#selector").nextAll('input:hidden').remove();        // Input type "hidden"
          jQuery("#selector").nextAll('input:text').remove();            // Input type "text"

// By the above code All the input field after the selector will remove

          jQuery("#selector").next('input:hidden').remove();        // Input type "hidden"
          jQuery("#selector").next('input:text').remove();            // Input type "text"

// By the above code only the next input field of the selector will remove





Wednesday, October 3, 2012

How to get a html output through xml in Magento


>> Basically we use to get the content page like :

         <reference name="content">
            <block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml" />
        </reference>

  It'll return html output with header, footer content. If you want to get only the content of list.phtml
Then you have to modify the code with following:

        <block type="core/text_list" name="root" output="toHtml">
              <block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml" />
        </block>




The above code will return only the content of list.phtml.





             




 

Friday, September 28, 2012

Block/Static Block in magento


>> Following is the code by which you can get a Static Block content :

     <?php echo $this->getLayout()->createBlock("cms/block")->setBlockId("identifier")->toHtml() ?>

                                                                       OR
      <?php
                  $block = Mage::getModel('cms/block')->load('identifier');
                   echo $block->getTitle();
                   echo $block->getContent();
       ?>

>> You can also get it from CMS page: Admin=>CMS=>Pages=>Content

     {{block type="cms/block" block_id="my_block" template="cms/content.phtml"}}

              ===========================================================

>> Create a block programmatically :

       <?php echo $this->getLayout()->createBlock('myModule/block')->setTemplate('folder/custom.phtml')->toHtml();?>

       Note:* If you want to access a method of an existing block. You can do it like:
       $blockObject = $this->getLayout()->createBlock('existingblock/name');
       Now call any method of that block using the $blockObject->yourMethod()

>> Set new template to an existing Block :

     <?php echo $this->getLayout()->createBlock('newsletter/subscribe')->setTemplate('newsletter/subscribe.phtml')->toHtml();?>

>> You can also Pass a variable through block :

      1. Through Admin CMS page. 
      {{block type="core/template" category_id="10" template="page/custom.phtml"}}

      2. In the XML file.
      <block type="core/template" name="sub.cat.list" template="catalog/category/visitourstore/subcatlist.phtml" >
               <action method="setData"><name>category_id</name><value>10</value></action>
       </block>

     3. In The phtml page :
     <?php echo $this->getLayout()->createBlock('core/template')->setData('category_id',10)->setTemplate('page/custom.phtml')->toHtml();?>

   Now you can easily get the value as: <?php echo $this->getCategoryId();  ?>




Tuesday, September 25, 2012

Catalog product collection sort by attribute( select type ) value position


>> By default if you set an attribute(select type) in sort by dropdown. It'll sort by the attribute option's name.
          e.g. Color => 'Black', 'Red', 'White'
     but you want show the product of this attribute like : Color => 'White', 'Black', 'Red'
     Then set the option position here :

       

      Now open this page : app/code/core/Mage/Eav/Model/Entity/Attribute/Source/Table.php

      Find this function addValueSortToCollection at line 112.

      In this function you'll find the code:

      $collection->getSelect()
                ->order("{$this->getAttribute()->getAttributeCode()} {$dir}");


      You've to change this code for your "Color" attribute    
      Replace the above code with the following :

     <?php
      if($this->getAttribute()->getAttributeCode() != 'color'){                // Attribute Code here
              $collection->getSelect()
                ->order("{$this->getAttribute()->getAttributeCode()} {$dir}");

      }else{
             $table = Mage::getSingleton('core/resource')->getTableName('eav_attribute_option');
             $collection->getSelect()
                            ->joinLeft(array('so' => $table), 'IF('.$valueTable2.'.value_id > 0, '.$valueTable2.'.value, '.$valueTable1.'.value) = so.option_id')
                            ->order(array('so.sort_order ASC'));

     }
     ?>

Now, you'll see the product first which selected 'White' and so on..

Note :  If no color option selected for some product that show at first. So, you've set the color for all products. OR you can change the order   ->order(array('so.sort_order ASC')); To ->order(array('so.sort_order DESC')); and set the position reverse. greater value first.