jeudi 23 avril 2015

Magento Custom Collection Prevents Proper Massaction Checkbox Selection

Magento 1.7.0.2 - I have created a custom collection by extending the Catalog Product Collection. Essentially what the collection is: a collection of unique combination of products and if any product has custom options then each unique combination of product id/option id is another item in the collection. The collection and count are working just fine.

However, the "Select All" button for the mass action checkbox selection only selects rows with a product id and not those where the product_id is concatenated with the option_id as shown in the image below.

enter image description here

Here is my collection class.

<?php 

class Lightsnholsters_Inventory_Model_Resource_Catalog_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection
{
  public function getSelectCountSql()
  {   
      $this->_renderFilters();
      $countSelect = clone $this->getSelect();
      $countSelect->reset(Zend_Db_Select::ORDER);
      $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
      $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
      $countSelect->reset(Zend_Db_Select::COLUMNS);

      // Count doesn't work with group by columns keep the group by 
      if(count($this->getSelect()->getPart(Zend_Db_Select::GROUP)) > 0) {
          $countSelect->reset(Zend_Db_Select::GROUP);
          $countSelect->distinct(true);
          $group = $this->getSelect()->getPart(Zend_Db_Select::GROUP);
          $countSelect->columns("COUNT(DISTINCT ".implode(", ", $group).")");
      } else {
          $countSelect->columns('COUNT(*)');
      }
      return $countSelect;
  }

  protected function _initSelect()
  {
      parent::_initSelect();
      $this->joinAttribute('name','catalog_product/name','entity_id');                  

      $this->getSelect()->reset(Zend_Db_Select::COLUMNS);

      $this->getSelect()->joinLeft(
          array('opt' => $this->getTable('catalog/product_option')),
          "e.entity_id = opt.product_id AND opt.type IN('radio','drop_down')",
          array(''));

      $this->getSelect()->joinLeft(
          array('opt_val' => $this->getTable('catalog/product_option_type_value')),
          "opt.option_id = opt_val.option_id AND opt_val.sku != '' AND opt_val.sku IS NOT NULL",
          array('entity_id' => new Zend_Db_Expr("CONCAT_WS('_',e.entity_id,opt_val.option_type_id)"),
                'option_type_id' => new Zend_Db_Expr("IF(opt_val.option_type_id > 0, opt_val.option_type_id,0)")));

      $this->getSelect()->joinLeft(
          array('opt_prc' => $this->getTable('catalog/product_option_type_price')),
          "opt_val.option_type_id = opt_prc.option_type_id AND opt_prc.store_id = {$this->getStoreId()}",
          array(''));

      $this->getSelect()->joinLeft(
          array('opt_ttl' => $this->getTable('catalog/product_option_type_title')),
          "opt_val.option_type_id = opt_ttl.option_type_id AND opt_ttl.store_id = {$this->getStoreId()}",
          array('name' => new Zend_Db_Expr("CONCAT_WS(' ',at_name.value,title)")));

      return $this;
  }  
}

Here is the massaction code that I use in the grid class

  protected function _prepareMassaction()
  {
    $this->setMassactionIdField('entity_id');
    $this->setMassactionIdFilter('entity_id');
    $this->getMassactionBlock()->setFormFieldName('entity_id');

    $this->getMassactionBlock()->addItem('updateAttributes', array(
      'label'   => $this->__('Update Attributes'),
      'url'     => $this->getUrl('*/*/massUpdateAttributes'),
    ));

    $statuses = array(
      array('label' => '',                    'value'=>''),
      array('label' => $this->__('Disabled'), 'value'=>0 ),
      array('label' => $this->__('Enabled') , 'value'=>1 )
    );
    $this->getMassactionBlock()->addItem('status', array(
         'label'=> $this->__('Change status'),
         'url'  => $this->getUrl('*/*/massStatus', array('_current'=>true)),
         'additional' => array(
             'visibility'  => array(
             'name'   => 'status',
             'type'   => 'select',
             'class'  => 'required-entry',
             'label'  => $this->__('Status'),
             'values' => $statuses
           )
         )
    ));

    Mage::dispatchEvent('lightsnholsters_inventory_autolist_grid_massaction_prepare', array('block' => $this));
    return $this;
  } 

It seems as though whatever field I put in $this->setMassactionIdField('entity_id'); is fully ignored.

My goal is that when I click "Select All" that all checkboxes are selected, not just the ones that do not have the option_id concatenated on.

Thank you for your help.




Aucun commentaire:

Enregistrer un commentaire