Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need custom tab for specific product type only not for all product type

I want to add new custom tab like in image "Price - Size" for my custom product type only

I have try code from this link-1 and link-2 but it show tab on all product type add/edit

my question is same as this but want to do this using coding

enter image description here

mysql4-install-0.1.0.php

$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'limits', array(
    'group'             => 'Price - Size',
    'type'              => 'varchar',
    'frontend'          => '',
            'backend'           => 'custproduct/entity_attribute_backend_limit',
    'label'             => 'Limit',
    'input'             => 'text',
    'class'             => '',
    'source'            => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
    'default'           => '1',
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => true,
    'unique'            => false,
    'apply_to'          => My_Custproduct_Model_Product_Type::TYPE_CustomProduct_PRODUCT, //also try 'custproduct'
    'is_configurable'   => false
));
$installer->addAttributeGroup('catalog_product', 'Default','Price - Size', 40);
$installer->addAttributeToSet('catalog_product','Default', 'Price - Size', 'limits');

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

 foreach ($fieldList as $field) {
    $applyTo = explode(',', $installer->getAttribute('catalog_product', $field, 'apply_to'));
    if (!in_array('custproduct', $applyTo)) {
        $applyTo[] = 'custproduct';
        $installer->updateAttribute('catalog_product', $field, 'apply_to', join(',', $applyTo));
    }
}
$installer->endSetup();

attribute 'limits' is added but it show on all product type I need it only with my custom product type(custproduct) only.

Thank for reply my issue solved now

just added 'limits' to $fieldList array

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

Thanks!!!

like image 287
Pragnesh Chauhan Avatar asked Dec 31 '12 07:12

Pragnesh Chauhan


2 Answers

Magento is very flexible, so there are numerous ways how you can achieve the desired result. The only problem is to determine the best way to do it, i.e. find the most reliable and effective way.

Two approaches can be suggested here. The choice depends on the technical details of the required functionality, which are not stated in the initial question:

  1. The custom tab will contain only basic fields, used for entering data for a product
  2. The custom tab will contain advanced fields, and/or javascript, and/or other custom HTML markup

Let's see the solution for both cases.

 

#1. The tab will contain only basic fields, used for entering data for a product

In such a case it is enough to use Magento's attributes mechanism. It allows to create attributes (fields) for a product, apply them to certain product types only, and separate the fields into groups (tabs).

This is how a script may look like.

<module_dir>/sql/install-1.0.0.0.php

<?php
/* @var $installer Mage_Catalog_Model_Resource_Setup */
$installer = $this;

// Add attribute
$allowedProductTypes = array(
    Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE,
    Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE,
);

$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'attribute_for_tab', array(
    'label'             => 'Attribute For Tab',
    'apply_to'          => implode(',', $allowedProductTypes),
    'type'              => 'varchar',
    'input'             => 'text',
    'default'           => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'user_defined'      => true,
    'visible'           => true,
    'required'          => false,
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'unique'            => false,
    'is_configurable'   => false
));

// Add group
$installer->addAttributeGroup(Mage_Catalog_Model_Product::ENTITY, 'Default', 'My Attribute Tab');

// Add attribute to set and group
$installer->addAttributeToSet(Mage_Catalog_Model_Product::ENTITY, 'Default', 'My Attribute Tab', 'attribute_for_tab');

 
Feel free to download simple module example for this approach: "attribute_tab.zip"

 
 

#2. The custom tab will contain advanced fields, and/or javascript, and/or other custom HTML markup

In such a case the tab should be created as a usual Magento block. And injected into the Tabs renderer via layout.

Custom tab block <module_dir>/Block/Adminhtml/Catalog/Product/Edit/Tab/Custom.php

<?php
class Zerkella_CustomTab_Block_Adminhtml_Catalog_Product_Edit_Tab_Custom
    extends Mage_Adminhtml_Block_Widget implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
    /**
     * Class constructor
     *
     */
    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('zerkella_customtab/catalog/product/edit/tab/custom.phtml');
    }

    /**
     * Get tab label
     *
     * @return string
     */
    public function getTabLabel()
    {
        return Mage::helper('zerkella_customtab')->__('My Custom Tab');
    }

    /**
     * Get tab title
     *
     * @return string
     */
    public function getTabTitle()
    {
        return Mage::helper('zerkella_customtab')->__('My Custom Tab');
    }

    /**
     * Check if tab can be displayed
     *
     * @return boolean
     */
    public function canShowTab()
    {
        $allowedProductTypes = array(
            Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE,
            Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE,
        );
        $productType = $this->_getProduct()->getTypeId();

        return in_array($productType, $allowedProductTypes);
    }

    /**
     * Retrieve product
     *
     * @return Mage_Catalog_Model_Product
     */
    protected function _getProduct()
    {
        return Mage::registry('current_product');
    }

    /**
     * Check if tab is hidden
     *
     * @return boolean
     */
    public function isHidden()
    {
        return false;
    }
}

 
The layout file app/design/adminhtml/default/default/layout/zerkella_customtab.xml:

<?xml version="1.0"?>
<layout>
    <adminhtml_catalog_product_new>
        <reference name="product_tabs">
            <action method="addTab">
                <name>my_custom_tab</name>
                <block>zerkella_customtab/adminhtml_catalog_product_edit_tab_custom</block>
            </action>
        </reference>
    </adminhtml_catalog_product_new>
    <adminhtml_catalog_product_edit>
        <reference name="product_tabs">
            <action method="addTab">
                <name>my_custom_tab</name>
                <block>zerkella_customtab/adminhtml_catalog_product_edit_tab_custom</block>
            </action>
        </reference>
    </adminhtml_catalog_product_edit>
</layout>

 
Note: if the product types, having custom tab, are fixed, then you can put them statically in the layout file, instead of checking them dynamically in block's canShowTab() method.

Here is the layout file for such a sample case, when product types with custom tab are fixed and include Downloadable only, app/design/adminhtml/default/default/layout/zerkella_customtab.xml:

<?xml version="1.0"?>
<layout>
    <adminhtml_catalog_product_downloadable>
        <reference name="product_tabs">
            <action method="addTab">
                <name>my_custom_tab</name>
                <block>zerkella_customtab/adminhtml_catalog_product_edit_tab_custom</block>
            </action>
        </reference>
    </adminhtml_catalog_product_downloadable>
</layout>

 
The rest is simple - everything you put into zerkella_customtab/catalog/product/edit/tab/custom.phtml will be rendered in the tab.

You can download simple module example for this approach: "custom_tab.zip"


Also I would not recommend to use class rewrites to implement the task. The approaches, described above, cover all the developer's needs. There is no sense to utilize rewrites. While rewrites is a powerful feature, which allows to do anything in Magento, it has two restrictions:

  • A class can be rewritten by one module only
  • If there are rewritten classes in your system, then more work will be needed in order to upgrade Magento to a newer version

The proposed approaches follow a natural way of customizing product tabs in Magento, so it is better to choose one of them.

Good luck with your store :)

like image 76
Andrey Tserkus Avatar answered Oct 21 '22 22:10

Andrey Tserkus


You should use a parameter apply_to

$productTypes = array(
    Mage_Catalog_Model_Product_Type::TYPE_SIMPLE,
    Mage_Catalog_Model_Product_Type::TYPE_BUNDLE,
    Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE,
    Mage_Catalog_Model_Product_Type::TYPE_VIRTUAL
);

$productTypes = join(',', $productTypes);
$globalScope = Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL;
$installer->addAttribute(
Mage_Catalog_Model_Product::ENTITY, 'artist_id',
array(
     'global'                  => $globalScope,
     'visible'                 => false,
     'required'                => false,
     'user_defined'            => true,
     'default'                 => '',
     'apply_to'                => $productTypes, // <-- apply_to
     'visible_on_front'        => false,
     'used_in_product_listing' => false,
     'type'                    => 'int', //backend_type

)
);

.. and add the attribute to the attribute set

$setup->addAttributeToSet(
    'product_catalog',
    %ATTRIBUTE_SET%,
    %ATTRIBUTE_GROUP%,
    'testing_attribute'
);

to get the default attributeset for products use this code:

$productDefaultAttributeSet = $installer->getDefaultAttributeSetId(Mage_Catalog_Model_Product::ENTITY);
like image 23
Igor Sydorenko Avatar answered Oct 21 '22 22:10

Igor Sydorenko