Home » Php » php – I need custom tab for specific product type only not for all product type

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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!!!

How to&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 🙂

Answer:

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);

Answer:

New Group in Backend

Here is the code to create a new “group” tab in the backend:

$installer = $this;
$productAttributesSetup = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();

// this is the line you need 
$productAttributesSetup->addAttributeGroup('catalog_product', 'Attribute Set', 'Name of Group');
$installer->endSetup();

Only Special Products

To have it come up only on certain products follow these steps:

  1. Create a new attribute set (based on the one you are already using)
  2. Add the custom tab / group to this new attribute set
  3. All special products (that you want to have this tab in) should use this new attribute set

Getting Custom Product Working in Cart

Inchoo has created a ‘custom product’ module, I would suggest you compare your code with theirs.

Here is a link to their module (and article):

http://inchoo.net/ecommerce/magento/how-to-create-a-new-product-type-in-magento/

Answer:

I think to get the behavior you want, you will need to rewrite the class Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs

In there, the method _prepareLayout() creates all the tabs, i.e.:

    foreach ($groupCollection as $group) {
    ...
    $this->addTab('group_'.$group->getId(), array(
        'label' => Mage::helper('catalog')->__($group->getAttributeGroupName()),
        'content'   => $this->_translateHtml(
                          $this->getLayout()
                               ->createBlock($this->getAttributeTabBlock(),
                                   'adminhtml.catalog.product.edit.tab.attributes')
                               ->setGroup($group)
                               ->setGroupAttributes($attributes)
                               ->toHtml()),
            ));
    }

Inside the method you have the variable $product which you can use to check for the product type. So check for the specific group name of your attribute and your product type and add the corresponding tab only, if it matches the way you want.