I have tried to create a product grid custom extension
but product grid filter is not working and I am not able to save data
this is my code:
<?php
namespace MagebeesArtistsBlockAdminhtmlSlideEdit;
class Tabs extends MagentoBackendBlockWidgetTabs
{
protected function _construct()
{
parent::_construct();
$this->setId('slide_tabs');
$this->setDestElementId('edit_form');
$this->setTitle(__('Artists Album'));
}
protected function _prepareLayout()
{
$this->addTab('main_section',
[
'label' => __('Album Information'),
'title' => __('Album Information'),
'content' => $this->getLayout()->createBlock(
'MagebeesArtistsBlockAdminhtmlSlideEditTabMain')->toHtml(),
'active' => false
]
);
$this->addTab('img_section',
[
'label' => __('Images'),
'title' => __('Images'),
'content' => $this->getLayout()->createBlock(
'MagebeesArtistsBlockAdminhtmlSlideEditTabImages')->toHtml(),
'active' => false
]
);
// $this->addTab('video_section',
// [
// 'label' => __('Video'),
// 'title' => __('Video'),
// 'content' => $this->getLayout()->createBlock(
// 'MagebeesArtistsBlockAdminhtmlSlideEditTabVideo')->toHtml(),
// 'active' => false
// ]
// );
$this->addTab('video_section',
[
'label' => __('Video'),
'title' => __('Video'),
'content' => $this->getLayout()->createBlock(
'MagebeesArtistsBlockAdminhtmlSlideEditTabVideo'
)->toHtml(),
'active' => false
]
);
$this->addTab('tab_section',
[
'label' => __('tab_section'),
'title' => __('tab_section'),
'content' => $this->getLayout()->createBlock(
'MagebeesArtistsBlockAdminhtmlSlideEditTabCustomTab')->toHtml(),
'active' => false
]
);
$this->addTab('social_media',
[
'label' => __('social_media'),
'title' => __('social_media'),
'content' => $this->getLayout()->createBlock(
'MagebeesArtistsBlockAdminhtmlSlideEditTabMyConnection')->toHtml(),
'active' => false
]
);
$this->addTab('my_product',
[
'label' => __('my_product'),
'title' => __('my_product'),
'content' => $this->getLayout()->createBlock(
'MagebeesArtistsBlockAdminhtmlSlideEditTabMyProduct')->toHtml(),
'active' => false
]
);
return parent::_prepareLayout();
}
}
‘MagebeesArtistsBlockAdminhtmlSlideEditTabMyProduct
to get all product in grid
<?php
namespace MagebeesArtistsBlockAdminhtmlSlideEditTab;
use MagentoBackendBlockWidgetGridColumn;
use MagentoBackendBlockWidgetGridExtended;
class MyProduct extends Extended
{
/**
* Core registry
*
* @var MagentoFrameworkRegistry
*/
protected $_coreRegistry = null;
/**
* @var MagentoCatalogModelProductLinkFactory
*/
protected $_linkFactory;
/**
* @var MagentoEavModelResourceModelEntityAttributeSetCollectionFactory]
*/
protected $_setsFactory;
/**
* @var MagentoCatalogModelProductFactory
*/
protected $_productFactory;
/**
* @var MagentoCatalogModelProductType
*/
protected $_type;
/**
* @var MagentoCatalogModelProductAttributeSourceStatus
*/
protected $_status;
/**
* @var MagentoCatalogModelProductVisibility
*/
protected $_visibility;
/**
* @param MagentoBackendBlockTemplateContext $context
* @param MagentoBackendHelperData $backendHelper
* @param MagentoCatalogModelProductLinkFactory $linkFactory
* @param MagentoEavModelResourceModelEntityAttributeSetCollectionFactory $setsFactory
* @param MagentoCatalogModelProductFactory $productFactory
* @param MagentoCatalogModelProductType $type
* @param MagentoCatalogModelProductAttributeSourceStatus $status
* @param MagentoCatalogModelProductVisibility $visibility
* @param MagentoFrameworkRegistry $coreRegistry
* @param array $data
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
MagentoBackendBlockTemplateContext $context,
MagentoBackendHelperData $backendHelper,
MagentoCatalogModelProductLinkFactory $linkFactory,
MagentoEavModelResourceModelEntityAttributeSetCollectionFactory $setsFactory,
MagentoCatalogModelProductFactory $productFactory,
MagentoCatalogModelProductType $type,
MagentoCatalogModelProductAttributeSourceStatus $status,
MagentoCatalogModelProductVisibility $visibility,
MagentoFrameworkRegistry $coreRegistry,
array $data = []
) {
$this->_linkFactory = $linkFactory;
$this->_setsFactory = $setsFactory;
$this->_productFactory = $productFactory;
$this->_type = $type;
$this->_status = $status;
$this->_visibility = $visibility;
$this->_coreRegistry = $coreRegistry;
parent::__construct($context, $backendHelper, $data);
}
/**
* Set grid params
*
* @return void
*/
protected function _construct()
{
parent::_construct();
$this->setId('related_product_grid');
$this->setDefaultSort('entity_id');
$this->setUseAjax(true);
}
/**
* Add filter
*
* @param Column $column
* @return $this
*/
protected function _filterCollection($collection, $column)
{ die('dsas..');
if (!$value = $column->getFilter()->getValue()) {
return $this;
}
if ($value['from']) {
$collection->addFieldToFilter('position', ['gteq' => $value['from']]);
}
if ($value['to']) {
$collection->addFieldToFilter('position', ['lteq' => $value['to']]);
}
return $this;
}
// Inside your grid block class
protected function filterProductPosition($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return $this;
}
if ($value['from']) {
$collection->addFieldToFilter('position', ['gteq' => $value['from']]);
}
if ($value['to']) {
$collection->addFieldToFilter('position', ['lteq' => $value['to']]);
}
return $this;
}
protected function _getSelectedProducts()
{
$products = $this->getProductsRelated();
if (!is_array($products)) {
$products = array_keys($this->getSelectedRelatedProducts());
}
return $products;
}
/**
* Retirve currently edited product model
*
* @return MagentoCatalogModelProduct
*/
public function getBanner()
{
return $this->_coreRegistry->registry('current_banner');
}
/**
* Prepare collection
*
* @return Extended
*/
protected function _prepareCollection()
{
$collection = $this->_linkFactory->create()->getProductCollection()->addAttributeToSelect('*');
$this->setCollection($collection);
return parent::_prepareCollection();
}
/**
* Add columns to grid
*
* @return $this
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function _prepareColumns()
{
$this->addColumn(
'in_products',
[
'type' => 'checkbox',
'name' => 'in_products',
'values' => '',//$this->_getSelectedProducts(),
'align' => 'center',
'index' => 'entity_id',
'header_css_class' => 'col-select',
'column_css_class' => 'col-select'
]
);
$this->addColumn(
'entity_id',
[
'header' => __('ID'),
'sortable' => true,
'index' => 'entity_id',
'header_css_class' => 'col-id',
'column_css_class' => 'col-id'
]
);
$this->addColumn(
'name',
[
'header' => __('Name'),
'index' => 'name',
'header_css_class' => 'col-name',
'column_css_class' => 'col-name'
]
);
$this->addColumn(
'type',
[
'header' => __('Type'),
'index' => 'type_id',
'type' => 'options',
'options' => $this->_type->getOptionArray(),
'header_css_class' => 'col-type',
'column_css_class' => 'col-type'
]
);
$sets = $this->_setsFactory->create()->setEntityTypeFilter(
$this->_productFactory->create()->getResource()->getTypeId()
)->load()->toOptionHash();
$this->addColumn(
'set_name',
[
'header' => __('Attribute Set'),
'index' => 'attribute_set_id',
'type' => 'options',
'options' => $sets,
'header_css_class' => 'col-attr-name',
'column_css_class' => 'col-attr-name'
]
);
$this->addColumn(
'status',
[
'header' => __('Status'),
'index' => 'status',
'type' => 'options',
'options' => $this->_status->getOptionArray(),
'header_css_class' => 'col-status',
'column_css_class' => 'col-status'
]
);
$this->addColumn(
'visibility',
[
'header' => __('Visibility'),
'index' => 'visibility',
'type' => 'options',
'options' => $this->_visibility->getOptionArray(),
'header_css_class' => 'col-visibility',
'column_css_class' => 'col-visibility'
]
);
$this->addColumn(
'sku',
[
'header' => __('SKU'),
'index' => 'sku',
'header_css_class' => 'col-sku',
'column_css_class' => 'col-sku'
]
);
$this->addColumn(
'price',
[
'header' => __('Price'),
'type' => 'currency',
'currency_code' => (string)$this->_scopeConfig->getValue(
MagentoDirectoryModelCurrency::XML_PATH_CURRENCY_BASE,
MagentoStoreModelScopeInterface::SCOPE_STORE
),
'index' => 'price',
'header_css_class' => 'col-price',
'column_css_class' => 'col-price'
]
);
$this->addColumn(
'position',
[
'header' => __('Position'),
'name' => 'position',
'type' => 'number',
'validate_class' => 'validate-number',
'index' => 'position',
'header_css_class' => 'col-position',
'column_css_class' => 'col-position',
'filter_condition_callback' => [$this, 'filterProductPosition'],
]
);
return parent::_prepareColumns();
}
public function getSelectedRelatedProducts()
{
$banner = $this->getBanner();
$products = [];
if(!empty($banner->getRelatedProducts())){
foreach ($banner->getRelatedProducts() as $product) {
$products[$product['entity_id']] = ['position' => $product['position']];
}
}
return $products;
}
/**
* Rerieve grid URL
*
* @return string
*/
public function getGridUrl()
{
return $this->getUrl(
'artists/slide/relatedProductsGrid', // Replace with the correct URL path
['_current' => true] // Add any required parameters
);
}
}
anyone please help me.
How will this filter work? and how I can save the data. I have created product_data_base_file.