Skip to content

Commit c791fc8

Browse files
authored
Merge pull request #5 from justbetter/feature/admin-grid
Basic customer pricing grid
2 parents c1712d3 + d0464d2 commit c791fc8

File tree

15 files changed

+395
-3
lines changed

15 files changed

+395
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ We accept non-breaking PR's to accept functionality.
5454

5555
This module currently does not support pricing for specific stores / websites because we do not store a website/store id with each price.
5656

57-
### No insights in Magento
57+
### No adding/updating in the Magento 2 backend
5858

59-
There currently is no way to see the customer prices in the Magento 2 backend.
59+
It is currently only possible to view and delete customer specific prices in the backend.
6060

6161
## Contributing
6262

registration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
ComponentRegistrar::register(
66
ComponentRegistrar::MODULE,
77
'JustBetter_CustomerPricing',
8-
__DIR__ . '/src'
8+
__DIR__.'/src'
99
);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace JustBetter\CustomerPricing\Controller\Adminhtml\CustomerPricing;
4+
5+
use Magento\Backend\App\Action;
6+
use Magento\Backend\App\Action\Context;
7+
use JustBetter\CustomerPricing\Model\ResourceModel\CustomerPricing\CollectionFactory;
8+
use Magento\Framework\App\Action\HttpPostActionInterface;
9+
use Magento\Framework\Controller\ResultFactory;
10+
use Magento\Framework\Controller\ResultInterface;
11+
use Magento\Ui\Component\MassAction\Filter;
12+
13+
class MassDelete extends Action implements HttpPostActionInterface
14+
{
15+
public function __construct(
16+
Context $context,
17+
protected Filter $filter,
18+
protected CollectionFactory $collectionFactory
19+
) {
20+
parent::__construct($context);
21+
}
22+
23+
public function execute(): ResultInterface
24+
{
25+
$collection = $this->filter->getCollection($this->collectionFactory->create());
26+
$deletedItems = 0;
27+
28+
foreach ($collection->getItems() as $item) {
29+
$item->delete();
30+
$deletedItems++;
31+
}
32+
33+
$this->messageManager->addSuccessMessage(__('Deleted %1 item(s).', $deletedItems));
34+
35+
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
36+
return $resultRedirect->setPath('customerpricing/index/index');
37+
}
38+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace JustBetter\CustomerPricing\Controller\Adminhtml\Index;
4+
5+
use Magento\Backend\App\Action;
6+
use Magento\Backend\App\Action\Context;
7+
use Magento\Framework\View\Result\Page;
8+
use Magento\Framework\View\Result\PageFactory;
9+
use Magento\Framework\App\Action\HttpGetActionInterface;
10+
11+
class Index extends Action implements HttpGetActionInterface
12+
{
13+
public function __construct(
14+
Context $context,
15+
protected PageFactory $pageFactory
16+
) {
17+
parent::__construct($context);
18+
}
19+
20+
public function execute(): Page
21+
{
22+
$resultPage = $this->pageFactory->create();
23+
$resultPage->setActiveMenu('Magento_Catalog::catalog_products');
24+
$resultPage->getConfig()->getTitle()->prepend(__('JustBetter - Customer Specific Pricing'));
25+
26+
return $resultPage;
27+
}
28+
29+
protected function _isAllowed(): bool
30+
{
31+
return $this->_authorization->isAllowed('Magento_Catalog::catalog');
32+
}
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace JustBetter\CustomerPricing\Ui\Component\Listing\Column;
3+
4+
use Magento\Customer\Api\CustomerRepositoryInterface;
5+
use Magento\Framework\View\Element\UiComponent\ContextInterface;
6+
use Magento\Framework\View\Element\UiComponentFactory;
7+
use Magento\Ui\Component\Listing\Columns\Column;
8+
9+
class CustomerInfo extends Column
10+
{
11+
protected $customerRepository;
12+
13+
public function __construct(
14+
ContextInterface $context,
15+
UiComponentFactory $uiComponentFactory,
16+
CustomerRepositoryInterface $customerRepository,
17+
array $components = [],
18+
array $data = []
19+
) {
20+
$this->customerRepository = $customerRepository;
21+
parent::__construct($context, $uiComponentFactory, $components, $data);
22+
}
23+
24+
public function prepareDataSource(array $dataSource)
25+
{
26+
if (isset($dataSource['data']['items'])) {
27+
foreach ($dataSource['data']['items'] as &$item) {
28+
$customerId = $item['customer_id'];
29+
$customer = $this->customerRepository->getById($customerId);
30+
$customerName = $customer->getFirstname() . ' ' . $customer->getLastname();
31+
$item[$this->getData('name')] = $customerName . ' (' . $customerId . ')';
32+
}
33+
}
34+
35+
return $dataSource;
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace JustBetter\CustomerPricing\Ui\Component\Listing\Column;
4+
5+
use Magento\Framework\View\Element\UiComponent\ContextInterface;
6+
use Magento\Framework\View\Element\UiComponentFactory;
7+
use Magento\Ui\Component\Listing\Columns\Column;
8+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
9+
10+
class ProductSku extends Column
11+
{
12+
protected $productRepository;
13+
14+
public function __construct(
15+
ContextInterface $context,
16+
UiComponentFactory $uiComponentFactory,
17+
protected ProductResource $productResource,
18+
array $components = [],
19+
array $data = []
20+
) {
21+
parent::__construct($context, $uiComponentFactory, $components, $data);
22+
}
23+
24+
public function prepareDataSource(array $dataSource)
25+
{
26+
if (isset($dataSource['data']['items'])) {
27+
foreach ($dataSource['data']['items'] as &$item) {
28+
$productId = $item['product_id'];
29+
$sku = $this->productResource->getProductsSku([$productId])[0] ?? [];
30+
$item[$this->getData('name')] = $sku['sku'] ?? '';
31+
}
32+
}
33+
34+
return $dataSource;
35+
}
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace JustBetter\CustomerPricing\Ui\DataProvider\CustomerPricing\Listing;
4+
5+
use Magento\Framework\Data\Collection\AbstractDb;
6+
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
7+
use Magento\Framework\Data\Collection\EntityFactoryInterface;
8+
use Magento\Framework\Event\ManagerInterface;
9+
use Psr\Log\LoggerInterface;
10+
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
11+
12+
class Collection extends SearchResult
13+
{
14+
public function __construct(
15+
EntityFactoryInterface $entityFactory,
16+
LoggerInterface $logger,
17+
FetchStrategyInterface $fetchStrategy,
18+
ManagerInterface $eventManager,
19+
$mainTable,
20+
$resourceModel,
21+
$identifierName = null,
22+
$connectionName = null,
23+
AbstractDb $resource = null
24+
) {
25+
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel, $identifierName, $connectionName);
26+
}
27+
28+
protected function _initSelect(): void
29+
{
30+
$this->addFilterToMap('id', 'main_table.id');
31+
$this->addFilterToMap('name', 'customer_pricing.product_id');
32+
parent::_initSelect();
33+
}
34+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace JustBetter\CustomerPricing\Ui\DataProvider\CustomerPricing\Listing\Column;
4+
5+
use Magento\Framework\View\Element\UiComponentFactory;
6+
use Magento\Framework\View\Element\UiComponent\ContextInterface;
7+
use Magento\Framework\UrlInterface;
8+
use Magento\Ui\Component\Listing\Columns\Column;
9+
10+
class Actions extends Column
11+
{
12+
public function __construct(
13+
ContextInterface $context,
14+
UiComponentFactory $uiComponentFactory,
15+
protected UrlInterface $urlBuilder,
16+
protected $viewUrl = '',
17+
array $components = [],
18+
array $data = []
19+
) {
20+
parent::__construct($context, $uiComponentFactory, $components, $data);
21+
}
22+
23+
public function prepareDataSource(array $dataSource)
24+
{
25+
if (isset($dataSource['data']['items'])) {
26+
foreach ($dataSource['data']['items'] as &$item) {
27+
$name = $this->getData('name');
28+
if (isset($item['entity_id'])) {
29+
$item[$name]['view'] = [
30+
'href' => $this->urlBuilder->getUrl($this->viewUrl, ['id' => $item['entity_id']]),
31+
'target' => '_blank',
32+
'label' => __('View on Frontend')
33+
];
34+
}
35+
}
36+
}
37+
return $dataSource;
38+
}
39+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace JustBetter\CustomerPricing\Ui\DataProvider\CustomerPricing;
4+
5+
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider;
6+
7+
class ListingDataProvider extends DataProvider
8+
{
9+
}

src/etc/adminhtml/menu.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
3+
<menu>
4+
<add id="JustBetter_CustomerPricing::home" title="Customer Specific Prices" module="JustBetter_CustomerPricing" sortOrder="1000" parent="Magento_Catalog::catalog_products" resource="Magento_Catalog::catalog" action="customerpricing/index/index" />
5+
</menu>
6+
</config>

0 commit comments

Comments
 (0)