سؤال

I need to change the multi-select attributes in the admin product edit form to multiple checkbox attributes. Is this possible?

هل كانت مفيدة؟

المحلول

After long research, I did some workarounds on this and yes it did the job. Here are my findings

Magento 1.9 doesn't allow to create a multiple checkbox attribute from the backend. It has some predefined input types but not multiple checkboxes.

I created a module for writing an observer for the event

"adminhtml_catalog_product_edit_prepare_form"

My module config.xml will look like follows.

<?xml version="1.0"?>
<config>
<modules>
    <Company_Mymodule>
        <version>0.7.2</version>
    </Company_Mymodule>
</modules>
<global>
    <blocks>
        <productattributesmultiselect>
            <class>Company_Mymodule_Block</class>
        </productattributesmultiselect>
    </blocks>
    <models>
        <productattributesmultiselect>
            <class>Company_Mymodule_Model</class>
        </productattributesmultiselect>
    </models>
    <events>
        <adminhtml_catalog_product_edit_prepare_form>
            <observers>
                <productattributesmultiselect_adminhtml_catalog_product_edit_prepare_form_observer>
                    <type>singleton</type>
                    <class>productattributesmultiselect/observer</class>
                    <method>addRenderer</method>
                </productattributesmultiselect_adminhtml_catalog_product_edit_prepare_form_observer>
            </observers>
        </adminhtml_catalog_product_edit_prepare_form>

    </events>    
</global>

Next, write an observer model for handling the event. It should be Company/Mymodule/Model/Observer.php

<?php

class Bootworld_Product_Model_Observer
{
  public function addRenderer($observer)
  {
    $form = $observer->getForm();

    //array of attribute codes of multi-select fields which you want to 
    //convert to checkboxes 

    $fields = array( 
     "multiselect_field_1", //attribute code
     "multiselect_field_2",
     ".......",

     );

    //Iterate 

    foreach ($fields as $element) {
        $field = $form->getElement($element);

        if ($field) {

            //set the renderer block of the element 
            //Define a block in your module Campany/Mymodule/Block

            $field->setRenderer(Mage::app()->getLayout()->createBlock(
                 'productattributesmultiselect/adminhtml_edit_tab_attribute'
            ));
        }

     }
   }

 }

Create a block file in Block folder of your module, In this example, it gonna be in the path Company / Mymodule / Block / Adminhtml / Edit / Tab / Attribute.php

class Company_Mymodule_Block_Adminhtml_Edit_Tab_Attribute extends Mage_Adminhtml_Block_Widget implements Varien_Data_Form_Element_Renderer_Interface
{ 
   public function __construct()
 {
    $this->setTemplate('comapny/mymodule/edit/multiplecheckboxes.phtml');
 }

}

Last and final step create a template file in app/design/adminhtml/default/default/comapny/mymodule/edit/multiplecheckboxes.phtml for itrating the values ( multiselect options) and display it with checkbox.

<?php

   $_htmlId = $this->getElement()->getHtmlId();
   $_htmlClass = $this->getElement()->getClass();
   $_htmlName = $this->getElement()->getName();
   $_values = $this->getElement()->getValue();

   //get attribute 

   $attribute = 
     Mage::getModel('eav/entity_attribute')- >loadByCode('catalog_product', 
     $_htmlId);

   //values collection of attribute options 

   $valuesCollection = 
   Mage::getResourceModel('eav/entity_attribute_option_collection')
                            ->setAttributeFilter($attribute->getId())
                            ->setStoreFilter(0, false);                            

   $_values = explode(',',$_values); //attribute values

 ?>
<tr>
   <td class="label"><?php echo $this->getElement()->getLabel(); ?></td>
   <td colspan="10" class="grid tier">

  <ul class="checkbox"> 
    <?php foreach($valuesCollection->getData() as $value): ?>

     <li> <input type="checkbox" name="<?php echo $_htmlName ?>" value="<?php 
          echo $value['option_id']?>" <?php echo 
          in_array($value['option_id'],$_values) ? 'checked' : '' ?> id="<?php 
          echo $value['option_id']?>" /> 
         <label for="<?php echo $value['option_id']?>"><?php echo 
          $value['value'] ?>
         </label>
     </li> 

   <?php endforeach;?>    
     </ul> 
  </td>
</tr>
<style>
 ul.checkbox li { 
 border: 1px solid; 
 display:inline-block;
 width:15em;
 padding: 5px;
}

نصائح أخرى

There is no option in magento for change the attribute type from one to another.

After create the attribute,you can't edit the attribute type. It will show in disable mode,refer the below screenshot.

attribute edit page

And also magento don't have an attribute type like "Multiple checkbox". "Multiple Select" attribute type is working like a multiple checkbox.

Refer the below screenshot for checking available attribute types in Magento.

attribute types

Thank you..

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top