<?php
namespace BlissPricePerCustomerBlockAdminhtmlTab;
use MagentoCatalogModelProductVisibility;
use MagentoFrameworkAppObjectManager;
use MagentoStoreModelStore;
use MagentoBackendBlockTemplateContext;
use MagentoBackendHelperData;
use MagentoCatalogModelProductFactory;
use BlissPricePerCustomerModelResourceModelCustomerPriceProdcutCollectionFactory;
use MagentoFrameworkRegistry;
use MagentoFrameworkModuleManager;
use MagentoStoreModelStoreManagerInterface;
use MagentoBackendBlockWidgetGridExtended;
class Productgrid extends Extended
{
/**
* @var MagentoFrameworkRegistry
*/
protected $coreRegistry = null;
/**
* @var MagentoCatalogModelProductFactory
*/
protected $productFactory;
/**
* @var BlissPricePerCustomerModelResourceModelCustomerPriceProdcutCollectionFactory
*/
protected $productCollFactory;
/**
* @param MagentoBackendBlockTemplateContext $context
* @param MagentoBackendHelperData $backendHelper
* @param MagentoCatalogModelProductFactory $productFactory
* @param MagentoFrameworkRegistry $coreRegistry
* @param MagentoFrameworkModuleManager $moduleManager
* @param MagentoStoreModelStoreManagerInterface $storeManager
* @param Visibility|null $visibility
* @param array $data
*/
public function __construct(
Context $context,
Data $backendHelper,
ProductFactory $productFactory,
CollectionFactory $productCollFactory,
Registry $coreRegistry,
Manager $moduleManager,
StoreManagerInterface $storeManager,
Visibility $visibility = null,
array $data = []
) {
$this->productFactory = $productFactory;
$this->productCollFactory = $productCollFactory;
$this->coreRegistry = $coreRegistry;
$this->moduleManager = $moduleManager;
$this->_storeManager = $storeManager;
$this->visibility = $visibility ?: ObjectManager::getInstance()->get(Visibility::class);
parent::__construct($context, $backendHelper, $data);
}
/**
* [_construct description]
* @return [type] [description]
*/
protected function _construct()
{
parent::_construct();
$this->setId('bliss_grid_products');
$this->setDefaultSort('entity_id');
$this->setDefaultDir('ASC');
$this->setUseAjax(true);
if ($this->getRequest()->getParam('entity_id')) {
$this->setDefaultFilter(['in_products' => 0]);
} else {
$this->setDefaultFilter(['in_products' => 1]);
}
$this->setSaveParametersInSession(true);
}
/**
* @inheritdoc
*/
public function getGridUrl()
{
return $this->getUrl('bliss_pricepercustomer/*/customerPrice', ['_current' => true]);
}
/**
* [get store id]
*
* @return Store
*/
protected function _getStore()
{
$storeId = (int) $this->getRequest()->getParam('store', 0);
return $this->_storeManager->getStore($storeId);
}
protected function _prepareCollection()
{
$store = $this->_getStore();
$collection = $this->productFactory->create()->getCollection()->addAttributeToSelect(
'sku'
)->addAttributeToSelect(
'name'
)->addAttributeToSelect(
'attribute_set_id'
)->addAttributeToSelect(
'type_id'
)->addAttributeToSelect(
'special_price'
)->setStore(
$store
);
// Joining additional attributes like price, status, visibility as per your original logic
if ($this->moduleManager->isEnabled('Magento_CatalogInventory')) {
$collection->joinField(
'qty',
'cataloginventory_stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left'
);
}
if ($store->getId()) {
$collection->setStoreId($store->getId());
$collection->addStoreFilter($store);
$collection->joinAttribute(
'name',
'catalog_product/name',
'entity_id',
null,
'inner',
Store::DEFAULT_STORE_ID
);
$collection->joinAttribute(
'status',
'catalog_product/status',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute(
'visibility',
'catalog_product/visibility',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute('price', 'catalog_product/price', 'entity_id', null, 'left', $store->getId());
} else {
$collection->addAttributeToSelect('price');
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
}
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _addColumnFilterToCollection($column)
{
if ($column->getId() == 'in_products') {
$productIds = $this->_getSelectedProducts();
if (empty($productIds)) {
$productIds = 0;
}
if ($column->getFilter()->getValue()) {
$this->getCollection()->addFieldToFilter('entity_id', ['in' => $productIds]);
} else {
if ($productIds) {
$this->getCollection()->addFieldToFilter('entity_id', ['nin' => $productIds]);
}
}
} else {
parent::_addColumnFilterToCollection($column);
}
return $this;
}
/**
* @return Extended
*/
protected function _prepareColumns()
{
$this->addColumn(
'in_products',
[
'type' => 'checkbox',
'html_name' => 'products_id',
'required' => true,
'values' => $this->_getSelectedProducts(),
'align' => 'center',
'index' => 'entity_id',
]
);
$this->addColumn(
'entity_id',
[
'header' => __('ID'),
'width' => '50px',
'index' => 'entity_id',
'type' => 'number',
]
);
$this->addColumn(
'name',
[
'header' => __('Name'),
'index' => 'name',
'header_css_class' => 'col-type',
'column_css_class' => 'col-type',
]
);
$this->addColumn(
'type_id',
[
'header' => __('Type'),
'index' => 'type_id',
'header_css_class' => 'col-type',
'column_css_class' => 'col-type',
]
);
$this->addColumn(
'sku',
[
'header' => __('SKU'),
'index' => 'sku',
'header_css_class' => 'col-sku',
'column_css_class' => 'col-sku',
]
);
$store = $this->_getStore();
$this->addColumn(
'price',
[
'header' => __('Price'),
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'index' => 'price',
'header_css_class' => 'col-price',
'column_css_class' => 'col-price',
]
);
// Add new columns for the additional fields
$this->addColumn(
'special_price',
[
'header' => __('Special Price'),
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'index' => 'special_price',
'header_css_class' => 'col-special-price',
'column_css_class' => 'col-special-price',
]
);
$this->addColumn(
'customer_price',
[
'header' => __('Customer Price'),
'type' => 'input',
'currency_code' => $store->getBaseCurrency()->getCode(),
'index' => 'customer_price',
'header_css_class' => 'col-customer-price',
'column_css_class' => 'col-customer-price',
]
);
$this->addColumn(
'customer_special_price',
[
'header' => __('Customer Special Price'),
'type' => 'input',
'currency_code' => $store->getBaseCurrency()->getCode(),
'index' => 'customer_special_price',
'header_css_class' => 'col-customer-special-price',
'column_css_class' => 'col-customer-special-price',
]
);
return parent::_prepareColumns();
}
/**
* @return array
*/
protected function _getSelectedProducts()
{
$products = array_keys($this->getSelectedProducts());
return $products;
}
/**
* @return array
*/
public function getSelectedProducts()
{
$id = $this->getRequest()->getParam('id');
$model = $this->productCollFactory->create()->addFieldToFilter('entity_id', $id);
$grids = [];
foreach ($model as $key => $value) {
$grids[] = $value->getProductId();
}
$prodId = [];
foreach ($grids as $obj) {
$prodId[$obj] = ['position' => "0"];
}
return $prodId;
}
public function getCustomerPrice()
{
$id = $this->getRequest()->getParam('id');
$collection = $this->productCollFactory->create();
$collection->addFieldToFilter('entity_id', $id);
$customerPrices = [];
foreach ($collection as $item) {
$customerPrice = $item->getCustomerPrice();
}
return $customerPrices;
}
public function getCustomerSpecialPrice()
{
$id = $this->getRequest()->getParam('id');
$collection = $this->productCollFactory->create();
$collection->addFieldToFilter('entity_id', $id);
$customerPrices = [];
foreach ($collection as $item) {
$customerPrice = $item->getCustomerSpecialPrice();
}
return $customerPrices;
}
}